我有下表:
ID | Plate | RecordedAt
| |
| |
对于每个盘子,我可以有 N 条记录。
我正在处理以下查询:
SELECT TOP 1 *
FROM table
WHERE table.Plate IN ('plate1', 'plate2', 'plate3')
ORDER BY c.Recordedat DESC
问题是此查询仅返回所有列表的一条记录。
我需要列表中每个项目的前 1 个。
有没有办法只用一个查询来实现这一点?
您将使用窗口函数:
select t.*
from (select t.*,
row_number() over (partition by plate order by recordedat desc) as seqnum
from t
where table.Plate in ('plate1', 'plate2', 'plate3')
) t
where seqnum = 1;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句