如何获得每个分支的第n个最高分?

时间:2015-10-29 07:42:02

标签: sql sql-server

我有一个表格,学生的字段为:Student_id, Student_Name, Mark, Branch

我希望在单个查询中获得每个分支的第n个最高标记和名称。有可能吗?

例如,如果数据是

S1   |  Amir  |  EC  |  121
S2   |  Ewe   |  EC  |  123
S3   |  Haye  |  EC  |  45
S4   |  Mark  |  EC  |  145
S5   |  Tom   |  CS  |  152
S6   |  Hudd  |  CS  |  218
S7   |  Ken   |  CS  |  48
S8   |  Ben   |  CS  |  15
S9   |  Wode  |  CS  |  123
S10  |  Kayle |  IT  |  125
S11  |  Den   |  IT  |  120
S12  |  Noy   |  IT  |  126
S13  |  Jon   |  GS  |  150

我选择在每个分支中显示第三个最高标记,输出应该是

S1   | Amir   | EC   | 121
S9   | Wode   | CS   | 123
S11  | Den    | IT   | 120
S13  |  Jon   |  GS  |  150

我尝试了以下解决方案:

select Student_id,Student_Name,Mark,Branch from (
    select  Student_id,Student_Name,Mark,Branch,dense_rank() over (partition by Branch order by Mark desc) rnk
    from Student ) where rnk=3;

但结果是:

 S1   | Amir   | EC   | 121
 S9   | Wode   | CS   | 123
 S11  | Den    | IT   | 120

因为分支' GS'只有一个条目。 我怎样才能得到我想要的结果

2 个答案:

答案 0 :(得分:1)

使用CTEUNION

--DECLARE TABLE
declare @temp table
(Student_id nvarchar(55), Student_Name nvarchar(55), Branch nvarchar(55), Mark int)

insert into @temp values ('S1 ','Amir','EC',121)
insert into @temp values ('S2 ','Ewe ','EC',123)
insert into @temp values ('S3 ','Haye','EC',45 )
insert into @temp values ('S4 ','Mark','EC',145)
insert into @temp values ('S5 ','Tom ','CS',152)
insert into @temp values ('S6 ','Hudd','CS',218)
insert into @temp values ('S7 ','Ken ','CS',48 )
insert into @temp values ('S8 ','Ben ','CS',15 )
insert into @temp values ('S9 ','Wode','CS',123)
insert into @temp values ('S10','Kayl','IT',125)
insert into @temp values ('S11','Den ','IT',120)
insert into @temp values ('S12','Noy ','IT',126)
insert into @temp values ('S13','Jon ','GS',150)
insert into @temp values ('S14','JonS','GS',100)

-- MAKE CTE
;WITH CTE AS (
select *,RN = ROW_NUMBER() OVER (PARTITION BY Branch ORDER BY Mark DESC) from @temp
)
--QUERY
SELECT * FROM CTE WHERE RN=3
UNION
SELECT TOP 1 * FROM CTE 
WHERE Branch NOT IN 
(SELECT Branch FROM CTE WHERE RN=3)

答案 1 :(得分:0)

select Student_id,Student_Name,Mark,Branch from (
    select  Student_id,Student_Name,Mark,Branch,dense_rank() over (partition by Branch order by Mark desc) rnk
    from Student ) x where rnk=3
    union

    select Student_id,Student_Name,Mark,Branch from student
where Branch in  ( 
     select  Branch
    from Student 
    group by Branch
    having count(*) =1
    )