日期:2014-05-17 浏览次数:20590 次
select StudentName '学生',
GroupName '所在小组',
Score '所得成绩'
from
(select StudentName,GroupName,Score,
rank() over(partition by GroupName order by Score desc) 'rn'
from tScores) t
where rn=1
select tabAAA.[GroupName],tabAAA.组最高分,tScores.[StudentName]
(
select distinct [GroupName],(select max([Score]) from tScores where [GroupName]=tbA.[GroupName]) as 组最高分 from tScores tbA)tabAAA
left join tScores on tabAAA.[GroupName]=tScores.[GroupName] and tabAAA.[Score]=tScores.[Score]
;with cte(StudentName,GroupName,Score) as
(
select '张一','一组','90' union all
select '张二','一组','58' union all
select '张三','一组','90' union all
select '李一','二组','78' union all
select '李二','二组','97' union all
select '李三','二组','45' union all
select '王一','三组','78' union all
select '王二','三组','98' union all
select '王三','三组','33'
)
select StudentName,GroupName,Score from (
select DENSE_RANK() over(partition by GroupName order by Score desc)id,* from cte
)a where id=1
order by GroupName desc
--------------------------
StudentName GroupName Score
----------- --------- -----
张三 一组 90
张一 一组 90
王二 三组 98
李二 二组 97
(4 行受影响)
create table cte(
StudentName varchar(10),GroupName varchar(10),Score int
)
insert in