1.需求业务要查询某个计划表根据年度来统计每个月的计划日期的数量和完成的数量
如下写法一:
select
to_char(year_month,'YYYY-MM') as month_date,
(select count(1) as p_count from eqms.instrument_calibration_plan as p WHERE to_char( p.plan_date::DATE,'YYYY-MM')=to_char(year_month,'YYYY-MM')) as plan_count,
(select count(1) from eqms.instrument_calibration_plan as a WHERE confirm_flag=1 and to_char(a.modify_date::DATE,'YYYY-MM')=to_char(year_month, 'YYYY-MM')) as complete_count
from
(select generate_series(cast('2023-01-01' as date),cast('2023-12-01' as date),'1 month') year_month) as y
①使用(select generate_series(cast(‘2023-01-01’ as date),cast(‘2023-12-01’ as date),‘1 month’) year_month)计算出开始时间和结束时间范畴内的所有月份year_month
②在查询列上代入查出的每个月份日期查询对应的月份的计划日期的计划数量和完成的计划数量
③
to_char(代入要转text的属性,要转的格式)
cast(属性值 as 强转的类型格式) 自动抓换数据类型
generate_series(开始分割日期,最后分割日期,分割的值大小)