Sqlserver
 sql >> Datenbank >  >> RDS >> Sqlserver

Wie erhalte ich den ersten und den letzten Datensatz pro Gruppe in SQL Server 2008?

Wie wäre es mit ROW_NUMBER :

SQL-Fiddle

WITH Cte AS(
    SELECT *,
        RnAsc = ROW_NUMBER() OVER(PARTITION BY [group] ORDER BY val),
        RnDesc = ROW_NUMBER() OVER(PARTITION BY [group] ORDER BY val DESC)
    FROM tbl
)
SELECT
    id, [group], val, start, [end]
FROM Cte
WHERE
    RnAsc = 1 OR RnDesc = 1
ORDER BY [group], val