2014-02-07 53 views
0

我有這樣一個表的最大計數......設置重複

Name 
------------ 
George 
George  
George 
George 
John 
John 
John 
Paul 
Paul 
Arnold 
. 

,我需要重複的最大數量的結果。 Forexaple最多2個重複。

Name 
------------ 
George 
George 
John 
John 
Paul 
Arnold 
. 

我該如何編寫腳本? 感謝

回答

1

您可以使用ROW_NUMBER,例如:

WITH CTE AS 
(
    SELECT RN = ROW_NUMBER() OVER (PARTITION BY Name ORDER BY Name) 
     , Name 
    FROM dbo.Names 
) 
SELECT Name FROM CTE WHERE RN <= 2 

Demo