表结构:
create table Students(
Sid varchar(50) not null primary key,
Sname varchar(50)not null,
Sage datetime,
Ssex varchar(50)
)
create table Grade(
Sid varchar(50) not null,
Cid varchar(10) not null,
grade decimal(18,1)
)
create table Course(
Cid varchar(10) not null primary key,
Cname varchar(10) not null,
)
alter table Grade
add constraint FK_Students_SC
foreign key (Sid) references Students (Sid)
alter table Grade
add constraint FK_Course_SC
foreign key (Cid) references Course (Cid)
insert into Students values('01' , '赵雷' , '1990-01-01' , '男')
insert into Students values('02' , '钱电' , '1990-12-21' , '男')
insert into Students values('03' , '孙风' , '1990-05-20' , '男')
insert into Students values('04' , '李云' , '1990-08-06' , '男')
insert into Students values('05' , '周梅' , '1991-12-01' , '女')
insert into Students values('06' , '吴兰' , '1992-03-01' , '女')
insert into Students values('07' , '郑竹' , '1989-07-01' , '女')
insert into Students values('08' , '王菊' , '1990-01-20' , '女')
insert into Course values('01' , '语文')
insert into Course values('02' , '数学')
insert into Course values('03' , '英语')
insert into Grade values('01' , '01' , 80)
insert into Grade values('01' , '02' , 90)
insert into Grade values('01' , '03' , 99)
insert into Grade values('02' , '01' , 70)
insert into Grade values('02' , '02' , 60)
insert into Grade values('02' , '03' , 80)
insert into Grade values('03' , '01' , 80)
insert into Grade values('03' , '02' , 80)
insert into Grade values('03' , '03' , 80)
insert into Grade values('04' , '01' , 50)
insert into Grade values('04' , '02' , 30)
insert into Grade values('04' , '03' , 20)
insert into Grade values('05' , '01' , 76)
insert into Grade values('05' , '02' , 87)
insert into Grade values('05' , '03' , 87)
insert into Grade values('06' , '02' , 87)
insert into Grade values('06' , '02' , 87)
insert into Grade values('06' , '03' , 31)
insert into Grade values('07' , '01' , 80)
insert into Grade values('07' , '02' , 90)
insert into Grade values('07' , '03' , 99)
------解决方案--------------------
/*sname grade
-------------------------------------------------- ---------------------------------------
赵雷 269.0
郑竹 269.0
周梅 250.0
(3 行受影响)*/
------解决方案--------------------
试试这个:
select *
from
(
select
s.Sname as '姓名',SUM(g.grade) as '总成绩'
,dense_rank()over( order by SUM(g.grade) desc) as '排名'