如何编写SQL查询来读取最近的时间戳(使用窗口函数)?

问题描述 投票:0回答:1

我的数据库中有一个表,其中插入了行而没有更新旧行。这会导致记录具有相同的 ID 但不同的时间戳。如何编写使用窗口函数读取具有不同 ID 和最新时间戳的行的 SQL 查询?

sql window-functions
1个回答
0
投票
with base as (
select *, row_number() over(partition by id order by timestamp desc) as rn from table
)
select * except(rn) from base
where rn = 1
© www.soinside.com 2019 - 2024. All rights reserved.