我试图找出如何从这张表中提取数据:
表:状态
OrderNo Status Date User 1 1 2011-07-01 user1 1 2 2011-07-05 user1 2 1 2011-07-01 user1 2 2 2011-07-06 user2 2 3 2011-07-10 user1
我需要产生以下结果:
OrderNo Status Date User 1 2 2011-07-05 user1 2 3 2011-07-10 user1
实际上,我需要的是显示OrderNo的DISTINCT列表及其最后状态。
感谢您提供所有可能的帮助。
干杯!
答案 0 :(得分:2)
WITH S AS
(
SELECT OrderNo, Status, Date, User
, ROW_NUMBER() OVER
(
PARTITION BY OrderNo
ORDER BY Date DESC
) Ranking
FROM Status
)
SELECT OrderNo, Status, Date, User
FROM S
WHERE Ranking = 1
答案 1 :(得分:0)
select OrderNo, max(Status) as 'Status'
from Status
group by OrderNo