get top 1 row of each group

Solutions on MaxInterview for get top 1 row of each group by the best coders in the world

showing results for - "get top 1 row of each group"
Emil
30 Jan 2017
1;WITH cte AS
2(
3   SELECT *,
4         ROW_NUMBER() OVER (PARTITION BY DocumentID ORDER BY DateCreated DESC) AS rn
5   FROM DocumentStatusLogs
6)
7SELECT *
8FROM cte
9WHERE rn = 1
10
Tom
06 May 2017
1|ID| DocumentID | Status | DateCreated |
2| 2| 1          | S1     | 7/29/2011   |
3| 3| 1          | S2     | 7/30/2011   |
4| 6| 1          | S1     | 8/02/2011   |
5| 1| 2          | S1     | 7/28/2011   |
6| 4| 2          | S2     | 7/30/2011   |
7| 5| 2          | S3     | 8/01/2011   |
8| 6| 3          | S1     | 8/02/2011   |
9
10
11;WITH cte AS
12(
13   SELECT *,
14         ROW_NUMBER() OVER (PARTITION BY DocumentID ORDER BY DateCreated DESC) AS rn
15   FROM DocumentStatusLogs
16)
17SELECT *
18FROM cte
19WHERE rn = 1
20
21
22