Get top 1 row of each group

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

    SELECT o.*
    FROM `DocumentStatusLogs` o                   
      LEFT JOIN `DocumentStatusLogs` b                   
      ON o.DocumentID = b.DocumentID AND o.DateCreated < b.DateCreated
     WHERE b.DocumentID is NULL ;
    

    If you want to return only recent document order by DateCreated, it will return only top 1 document by DocumentID

提交回复
热议问题