基于前一天的指数周末

Index weekends based on previous day

我有一个查询,我的 select 语句如下所示:

SELECT 
    CAST(p.date AS DATE) AS 'Date', 
    x.Month,
    x.Version,
    x.Value AS 'fcst',
     CASE WHEN isholiday = 0 
     THEN ROW_NUMBER() OVER (PARTITION BY x.Department, x.Month, p.isholiday ORDER BY p.date)
     ELSE 0
     END AS 'Index'

输出如下所示:

 Date       Month   Version fcst    isholiday   Index
2020-01-01  January 3plus9  3679    1            0
2020-01-02  January 3plus9  3679    0            1
2020-01-03  January 3plus9  3679    0            2
2020-01-04  January 3plus9  3679    1            0
2020-01-05  January 3plus9  3679    1            0
2020-01-06  January 3plus9  3679    0            3
2020-01-07  January 3plus9  3679    0            4
2020-01-08  January 3plus9  3679    0            5
2020-01-09  January 3plus9  3679    0            6
2020-01-10  January 3plus9  3679    0            7
2020-01-11  January 3plus9  3679    1            0
2020-01-12  January 3plus9  3679    1            0
2020-01-13  January 3plus9  3679    0            8
2020-01-14  January 3plus9  3679    0            9
2020-01-15  January 3plus9  3679    0            10
2020-01-16  January 3plus9  3679    0            11
2020-01-17  January 3plus9  3679    0            12
2020-01-18  January 3plus9  3679    1            0
2020-01-19  January 3plus9  3679    1            0
2020-01-20  January 3plus9  3679    0            13

索引列基于 'isHoliday' 列。在 isHoliday = 1 的每个点,Index 都显示为 0,如 case 语句所示。相反,如果第 <>1 天,我需要将索引值显示为其上方索引的值。

例如,在日期 = 1 月 4 日和 1 月 5 日的行中,索引需要显示为 2。 我已尝试更改案例说明,但一直无法找到解决方案。

我想你想要 holiday = 0:

的累计计数
select sum(case when isholiday = 0 then 1 else 0 end) over (partition by department, month order by date) as my_index

如果holiday只取两个值,你可以简化为:

select sum(1 - isholiday) over (partition by department, month order by date) as my_index