Get top 1 row of each group

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

    In scenarios where you want to avoid using row_count(), you can also use a left join:

    select ds.DocumentID, ds.Status, ds.DateCreated 
    from DocumentStatusLogs ds
    left join DocumentStatusLogs filter 
        ON ds.DocumentID = filter.DocumentID
        -- Match any row that has another row that was created after it.
        AND ds.DateCreated < filter.DateCreated
    -- then filter out any rows that matched 
    where filter.DocumentID is null 
    

    For the example schema, you could also use a "not in subquery", which generally compiles to the same output as the left join:

    select ds.DocumentID, ds.Status, ds.DateCreated 
    from DocumentStatusLogs ds
    WHERE ds.ID NOT IN (
        SELECT filter.ID 
        FROM DocumentStatusLogs filter
        WHERE ds.DocumentID = filter.DocumentID
            AND ds.DateCreated < filter.DateCreated)
    

    Note, the subquery pattern wouldn't work if the table didn't have at least one single-column unique key/constraint/index, in this case the primary key "Id".

    Both of these queries tend to be more "expensive" than the row_count() query (as measured by Query Analyzer). However, you might encounter scenarios where they return results faster or enable other optimizations.

提交回复
热议问题