group by分组问题

frankl123 2014-12-25 11:50:29

with cte as
(
select 1 id,1 personid,2 EducationOrder
union all
select 2 id,1 personid,1 EducationOrder
union all
select 3 id,2 personid,1 EducationOrder
union all
select 4 id,3 personid,1 EducationOrder
)
select * from cte
/*
id personid EducationOrder
----------- ----------- --------------
1 1 2
2 1 1
3 2 1
4 3 1
*/
按照personid分组 取EducationOrder大的行的id值
我想要的结果是
/*
id personid EducationOrder
----------- ----------- --------------
1 1 2
3 2 1
4 3 1
*/
...全文
77 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
还在加载中灬 2014-12-25
  • 打赏
  • 举报
回复
select * from cte T1
WHERE NOT EXISTS
	(SELECT 1 FROM cte T2 WHERE T1.personid=T2.personid AND T2.EducationOrder>T1.EducationOrder)

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧