2011-02-03 112 views
0
Code  Description  Whatever 
--------------------------------- 
1  stuff   blah 
1  something  meh 
2  yah    bong 
2  never   hammer time 

如何獲取結果從這組與各Code只出現一次? (我不太在乎那個代碼的記錄)。取得每個標識符組第一個記錄SQL

所以我想....

1  stuff   blah 
2  yah    bong 

回答

0
SELECT * 
FROM (
SELECT * , row_number() over(partition by code order by Description) as id 
from yourTable 
) temp 
WHERE id = 1 

我覺得這是SQL服務器只

0

首先,您需要選擇哪些決定作爲「第一個結果制」最重要的列。在我的例子中,我選擇了Description

SELECT * FROM YourTable first 
WHERE 
    (SELECT COUNT(*) FROM YourTable previous 
    WHERE previous.Code=first.Code AND previous.Description < first.Description) = 0 
相關問題