在 InterSys Cache DB 中获取最近 3 个月(T、T-1 和 T-2)

Get 3 most recent months (T, T-1, and T-2) in InterSys Cache DB

我如何获得最近三个月的数据? 我尝试了如下所示的方法只是为了得到 T -1,但它给了我错误的结果:-

select  to_date(CAST(MONTH(DATEADD(month, - 1, GETDATE())) as date),'YYYY/MM/DD')

结果:-

02/01/1841

您最好在 https://community.intersystems.com/

上询问 Intersystems 开发人员社区

然而,以下将 return 当前月份的第一个日期,然后是前 3 个月的第一个和最后一个日期。我不是 SQL 专家,所以可能会有更简单的方法!

select CAST(DATEADD('d' , 1-{fn DAYOFMONTH(CURRENT_DATE)}, CURRENT_DATE) AS DATE) as FirstDateThisMonth
,CAST(DATEADD('d', -{fn DAYOFMONTH(CURRENT_DATE)}, CURRENT_DATE) AS DATE) as LastDateLastMonth
,CAST(DATEADD('m', -1,DATEADD('d' , 1-{fn DAYOFMONTH(CURRENT_DATE)}, CURRENT_DATE)) AS DATE) as FirstDateLastMonth
,CAST(DATEADD('d', -1,DATEADD('m',-1,DATEADD('d' , 1-{fn DAYOFMONTH(CURRENT_DATE)}, CURRENT_DATE))) AS DATE) as LastDateTwoMonthsAgo
,CAST(DATEADD('m', -2,DATEADD('d' , 1-{fn DAYOFMONTH(CURRENT_DATE)}, CURRENT_DATE)) AS DATE) as FirstDateTwoMonthsAgo
,CAST(DATEADD('d', -1,DATEADD('m',-2,DATEADD('d' , 1-{fn DAYOFMONTH(CURRENT_DATE)}, CURRENT_DATE))) AS DATE) as LastDateThreeMonthsAgo
,CAST(DATEADD('m', -3,DATEADD('d' , 1-{fn DAYOFMONTH(CURRENT_DATE)}, CURRENT_DATE)) AS DATE) as FirstDateThreeMonthsAgo