這是我最初帖子的后續問題
示例情況:訂單系統通過記錄始終唯一的系統日志日期來跟蹤手動輸入的到期日期(這將是一個日期時間,但為了簡單起見,我使用了日期,使每個日期都是唯一的)。
我想為每個截止日期分組分配一個節號,其中截止日期按時間順序保持相同。
Stu 的回復解決了我最初帖子中的表格,但我注意到,如果我將 2022 年 1 月 16 日與 SysLogDate 關聯的 2022 年 4 月 15 日到期日期替換為 2022 年 4 月 13 日,所需的排序似乎不待維護:
注意:4/13/2022 日期是任意更改。如果我使用尚未在 DueDate 列中的任何其他唯一日期,則會出現同樣的問題。最終,我還需要能夠處理對 NULL 的更改/從 NULL 更改,其中有人“忘記”輸入日期,但用 NULL 替換日期也會產生相同的問題。
更新表:
CREATE TABLE #DueDates (OrderNo INT, DueDate Date, SysLogDate Date)
INSERT INTO #DueDates Values (1, '4/10/2022', '1/10/2022')
,(1, '4/10/2022', '1/11/2022')
,(1, '4/15/2022', '1/15/2022')
,(1, '4/13/2022', '1/16/2022') -- Due Date Altered since prior post
,(1, '4/15/2022', '1/17/2022')
,(1, '4/10/2022', '1/18/2022')
,(1, '4/10/2022', '1/19/2022')
,(1, '4/10/2022', '1/20/2022')
,(2, '4/10/2022', '2/16/2022')
,(2, '4/10/2022', '2/17/2022')
,(2, '4/15/2022', '2/18/2022')
,(2, '4/15/2022', '2/20/2022')
,(2, '4/15/2022', '2/21/2022')
,(2, '4/10/2022', '2/22/2022')
,(2, '4/10/2022', '2/24/2022')
,(2, '4/10/2022', '2/26/2022')
期望的結果是:
OrderNo DueDate SysLogDate SectionNumber_WithinDueDate
1 2022-04-10 2022-01-10 1
1 2022-04-10 2022-01-11 1
1 2022-04-15 2022-01-15 2
1 2022-04-13 2022-01-16 3
1 2022-04-15 2022-01-17 4
1 2022-04-10 2022-01-18 5
1 2022-04-10 2022-01-19 5
1 2022-04-10 2022-01-20 5
2 2022-04-10 2022-02-16 1
2 2022-04-10 2022-02-17 1
2 2022-04-15 2022-02-18 2
2 2022-04-15 2022-02-20 2
2 2022-04-15 2022-02-21 2
2 2022-04-10 2022-02-22 3
2 2022-04-10 2022-02-24 3
2 2022-04-10 2022-02-26 3
...但是將我之前帖子中的解決方案應用于此更新的表格會產生:
OrderNo DueDate SysLogDate SectionNumber_WithinDueDate
1 2022-04-10 2022-01-10 1
1 2022-04-10 2022-01-11 1
1 2022-04-15 2022-01-15 2
1 2022-04-13 2022-01-16 3 **
1 2022-04-15 2022-01-17 3 **
1 2022-04-10 2022-01-18 3 **
1 2022-04-10 2022-01-19 3 **
1 2022-04-10 2022-01-20 3 **
2 2022-04-10 2022-02-16 1
2 2022-04-10 2022-02-17 1
2 2022-04-15 2022-02-18 2
2 2022-04-15 2022-02-20 2
2 2022-04-15 2022-02-21 2
2 2022-04-10 2022-02-22 3
2 2022-04-10 2022-02-24 3
2 2022-04-10 2022-02-26 3
這是一個使用上述更新表和我之前帖子中的解決方案的演示,并顯示了上述非預期結果: Fiddle
演示顯示當日期替換為 NULL 時的相同效果:Fiddle with NULL
從我之前的帖子中選擇的解決方案的副本(在上面的小提琴中使用):
select OrderNo, DueDate, SysLogDate,
dense_rank() over(partition by orderno order by gp) SectionNumber_WithinDueDate
from (
select *,
Row_Number() over(partition by OrderNo order by SysLogDate)
- Row_Number() over(partition by OrderNo, DueDate order by SysLogDate) gp
from #DueDates
)t
order by OrderNo, SysLogDate;
這是資料中的一個小變化,但我無法弄清楚如何更改子查詢中的“Row_Number 差異行”以獲得所需的結果。
感謝您在這里提供的任何建議:)
uj5u.com熱心網友回復:
對于間隙和孤島問題,我更喜歡使用lag()視窗函式,因為它更容易理解。
用于lag()比較前一行的值,并在更改時設定一個標志(值 1)。在標志上執行累積和,你得到grp. 使用dense_rank()它grp,它會給你你的SectionNumber_WithinDueDate
由于您有NULL值,請使用ISNULL()回傳日期值 ( 99991231) 進行比較
select OrderNo, DueDate, SysLogDate,
SectionNumber_WithinDueDate = dense_rank() over (partition by OrderNo
order by grp)
from
(
select *, grp = sum(g) over (partition by OrderNo
order by SysLogDate)
from
(
select *,
g = case when isnull(DueDate, '99991231')
<> isnull(lag(DueDate) over (partition by OrderNo
order by SysLogDate), '99991231')
then 1
else 0
end
from #DueDates
) d
) d
order by OrderNo, SysLogDate;
擺弄您的示例資料:
小提琴 1
小提琴 2
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/531688.html
