简体   繁体   English

如何只选择最近的

[英]How to select only the most recent

Table A has ID and date and name.表 A 有 ID、日期和名称。 Each time the record is changed the first 11 digits of the Id remain the same but the final digit would increase by 1. For example每次更改记录时,Id 的前 11 位数字保持不变,但最后一位数字会增加 1。例如

123456789110 01-01-2020 John smith
119876543210 01-01-2020 Peter Griffin
119876543211 05-01-2020 Peter Griffin

How could I write a statement that shows The iD associated with John smith as well as the most recent Id of Peter Griffin?我怎么能写一个声明,显示与约翰史密斯相关的 iD 以及彼得格里芬的最新 ID? Thanks谢谢

Yet another option is using WITH TIES另一种选择是使用WITH TIES

Select top 1 with ties *
 From  YourTable
 Order by row_number() over (partition by left(id,11) order by date desc)

Why not just use max() ?为什么不直接使用max()

select name, max(id)
from t
group by name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM