Get top 1 row of each group

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

    My code to select top 1 from each group

    select a.* from #DocumentStatusLogs a where 
     datecreated in( select top 1 datecreated from #DocumentStatusLogs b
    where 
    a.documentid = b.documentid
    order by datecreated desc
    )
    

提交回复
热议问题