You can use a common table expression which applies a row number to each subsequent row with the same ID
/S_DATE
values, then filter on that row number.
;WITH src AS
(
SELECT ID, S_DATE, NR, rn = ROW_NUMBER() OVER
(PARTITION BY ID, S_DATE ORDER BY NR)
FROM dbo.TABLE_1
)
SELECT ID, S_DATE, NR
FROM src
WHERE rn = 1;
You could also do this, which is more straightforward, but it is much more complicated to do when you have many columns to group by (or if you want more complicated logic for which NR
to include):
SELECT ID, S_DATE, NR = MIN(NR)
FROM dbo.TABLE_1
GROUP BY ID, S_DATE;
Fiddle
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…