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

相关推荐
Databend11 分钟前
Databend 亮相 RustChinaConf 2025,分享基于 Rust 构建商业化数仓平台的探索
数据库
得物技术1 小时前
破解gh-ost变更导致MySQL表膨胀之谜|得物技术
数据库·后端·mysql
Java水解2 小时前
【MySQL】从零开始学习MySQL:基础与安装指南
后端·mysql
Raymond运维6 小时前
MariaDB源码编译安装(二)
运维·数据库·mariadb
沢田纲吉6 小时前
🗄️ MySQL 表操作全面指南
数据库·后端·mysql
RestCloud21 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud21 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence1 天前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
Java水解1 天前
Mysql查看执行计划、explain关键字详解(超详细)
后端·mysql
知其然亦知其所以然1 天前
MySQL 社招必考题:如何优化查询过程中的数据访问?
后端·mysql·面试