1.取时间最新的记录 不分组有重复(多条CreateTime一样的都是最新记录)

select * from test t where pid in ( select PId from Test t where time=(select
max(time) from Test t1 where t1.PId=t.PId) group by Pid ) and time=(select max(
time) from Test t1 where t1.PId=t.PId)

2.分组后取时间最新的记录
SELECT max(Id)/*注意Id必须使用聚合函数Max*/ , Pid, MAX(Time) as MaxTime FROM Test GROUP
BY pid

3.如果Id是uuid类型无法使用max(id)的解决办法(使用开窗函数)
select * from ( select row_number() over(partition by [Pid] order by [Time]
desc /*降序是为了where KeyId=1 (1是固定值第一条),如果升序由于不知道每组多少条where中KeyId就无法过滤了*/ ) as
KeyId,* from Test ) d where KeyId=1

技术
今日推荐
PPT
阅读数 121
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信