Get top 1 row of each group

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

    I know this is an old thread but the TOP 1 WITH TIES solutions is quite nice and might be helpful to some reading through the solutions.

    select top 1 with ties
       DocumentID
      ,Status
      ,DateCreated
    from DocumentStatusLogs
    order by row_number() over (partition by DocumentID order by DateCreated desc)
    

    More about the TOP clause can be found here.

提交回复
热议问题