Get top 1 row of each group

后端 未结 20 3039
余生分开走
余生分开走 2020-11-21 04:42

I have a table which I want to get the latest entry for each group. Here\'s the table:

DocumentStatusLogs Table

|ID| DocumentID | Status         


        
20条回答
  •  时光说笑
    2020-11-21 05:27

    This is the most vanilla TSQL I can come up with

        SELECT * FROM DocumentStatusLogs D1 JOIN
        (
          SELECT
            DocumentID,MAX(DateCreated) AS MaxDate
          FROM
            DocumentStatusLogs
          GROUP BY
            DocumentID
        ) D2
        ON
          D2.DocumentID=D1.DocumentID
        AND
          D2.MaxDate=D1.DateCreated
    

提交回复
热议问题