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

相关推荐
StarRocks_labs2 小时前
StarRocks Community Monthly Newsletter (Jun)
数据库·starrocks·数据湖·物化视图·存算分离
光电的一只菜鸡3 小时前
ubuntu之坑(十五)——设备树
linux·数据库·ubuntu
ob熔天使——武3 小时前
MySQL
数据库·mysql
小光学长4 小时前
基于vue框架的防疫物资仓库管理系统09y38(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
野生技术架构师8 小时前
MySQL数据实时同步到Elasticsearch的高效解决方案
数据库·mysql·elasticsearch
白仑色8 小时前
Oracle 高可用性与安全性
数据库·oracle·数据安全·goldengate·高可用架构
紫无之紫9 小时前
SQL性能调优经验总结
数据库·sql·性能调优
CZZDg9 小时前
Redis Sentinel哨兵集群
java·网络·数据库
__风__9 小时前
PostgreSQL ExecInitIndexScan 函数解析
数据库·postgresql
小云数据库服务专线9 小时前
GaussDB in的用法
数据库·sql·gaussdb