做统计用到的几个常用sql

17 篇文章 1 订阅
2 篇文章 0 订阅

计算同比

SELECT
	old.now_time,
	ROUND( ( new.count - old.count ) / old.count * 100, 2 ),
	new.count ncount,
	old.count ocount 
FROM (
SELECT
	sum( IF ( b.count > 1, 1, 1 ) ) count,
	b.now_time,
	b.tenant_code 
FROM
	(
	SELECT
		date_format( ro.create_dt, '%Y-%m' ) AS now_time,
		a.tenant_code,
		a.order_no,
		count( a.id ) count 
	FROM
		t_rep_order_timeout a 
	INNER JOIN t_rep_order ro on 
		a.tenant_code = ro.tenant_code and a.order_no = ro.order_no
	WHERE
		a.tenant_code = 'zlyy' 
	GROUP BY
		date_format( ro.create_dt, '%Y-%m' ),
		a.tenant_code,
		a.order_no 
	) b 
GROUP BY
	b.now_time,
	b.tenant_code
	) new 
LEFT JOIN
(SELECT
	sum( IF ( b.count > 1, 1, 1 ) ) count,
	b.now_time,
	b.tenant_code 
FROM
	(
	SELECT
		date_format( DATE_ADD( ro.create_dt, INTERVAL 1 YEAR ), '%Y-%m') AS now_time,
		a.tenant_code,
		a.order_no,
		count( a.id ) count 
	FROM
		t_rep_order_timeout a 
	INNER JOIN t_rep_order ro on 
		a.tenant_code = ro.tenant_code and a.order_no = ro.order_no
	WHERE
		a.tenant_code = 'zlyy' 
	GROUP BY
		date_format( DATE_ADD( ro.create_dt, INTERVAL 1 YEAR ), '%Y-%m'),
		a.tenant_code,
		a.order_no 
	) b 
GROUP BY
	b.now_time,
	b.tenant_code) old 
	on old.tenant_code = new.tenant_code and old.now_time = new.now_time

计算环比

SELECT
	old.now_time,
	ROUND( ( new.count - old.count ) / old.count * 100, 2 ),
	new.count ncount,
	old.count ocount 
FROM (
SELECT
	sum( IF ( b.count > 1, 1, 1 ) ) count,
	b.now_time,
	b.tenant_code 
FROM
	(
	SELECT
		date_format( ro.create_dt, '%Y-%m' ) AS now_time,
		a.tenant_code,
		a.order_no,
		count( a.id ) count 
	FROM
		t_rep_order_timeout a 
	INNER JOIN t_rep_order ro on 
		a.tenant_code = ro.tenant_code and a.order_no = ro.order_no
	WHERE
		a.tenant_code = 'zlyy' 
	GROUP BY
		date_format( ro.create_dt, '%Y-%m' ),
		a.tenant_code,
		a.order_no 
	) b 
GROUP BY
	b.now_time,
	b.tenant_code
	) new 
LEFT JOIN
(SELECT
	sum( IF ( b.count > 1, 1, 1 ) ) count,
	b.now_time,
	b.tenant_code 
FROM
	(
	SELECT
		date_format( DATE_ADD( ro.create_dt, INTERVAL 1 MONTH ), '%Y-%m') AS now_time,
		a.tenant_code,
		a.order_no,
		count( a.id ) count 
	FROM
		t_rep_order_timeout a 
	INNER JOIN t_rep_order ro on 
		a.tenant_code = ro.tenant_code and a.order_no = ro.order_no
	WHERE
		a.tenant_code = 'zlyy' 
	GROUP BY
		date_format( DATE_ADD( ro.create_dt, INTERVAL 1 MONTH ), '%Y-%m'),
		a.tenant_code,
		a.order_no 
	) b 
GROUP BY
	b.now_time,
	b.tenant_code) old 
	on old.tenant_code = new.tenant_code and old.now_time = new.now_time

获取某一年份所有月份

SELECT
CASE
		
	WHEN
		length( mon ) = 1 THEN
			concat( '2019-0', mon ) ELSE concat( '2019-', mon ) 
		END months 
FROM
	( SELECT @m := @m + 1 mon FROM t_rep_order_timeout, ( SELECT @m := 0 ) a ) aa 
	LIMIT 12

此处所用的辅助表t_rep_oder_timeot表数据必须超过12条

删除表内的重复数据

DELETE t 
FROM
	interview t
	LEFT JOIN ( SELECT title, min( id ) AS min_id FROM interview GROUP BY title ) t1 ON t.id = t1.min_id 
WHERE
	t1.min_id IS NULL;

在这里插入图片描述
希望本文对你有所帮助~~如果对接口测试、自动化测试、面试经验交流感兴趣可以加入我们。642830685,免费领取最新软件测试大厂面试资料和Python自动化、接口、框架搭建学习资料!技术大牛解惑答疑,同行一起交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值