2013-04-28 98 views
3

從下面的表格可以看出,如果Project.Date組有失敗和成功,我想保留失敗行,但是如果單行(與其他行一樣),那麼不管狀態如何,保留該行。例如,我想保留第一行,放棄第二行,並將其餘的放在下面的表格上。從組中選擇具有特定值的行?

╔═════════╦══════════╦═════════╗ 
║ PROJECT ║ DATE ║ STATUS ║ 
╠═════════╬══════════╬═════════╣ 
║ HLM  ║ 20130422 ║ Fail ║ 
║ HLM  ║ 20130422 ║ Success ║ 
║ HLM  ║ 20130423 ║ Fail ║ 
║ HLM  ║ 20130424 ║ Success ║ 
║ HLM  ║ 20130425 ║ Fail ║ 
║ HLM  ║ 20130426 ║ Success ║ 
╚═════════╩══════════╩═════════╝ 

回答

2
WITH records 
AS 
(
    SELECT [Project], [Date], [Status], 
      ROW_NUMBER() OVER (PARTITION BY [Project], [Date] 
           ORDER BY [Status]) rn 
    FROM TableName 
) 
SELECT [Project], [Date], [Status] 
FROM records 
WHERE rn = 1 

輸出

╔═════════╦══════════╦═════════╗ 
║ PROJECT ║ DATE ║ STATUS ║ 
╠═════════╬══════════╬═════════╣ 
║ HLM  ║ 20130422 ║ Fail ║ 
║ HLM  ║ 20130423 ║ Fail ║ 
║ HLM  ║ 20130424 ║ Success ║ 
║ HLM  ║ 20130425 ║ Fail ║ 
║ HLM  ║ 20130426 ║ Success ║ 
╚═════════╩══════════╩═════════╝ 
+0

美麗!謝謝。 – user1013388 2013-04-28 16:45:40

+0

不客氣':D' – 2013-04-28 16:46:26

相關問題