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

相关推荐
indexsunny1 小时前
互联网大厂Java求职面试实战:微服务与Spring生态全攻略
java·数据库·spring boot·安全·微服务·面试·消息队列
沪漂阿龙1 小时前
别再让数据库“吃”脏数据了!一文讲透MySQL约束,从入门到精通
数据库·mysql
skiy2 小时前
java与mysql连接 使用mysql-connector-java连接msql
java·开发语言·mysql
2401_873544923 小时前
使用Python进行PDF文件的处理与操作
jvm·数据库·python
虾..3 小时前
多路复用 --- select系统调用
服务器·数据库·sql
杨云龙UP3 小时前
mysqldump逻辑备份文件恢复总结:全库恢复、单库恢复,一篇讲明白
linux·运维·服务器·数据库·mysql·adb
ybwycx3 小时前
mysql重置root密码(适用于5.7和8.0)
数据库·mysql·adb
色空大师4 小时前
【网站搭建实操(一)环境部署】
java·linux·数据库·mysql·网站搭建
亚历克斯神5 小时前
Flutter for OpenHarmony: Flutter 三方库 mutex 为鸿蒙异步任务提供可靠的临界资源互斥锁(并发安全基石)
android·数据库·安全·flutter·华为·harmonyos
IAUTOMOBILE5 小时前
用Python批量处理Excel和CSV文件
jvm·数据库·python