日期:2014-05-18  浏览次数:20395 次

简单sql语法,求回帖!求解决!在线等!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SQL code


create table StuScore
(
ID int identity(1,1),
[Name] varchar(20),
Class varchar(20),
Score int
)

truncate table StuScore

insert into StuScore values('张春明','语文',89)
insert into StuScore values('张春明','数学',97)
insert into StuScore values('刘菲','数学',97)
insert into StuScore values('刘菲','英语',20)
insert into StuScore values('刘菲','语文',70)
insert into StuScore values('吴京','语文',91)
insert into StuScore values('吴京','英语',96)
insert into StuScore values('袁磊','数学',84)
insert into StuScore values('袁磊','英语',96)

select * from StuScore





Ⅰ、对全体学生总成绩进行排序
Ⅱ、对全体学生单科成绩进行排序
(
排序要求:
a、必须使用排序函数;
b、如果成绩相同,不并排,按顺序[格式如:1、2、3];
c、如果成绩相同,并排,跳序排列[格式如:1、2、2、4];
d、如果成绩相同,并排,不跳序[格式如:1、2、2、3];
)


------解决方案--------------------
SQL code
select *,ROW_NUMBER()over( ORDER BY 總分 desc) AS 名次 from (select [Name],SUM(Score) AS 總分 FROM StuScore GROUP BY [Name])t

select *,DENSE_RANK()over(ORDER BY 總分 desc) AS 名次 from (select [Name],SUM(Score) AS 總分 FROM StuScore GROUP BY [Name])t

select *,RANK()over(ORDER BY 總分 desc) AS 名次 from (select [Name],SUM(Score) AS 總分 FROM StuScore GROUP BY [Name])t