刚接触SQL,请大家指点下,感激不尽现有表
Grade 学生成绩表
create table grade
(
student_name char(8) not null, -- 学生姓名
subject char(8) not null, --课目
grade float not null --成绩
primary key(student_name,subject)
)
问题如下:
1。求出总分排在 第三名 的学生和总分。
2。列出前三名的学生与分别与前一名的学生的分差(请基于SQL SERVER 2005去完成调试出结果)
如:
student_name ,总分, 与前一名的分差
张三 200 0
李四 190 10
王八 120 70
Grade 学生成绩表
create table grade
(
student_name char(8) not null, -- 学生姓名
subject char(8) not null, --课目
grade float not null --成绩
primary key(student_name,subject)
)
问题如下:
1。求出总分排在 第三名 的学生和总分。
2。列出前三名的学生与分别与前一名的学生的分差(请基于SQL SERVER 2005去完成调试出结果)
如:
student_name ,总分, 与前一名的分差
张三 200 0
李四 190 10
王八 120 70
(
student_name char(8) not null, -- 学生姓名
subject char(8) not null, --课目
grade float not null --成绩
primary key(student_name,subject)
) insert into grade values('张三','语文',70)
insert into grade values('张三','数学',80)
insert into grade values('张三','物理',80)insert into grade values('李四','语文',20)
insert into grade values('李四','数学',30)
insert into grade values('李四','物理',30)insert into grade values('王二','语文',90)
insert into grade values('王二','数学',90)
insert into grade values('王二','物理',90)SELECT * FROM
(
SELECT *, ROW_NUMBER() OVER(ORDER BY TOTAL DESC) AS RANK
FROM
(
SELECT STUDENT_NAME,SUM(GRADE) AS TOTAL FROM GRADE
GROUP BY STUDENT_NAME
)T
)T
WHERE RANK = 3
if object_id('[grade]') is not null drop table [grade]
go
create table [grade]([student_name] varchar(4),[subject] char(8),[grade] int)
insert [grade]
select '张三','1',200 union all
select '李四','2',190 union all
select '王八','3',120
go
--select * from [grade]with t1 as
(select student_name,total=sum(grade) from grade group by student_name)
,t2 as
(select topGrade=max(total) from t1)
select student_name,total,dis=topGrade-total
from t1,t2
order by total desc
/*
student_name total dis
------------ ----------- -----------
张三 200 0
李四 190 10
王八 120 80(3 行受影响)
*/