根据过去的月份和年份获取过去3个月的数据

时间:2015-01-23 07:47:42

标签: postgresql datetime

我有一个psql查询,它必须返回过去一个月的数据和前三个月的数据。如果我把月份作为2014年的12月份和年份,我应该得到9月,10月,11月和12月的数据。如果前一个月与过去的年份相同,则查询工作正常。但如果我将月份作为2015年的1月份和年份通过,那么我的输出结果不会得到任何结果。

请检查下面提到的查询,并告诉我相应的更改:

SELECT(SUM(pendingesla)/nullif(SUM(pendingesla+completedwsla+completedesla+pendingwsla),0))*100 AS "Inprogress",
      (SUM(completedesla)/nullif(SUM(pendingesla+completedwsla+completedesla+pendingwsla),0))*100 AS "Completed > sla",
      (SUM(completedwsla)/nullif(SUM(pendingesla+completedwsla+completedesla+pendingwsla),0))*100 AS "Completed < SLA",
      (SUM(pendingwsla)/nullif(SUM(pendingesla+completedwsla+completedesla+pendingwsla),0))*100 AS "Pending < SLA",
      to_char(to_timestamp(to_char(EXTRACT (MONTH FROM creation_dt), '999'), 'MM'), 'Month') AS MONTH,
      CAST(EXTRACT (MONTH FROM creation_dt) AS INT) AS MONTHID,
      CAST(EXTRACT (YEAR FROM creation_dt) AS INT) AS YEAR
FROM   cs_dm.v_agg_cs_level1  CSF
WHERE EXTRACT (MONTH FROM creation_dt) >= EXTRACT(month from to_timestamp(to_char(1, '999'), 'MM')- INTERVAL'3 month')
AND EXTRACT (MONTH FROM creation_dt) <= 1
AND EXTRACT(YEAR FROM creation_dt)= 2015
AND zone_id=4001
GROUP BY MONTH,YEAR,MONTHID
ORDER BY MONTHID;

快速及时的回复将不胜感激。非常需要这个结果。

0 个答案:

没有答案