2016-08-23 81 views
0

我有下面的查询返回12个月的滚动数据。因此,如果我今天运行它,它会将数据从2015年8月23日带回到2016年8月23日。现在理想情况下,我希望它从2015年8月1日开始,如果我要在下个月再次运行,它将从2015年9月1日开始运行。这可能吗?谢谢12个月滚动

select 

Date 
Street 
Town 
Incidents 
IncidentType A 
IncidentType B 
IncidentType C 

FROM 
(

select 

COUNT(I.INC_NUM) as Incidents, 

COUNT(case when i.INC_TYPE = ''A'' THEN 1 
    end) 
"IncidentType A" 
COUNT(case when i.INC_TYPE = ''B'' THEN 1 
    end) 
"IncidentType B" 
COUNT(case when i.INC_TYPE = ''C'' THEN 1 
    end) 
"IncidentType C" 

FROM Table i 


GROUP BY i.INC_NUM 

) i 

where Date >= (now()-('12 months'::interval)) 
+2

请与您正在使用的数据库标记您的问题。 –

回答

1

你的代码表明你正在使用Postgres。如果代码工作,你只需要调整where条款,使用date_trunc()

where Date >= date_trunc('month', now() - ('12 months'::interval)) 
+0

谢谢Gordon,这太棒了,非常感谢 – whitz11