zoukankan      html  css  js  c++  java
  • 最近做统计用到的几个常用sql

    计算同比

    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;
    

    欢迎搜索关注本人与朋友共同开发的微信面经小程序【大厂面试助手】和公众号【微瞰技术】

    file
    file

  • 相关阅读:
    JavaScript 检测浏览器更多信息【每日一段代码66】
    JavaScript throw 声明【每日一段代码64】
    JavaScript 计时器2 【每日一段代码73】
    JavaScript 按钮动画【每日一段代码70】
    JavaScript 检测浏览器【每日一段代码67】
    JavaScript 使用计时事件制作的钟表 【每日一段代码76】
    一个实现恢复删除机制(do undo)的设计
    基于邻接表的广度优先搜索遍历
    HDU1045 Fire Net
    深度遍历
  • 原文地址:https://www.cnblogs.com/zhendiao/p/14061223.html
Copyright © 2011-2022 走看看