我无法使用自定义周期表计算滚动季度:
时期 |
---|
2017-06-01 |
2020-12-01 |
2020-11-01 |
滚动月份和年份正在工作:
DATEADD(MONTH, -2, "Period") AS "Period M-2"
DATEADD(YEAR, -1, "Period") AS "Period Y-1"
当我使用current_date()查询宿舍时,这是有效的:
SELECT DATE_TRUNC('MONTH',DATEADD(QUARTER, -1, current_date()))
当我查询我的表时
DATEADD(QUARTER, -1, "Period") AS "Period Q-1"
我有以下结果(似乎计算 M-3 而不是宿舍):
时期 | 期间 Q-1 |
---|---|
2017-08-01 | 2017-05-01 |
2017-09-01 | 2017-06-01 |
2017-10-01 | 2017-07-01 |
要重现该问题:
WITH input AS (select $1 "Period" from values
('2017-08-01'),
('2017-09-01'),
('2017-10-01'),
('2017-05-01'),
('2020-11-01'),
('2020-11-01'),
('2017-04-01'),
('2020-10-01'),
('2020-10-01')
)
SELECT "Period",
DATE(DATEADD(MONTH, -1, "Period" )) AS "Period M-1",
DATE(DATEADD(MONTH, -2, "Period" )) AS "Period M-2",
DATE(DATEADD(QUARTER, -1, "Period" )) AS "Period Q-1",
DATE(DATEADD(QUARTER, -2, "Period" )) AS "Period Q-2",
DATE(DATEADD(YEAR, -1, "Period" )) AS "Period Y-1",
DATE(DATEADD(YEAR, -2, "Period" )) AS "Period Y-2"
FROM input
更新:这是使用这个公式,谢谢你的解释@Francesco。
DATE_TRUNC('MONTH',DATEADD('DAY', -1, DATE_TRUNC('QUARTER', "Period"))) AS "Period Q1"
但是第二季度和第三季度仍然无法正常工作。