Return value at max date for a particular id

前端 未结 3 355
清歌不尽
清歌不尽 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:17

    You haven't specified your SQL implementation, but something like this should work:

    Note that the op didn't specifically ask to use max(), just to get the id, value at the max[imum] date.

    TSQL:

    select top 1 ID, Date, Value from yourtable order by Date DESC;
    

    Not TSQL, has to support limit: (Not tested.)

    select ID, Date, Value from yourtable order by Date DESC limit 1,1;
    

提交回复
热议问题