Get last distinct set of records

后端 未结 5 632
既然无缘
既然无缘 2020-12-08 00:06

I have a database table containing the following columns:

id   code   value   datetime   timestamp

In this table the only unique values res

5条回答
  •  离开以前
    2020-12-08 00:41

    I'll try something like this :

    select * from table
    where id in (
        select id
        from table
        group by code
        having datetime = max(datetime)
    )
    

    (disclaimer: this is not tested)

    If the row with the bigger datetime also have the bigger id, the solution proposed by smdrager is quicker.

提交回复
热议问题