我有一个这样的表:
WOTranID WOID Status DateCreated
----------------------------------------------
1 5 Ready 6/6/2015
2 5 Pending 6/5/2015
3 7 Pending 6/9/2015
4 8 Scheduled 6/10/2015
我需要的是选择状态为待处理但当前不是待处理的所有 WOID。
感谢您的帮助。
编辑:使用上面的示例表,我只想返回 WOID 5。
最佳答案
首先,选择所有状态为待处理的 WOID,然后将其与当前状态非待处理的 WOID 列表相交(使用 row_number()
选择每个 WOID 的最新状态) 。
(Select Distinct WOID
from MyTable
where Status = 'Pending')
intersect
(Select WOID
from
(Select WOID
, Status
, Row_number() over (partition by WOID order by DateCreated desc) as RN
from MyTable) a
where Status <> 'Pending' and RN = 1)
关于SQL Server : select where row was X but is not now X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30898812/