【发布时间】:2020-08-03 09:56:57
【问题描述】:
我有一个查询,我的选择语句如下所示:
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。 我已尝试更改案例陈述,但未能找到解决方案。
【问题讨论】:
-
我目前显示索引的方式仅基于工作日。在非工作日,我需要保持与前一个工作日相同的值。
标签: sql sql-server partition