我正在开发应用程序,它将在特定日期范围之间获取报告。有可能选择多个日期选择。通常我们有频率选择,如月度,季度和年度。因此,可以选择任意数量的日期范围(按任何频率类型),没有任何重叠周期。现在在报告中我需要使用Postgresql以月为单位显示选择日期范围。这可能吗?
例如样本查询:
select dt, description
from table1
where (
dt between '2005-12-26' and '2006-03-26'
or dt between '2006-03-27' and '2006-06-25'
or dt between '2006-06-26' and '2006-12-31'
or dt between '2007-12-31' and '2008-12-28'
)
group by dt, description
order by dt, description
下面
所以现在我的报告看起来应该是
Date DateRange Description
2006-01-01 January2006-March2006 XXXXXXXXXXX
2006-08-31 July2006-Dec2006 UUUUUUUUUUU
2008-05-20 January2008-dec2008 ZZZZZZZZZZ
答案 0 :(得分:0)
我想你想要join
,就像这样:
select dt, to_char(r.date1, 'MonthYYYY') || '-' || to_char(r.date2, 'MonthYYYY') as daterange,
description
from table1 t join
(select '2005-12-26'::date as date1, '2006-03-26'::date as date2 union all
select '2006-03-27'::date, '2006-06-25'::date union all
select '2006-06-26'::date, '2006-12-31'::date union all
select '2007-12-31'::date, '2008-12-28'::date
) r
on t.dt between r.date1 and r.date2
group by t.dt, r.date1, r.date2, t.description
order by dt, description;