如何 select 仅 SQL 中的最大时间戳行?

How to select just the max timestamp rows in SQL?

所以我有一些数据如下:

USER_ID                 TIMESTAMP   data  data2
   0001   2021-05-09 12:13:03.445    tim     44
   0002   2021-05-09 13:13:03.445    rob    543
   0002   2021-05-09 11:13:03.445   jeff    252
   0003   2021-05-09 09:13:03.445  perry    333
   0002   2021-05-09 12:13:03.445   carl    333
   0003   2021-05-09 16:13:03.445  mitch      1

现在我通常写下面的代码来获取最小时间戳记录:

WITH form_raw AS (
    select df.*,
        rank() over (
            partition by user_ID
            order by timestamp
        ) as num
    FROM df
)
    select *
    from form_raw
    where num = 1
)

但是我该如何为最大时间戳执行此操作?我可以使用 num = 1 轻松做到这一点,但是因为每个 user_id 的条目数量不同,我该怎么做才能使其成为最大时间戳?

最终输出:

USER_ID                 TIMESTAMP   data  data2
   0001   2021-05-09 12:13:03.445    tim     44
   0002   2021-05-09 13:13:03.445    rob    543
   0003   2021-05-09 16:13:03.445  mitch      1

按描述排序:

select * from 
(  
  select *,rank() over (partition by user_ID order by timestamp desc) as num
  FROM df
) t
where num = 1