前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >最近用到的几个sql

最近用到的几个sql

作者头像
zhaozhen
发布2021-07-15 10:46:09
3460
发布2021-07-15 10:46:09
举报

计算同比

代码语言:javascript
复制
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

计算环比

代码语言:javascript
复制
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

获取某一年份所有月份

代码语言:javascript
复制
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条

删除表内的重复数据

代码语言:javascript
复制
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;
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微瞰技术 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 计算同比
  • 计算环比
  • 获取某一年份所有月份
  • 删除表内的重复数据
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档