小编Mr.*_*ato的帖子

从 generate_series() 填写记录集中缺少的日期

考虑:

with days as (select day::date
from generate_series(date '2013-01-01', date '2013-01-01' + 365, interval '1 day' day) day
) 
select 'Inspections'::text as data_label,
count(i.reporting_id) as daily_count, d.day as date_column
from days d
left join inspection i on i.close_case_date = d.day
group by d.day
order by d.day
Run Code Online (Sandbox Code Playgroud)

这将返回一个如下所示的集合:

data_label | daily_count | date_column
Inspections    1           01/01/13
Inspections    2           01/02/13
Inspections    4           01/04/13
Inspections    8           01/06/13
Run Code Online (Sandbox Code Playgroud)

请注意记录集中的 1 天和 2 天间隔。我需要生成一个用 0 填充的值的集合,如下所示:

data_label | daily_count | date_column
Inspections    1           01/01/13
Inspections …
Run Code Online (Sandbox Code Playgroud)

postgresql aggregate postgresql-9.1 set-returning-functions

9
推荐指数
1
解决办法
1万
查看次数