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

相关推荐
安当加密几秒前
SQL Server 数据库安全新范式:TDE 透明加密+ DBG数据库安全网关 双重装甲
数据库·oracle
java干货16 分钟前
如果光缆被挖断导致 Redis 出现两个 Master,怎么防止数据丢失?
数据库·redis·缓存
2401_8371638932 分钟前
CSS如何实现网页打印样式优化_利用@media print重写布局
jvm·数据库·python
Irene199132 分钟前
Oracle 21c XE 安装后默认不包含HR等示例表,CO 模式、SCOTT 模式安装过程记录
数据库·oracle
李白客1 小时前
能源系统数据库:面向智能电网与新能源场景的五大核心能力
数据库·能源
观北海1 小时前
机器人调度系统死锁卡死全复盘及解决方案
数据库·机器人
DolphinDB智臾科技1 小时前
高频行情低频化因子库:让 Tick 级数据为中低频策略所用
数据库·金融
爱莉希雅&&&1 小时前
Ansible+Docker案例(含ansible配置安装docker)
linux·运维·mysql·nginx·docker·容器·ansible
oradh1 小时前
Oracle数据库序列和同义词概述
数据库·oracle·数据库基础·数据库入门·oracle序列·oracle同义词
treesforest1 小时前
Ipdatacloud IP 地址查询方案适合哪些场景?
大数据·网络·数据库·网络协议·tcp/ip·ip