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

相关推荐
Hvitur3 分钟前
软考架构师【第八章】系统质量属性与架构评估
数据库·架构
遇见你...4 分钟前
B01 SpringMVC入门
数据库·sql
2601_949816168 分钟前
使用python进行PostgreSQL 数据库连接
数据库·python·postgresql
脑子加油站13 分钟前
mysql数据库常用命令
数据库
一个天蝎座 白勺 程序猿16 分钟前
KingbaseES性能优化实战:从CPU高使用率到高效运行的全路径解析
数据库·性能优化·时序数据库
Geoking.17 分钟前
MySQL COUNT 用法详解
mysql
SuperHeroWu719 分钟前
【Neo4j 】图数据库容器化部署(国内源,Linux 详细步骤)
linux·数据库·neo4j
Funky_oaNiu19 分钟前
Oracle大批量数据更新总体思路:避坑指南与关键原则
数据库·oracle
blxr_22 分钟前
Spring AI
数据库·人工智能·spring
湛生23 分钟前
django学习
数据库·后端·python·django·sqlite