下面是使用ROW_NUMBER()
做到這一點的一種方法:
SELECT OrderNo, WeavingLoom, Task, Process
FROM (
SELECT g1.OrderNo, g1.WeavingLoom, g1.Task, g1.Process,
ROW_NUMBER() OVER (ORDER BY g1.OrderNo) -
ROW_NUMBER() OVER (PARTITION BY Process ORDER BY g1.OrderNo) AS grp
FROM GanttTable AS g1
INNER JOIN (SELECT OrderNo, WeavingLoom
FROM GanttTable
WHERE Task = 99537) AS g2
ON g1.WeavingLoom = g2.WeavingLoom AND g1.OrderNo > g2.OrderNo) AS t
WHERE grp = 0 AND Process = 'ulk'
grp
字段標識具有相同Process
值的連續記錄的島嶼。第一個島,即緊接在Task -ID選擇記錄之後的記錄組,具有grp=0
。如果這部分記錄發生也具有Process = 'ulk'
,那麼它由上述查詢返回,否則不返回任何行。
Demo here
問題中提到的示例需要的輸出是什麼? –
爲什麼互聯網上已有的過多示例可以幫助您? –
對於複雜的解釋感到抱歉。在我的問題中有一個sqlfiddle鏈接。如果你看看結果,我只需要顯示順序的前兩行。 – Rapunzo