我需要知道具有相同值的连续行
我需要prsstatus
值为 0 的行,然后下一行是 1
假设您的意思是“下一个”行的密集值TblID
:
select YTN.*
from YourTableName as YTN inner join
YourTableName as NR on NR.TblID = YTN.TblId + 1 and
NR.PrsStatus = 1 and YTN.PrsStatus = 0
另一种方法,将 MyTable 更改为您的表,并将ORDER BY
子句更改为您想要排序的任何列。
;WITH MyTableSort AS (
SELECT TblID, EmpID, PrsDay, PrsTime, PrsStatus, ROW_NUMBER() OVER (ORDER BY MyTable.TblID) [Row] FROM MyTable
)
SELECT * FROM MyTableSort AS MyTableSortA
WHERE (MyTableSortA.PrsStatus = 0 AND EXISTS (SELECT 1 FROM MyTableSort WHERE MyTableSort.[Row] = MyTableSortA.[row]+1 AND MyTableSort.PrsStatus = 1))