我想从我们的netteza数据库中提取过去24个月的数据,但不包括最近两个季度(当前季度和最后一个季度)。数据库中日期字段的格式如下:YYYY
2015411 = Year=2016 Quarter=4 month= 11
2013108 = Year=2013 Quarter=1 month= 8预计时间框架应为2014年10月至2016年9月的数据。我们使用的是历年一月到三月是Q1,四月到六月是Q2等。这是我正在使用的查询,但它提取了过去27个月的一切,但我只想要过去24个月,但不包括最近的两个季度。
select * from myTable where substring (month_key,1,4) || substring (month_key, 6,7) || ''01'' > CURRENT_DATE - INTERVAL ''27 months'' 发布于 2017-03-23 22:35:48
您可以使用内置的季度评估。
下面的查询突出显示所需的值。
select add_months(to_date(to_char(to_date(to_char(current_date,'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ'),-24) as "24 months prior to 2 quarters ago"
,to_date(to_char(to_date(to_char(current_date,'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ') "2 quarters ago"在你的例子中就是这样的:
select * from myTable
where date_value between select add_months(to_date(to_char(to_date(to_char(current_date,'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ'),-24)
and to_date(to_char(to_date(to_char(current_date,'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ')https://stackoverflow.com/questions/42984447
复制相似问题