22,209
社区成员
发帖
与我相关
我的任务
分享
;with cte(id,姓名,科目,分数,分组排名)
as
(
select id,姓名,科目,分数,分组排名=RANK()over(partition by 科目 order by 分数 desc)
from xs
)
select id,姓名,科目,分数 from cte
where 分组排名=1
order by id
;with cte(科目,分数)
as
(
select 科目,max(分数) from xs
group by 科目
)
select a.* from xs a,cte b
where a.科目=b.科目 and a.分数=b.分数
/*
(3 行受影响)
id 姓名 科目 分数
-- ---- ---- --
7 王五 语文 94
3 张三 英语 99
8 王五 数学 88
*/
select * from xs a where not exists(select * from xs where a.分数<分数 and a.科目=科目 )
;with cte AS
(
select *,rowNum=ROW_NUMBER()over(partition by 科目 order by 分数 desc) from xs
)
select * from cte where rowNum=1