美文网首页
最近做统计用到的几个常用sql

最近做统计用到的几个常用sql

作者: 赵镇 | 来源:发表于2020-11-30 14:36 被阅读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;

欢迎关注和点赞

相关文章

  • 最近做统计用到的几个常用sql

    计算同比 计算环比 获取某一年份所有月份 此处所用的辅助表t_rep_oder_timeot表数据必须超过12条 ...

  • 常用SQL语句分享

    前言: 日常工作或学习过程中,我们可能会经常用到某些SQL,建议大家多多整理记录下这些常用的SQL,这样后续用到会...

  • MySQL GROUP BY和聚合函数

    聚合函数 使用sql的过程中有可能用到统计汇总的情况下就要使用聚合函数,常用的有5种。 count():计算表中的...

  • 数据分析常用SQL

    ?最近分析数据,用到了一些常用的SQL 代码,按照习惯,整理记录 一、联结表 SQL最强大的功能之一就是能在数据查...

  • mysql中的索引

    最近在做统计数据落地,写一些比较复杂的sql语句!更新数据用到on...depulicate key update...

  • MySQL Explain查看执行计划

    我们常常用到explain这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句有没有使用上了索引,有没有做...

  • python的分组统计之谜(一)

    最近处理数据比较多,而分组统计经常用到,所以在此做个记录 下次介绍pivot_table

  • 经常用到的sql语句

    获取一个表中的数据插入到另一个表 insert into studentsNew(id,name,sex,age)...

  • 常见面试题之数据库查询

    最近在面试的时候常见SQL题目: 1、统计胜负结果的SQL语句 期望结果: 具体实现SQL: 具体SQL: SEL...

  • Toodledo常用到的几个功能

    记得使用Wunderlist时的那些日子 我曾有大概两年的时间使用Wunderlist来管理我的事务,它漂亮灵巧,...

网友评论

      本文标题:最近做统计用到的几个常用sql

      本文链接:https://www.haomeiwen.com/subject/tzbrwktx.html