• mysql按月分组并补齐


    select
        a.month,
        IFNULL(b.point_count_0, 0) as point_count_0,
        IFNULL(b.point_count_1, 0) as point_count_1,
        IFNULL(b.point_count_2, 0) as point_count_2,
        IFNULL(b.point_count_3, 0) as point_count_3
    from
        (
        select
            DATE_FORMAT(CURDATE(), '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 1 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 2 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 3 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 4 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 5 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 6 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 7 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 8 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 9 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 10 month, '%Y-%m') as month
    union
        select
            DATE_FORMAT(CURDATE() - interval 11 month, '%Y-%m') as month
            ) a
    left join (
        select
            DATE_FORMAT(jsrp.create_time, '%Y-%m') as month,
            count(jsrg.grade_code = '64b73c51de18904e31ee3365' or null) as point_count_0,
            count(jsrg.grade_code = '64b73c5ede18904e31ee3366' or null) as point_count_1,
            count(jsrg.grade_code = '64b73c78de18904e31ee3367' or null) as point_count_2,
            count(jsrg.grade_code = '64b73c78de18904e31ee3368' or null) as point_count_3
        from
            jld_safe_risk_point jsrp
        left join jld_safe_risk_grade jsrg 
            on
            jsrp.grade_code = jsrg.grade_code
        where
            DATE_FORMAT(jsrp.create_time, '%Y-%m') > DATE_FORMAT(DATE_SUB(CURDATE(), interval 12 month), '%Y-%m')
        group by
            month
                ) b
                on
        a.month = b.month
    order by
        a.month asc
     

  • 相关阅读:
    BPF 调度器 sched_ext 实现机制、调度流程及样例
    JavaScript 如何验证 URL
    第八章 排序 十二、败者树
    数据结构––kmp算法(串)
    python-----matplotlib详细教程(未完...)
    SpringBoot集成EasyExcel快速人们
    0730~Mysql优化
    时序分解 | MATLAB实现北方苍鹰优化算法NGO优化VMD信号分量可视化
    SpringCloud 04 Eureka 服务注册和发现
    nRF5340(入门篇)之1.1 nrf5340芯片简介
  • 原文地址:https://blog.csdn.net/Heyll__/article/details/133122596