Get top 1 row of each group

后端 未结 20 3089
余生分开走
余生分开走 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:22

    I believe this can be done just like this. This might need some tweaking but you can just select the max from the group.

    These answers are overkill..

    SELECT
      d.DocumentID,
      MAX(d.Status),
      MAX(d1.DateCreated)
    FROM DocumentStatusLogs d, DocumentStatusLogs d1
    USING(DocumentID)
    GROUP BY d.DocumentID
    ORDER BY DateCreated DESC
    

提交回复
热议问题