Return value at max date for a particular id

前端 未结 3 353
清歌不尽
清歌不尽 2021-01-03 02:54

Here\'s my sql server table

ID       Date       Value 
___      ____       _____
3241     9/17/12    5
3241     9/16/12    100
3241     9/15/12    20
4355           


        
3条回答
  •  醉梦人生
    2021-01-03 03:14

    You can use the following:

    select t1.id, t2.mxdate, t1.value
    from yourtable t1
    inner join
    (
      select max(date) mxdate, id
      from yourtable
      group by id
    ) t2
      on t1.id = t2.id
      and t1.date = t2.mxdate
    

    See Demo

提交回复
热议问题