我正在尝试在 SnowSQL 中创建 SET 变量,这些变量将分别指定当前年份和前几年的每个开始月份和结束月份的第一个。我希望代码能够随着时间的推移滚动到新的月份。例如,由于现在是 2020 年 6 月,我们将有 m2_start = 2020-06-01 和 m2_end = 2020-07-01。m1_start = 2019-06-01 和 m1_end 2019-07-01。这是我到目前为止所拥有的,但我想要的并不安静,因为我不知道如何在月初指定:
SET m1_start = (SELECT DISTINCT DATEADD(year,-1,CURRENT_DATE) FROM DIMDATE d WHERE DAY(d."Date") = 1);
SET m1_end = (SELECT DISTINCT DATEADD(month, -11,CURRENT_DATE) FROM DIMDATE d WHERE DAY(d."Date") = 1);
SET m2_start = (SELECT d."Date" FROM DIMDATE d WHERE DAY(d."Date") = 1 AND MONTH(d."Date") = MONTH(CURRENT_DATE) AND YEAR(d."Date") = YEAR(当前的日期));
SET m2_end = (SELECT DISTINCT DATEADD(month, 1,CURRENT_DATE) FROM DIMDATE d WHERE DAY(d."Date") = 1);
SET 测试 = '2020-06-01';