Multiple max values in a query

前端 未结 6 1912
余生分开走
余生分开走 2021-01-12 21:08

I know the title does not sound very descriptive, but it is the best I could think of:

I have this table

ID     BDATE      VALUE
28911  14/4/2009  44820
2         


        
6条回答
  •  太阳男子
    2021-01-12 21:34

    You can use an INNER JOIN to filter out only the maximum rows:

    select t.*
    from YourTable t
    inner join (
         select id, max(bdate) as maxbdate
         from YourTable
         group by id
    ) filter
        on t.id = filter.id
        and t.bdate = filter.maxbdate
    

    This prints:

    id     bdate       value
    38605  2009-04-23  6936575
    38537  2009-04-22  81098692
    28911  2009-04-24  7749594.67
    

    Note that this will return multiple rows for an id which has multiple values with the same bdate.

提交回复
热议问题