答案 0 :(得分:3)
WITH CTE AS(
SELECT T.*, RN=ROW_NUMBER()OVER(PARTITION BY Col1 Order By Col2 ASC)
FROM dbo.TableName T
)
SELECT * FROM CTE WHERE RN <= 4
答案 1 :(得分:1)
SELECT
ID, SomeVal
FROM (
SELECT ID, SomeVal, row_number() over(PARTITION BY id ORDER BY SomeVal ASC) rn
FROM [TableName]
) T
WHERE rn <= 4
这为您提供了每SomeVal
个最低的前4个ID
值。