有3张表,Student表、SC表和Course表
Student表:学号(Sno)、姓名(Sname)、性别(Ssex)、年龄(Sage)和系名(Sdept)
Course表:课程号(Cno)、课程名(Cname)和学分(Ccredit);
SC表:学号(Sno)、课程号(Cno)和成绩(Grade)
请使用SQL语句查询学生姓名及其课程总学分
(注:如果课程不及格,那么此课程学分为0)
Student表:学号(Sno)、姓名(Sname)、性别(Ssex)、年龄(Sage)和系名(Sdept)
Course表:课程号(Cno)、课程名(Cname)和学分(Ccredit);
SC表:学号(Sno)、课程号(Cno)和成绩(Grade)
请使用SQL语句查询学生姓名及其课程总学分
(注:如果课程不及格,那么此课程学分为0)
解决方案 »
- Oracle 存错过程
- 为什么ORACLE 插入数据中文时,是乱码????
- 请教大虾——连接远程数据库 SQLServer2000或者Oracle10g
- 如何将 Blob中的内容显示到 jsp页面
- 单表查询 有两个字段相同,一个字段不同,合并不同字段,删除一个
- 存储过程utl_file的问题,请高手解决
- 关于数据库如何选择
- 如何是插入的数据Id号是紧密相连,无间隙???求教一简单问题。
- 请oracle高手帮忙一个菜鸟问题!
- 服务器上oracle数据库实例最近两月就挂掉一次,报472错误,还没查到具体问题,求帮忙啊,万分感谢!
- 请教,toad 9.6怎么跟踪调试pl/sql存储过程?
- 怪事--查询出的记录和插入表的记录数相差太多
case
when b.grade>= 60 then c.ccredit
else o end ccredit
from student a,sc b,course c
where a.sno=b.sno
and b.cno=c.cno再做个汇总就行
(select a.姓名 as xm,
case
when b.grade>= 60 then c.ccredit
else o end ccredit
from student a,sc b,course c
where a.sno=b.sno
and b.cno=c.cno) x group by x.xm order by x.xm
drop table student;
create table student(sno varchar2(10),sname varchar2(20),sex varchar2(2),sage int,sdept varchar2(20));
insert into student values('01','lili','F',23,'计算机');
insert into student values('02','zhangfei','M',23,'计算机');
insert into student values('03','guanyu','M',26,'计算机');drop table course;
create table course(cno varchar2(10),cname varchar2(20),ccredit int);
insert into course values('01','高数',6);
insert into course values('02','物量',6);
insert into course values('03','C语言',12);
insert into course values('04','数字电路',12);
drop table sc;
create table sc(sno varchar2(10),cno varchar2(10),grade int);
insert into sc values('01','01',65);
insert into sc values('01','02',87);
insert into sc values('01','03',69);
insert into sc values('01','04',54);insert into sc values('02','01',65);
insert into sc values('02','02',65);
insert into sc values('02','03',65);
insert into sc values('02','04',65);insert into sc values('03','01',57);
insert into sc values('03','02',98);
insert into sc values('03','03',47);
insert into sc values('03','04',80);select sname 姓名,sum(ccredit) 总学分
from(select a.sname,
case
when b.grade>= 60 then c.ccredit
else 0 end ccredit
from student a,sc b,course c
where a.sno=b.sno
and b.cno=c.cno)
group by sname--sqlserver下面
drop table student;
create table student(sno varchar(10),sname varchar(20),sex varchar(2),sage int,sdept varchar(20));
insert into student values('01','lili','F',23,'计算机');
insert into student values('02','zhangfei','M',23,'计算机');
insert into student values('03','guanyu','M',26,'计算机');drop table course;
create table course(cno varchar(10),cname varchar(20),ccredit int);
insert into course values('01','高数',6);
insert into course values('02','物量',6);
insert into course values('03','C语言',12);
insert into course values('04','数字电路',12);
drop table sc;
create table sc(sno varchar(10),cno varchar(10),grade int);
insert into sc values('01','01',65);
insert into sc values('01','02',87);
insert into sc values('01','03',69);
insert into sc values('01','04',54);insert into sc values('02','01',65);
insert into sc values('02','02',65);
insert into sc values('02','03',65);
insert into sc values('02','04',65);insert into sc values('03','01',57);
insert into sc values('03','02',98);
insert into sc values('03','03',47);
insert into sc values('03','04',80);select sname as 姓名,sum(ccredit) as 总学分
from
(select a.sname,
case
when b.grade>= 60 then c.ccredit
else 0 end ccredit
from student a,sc b,course c
where a.sno=b.sno
and b.cno=c.cno) as c
group by sname
(
sno NUMBER,
sname VARCHAR2(20) NOT NULL,
ssex CHAR(1),
sage NUMBER(22,3),
sdept VARCHAR2(20)
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
NOMONITORING;
ALTER TABLE HR.student ADD (
CONSTRAINT student_PK
PRIMARY KEY
(sno));CREATE TABLE HR.course
(
cno NUMBER,
cname VARCHAR2(50) NOT NULL,
ccredit NUMBER NOT NULL
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
NOMONITORING;
ALTER TABLE HR.course ADD (
PRIMARY KEY
(cno));
CREATE TABLE HR.SC
(
SNO NUMBER,
CNO NUMBER,
GRADE NUMBER
)
TABLESPACE USERS
PCTUSED 0
PCTFREE 10
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
)
LOGGING
NOCOMPRESS
NOCACHE
NOPARALLEL
MONITORING;
ALTER TABLE HR.SC ADD (
FOREIGN KEY (SNO)
REFERENCES HR.STUDENT (SNO),
CONSTRAINT F_COURSE
FOREIGN KEY (CNO)
REFERENCES HR.COURSE (CNO));SNO,SNAME,SSEX,SAGE,SDEPT
1,Tom,M,20,IT
2,MAY,F,19,MBACNO,CNAME,CCREDIT
1,C program,6
2,Math,5
3,English,5.5SNO,CNO,GRADE
1,1,80
1,2,75
1,3,55
2,1,65
2,2,60
2,3,95
select (select sname from hr.student where sno=sc.sno) as sname,
sum(c.ccredit) as credit
from hr.student s, hr.course c, hr.sc
where SC.SNO = S.SNO
and sc.cno = c.cno
and sc.grade >= 60
group by sc.sno
SNAME,CREDIT
Tom,11
MAY,16.5沒有SQL server環境
--o or s
select t.sname, sum(t.credit) credit from (
select s.sno, s.sname, case when sc.grade is null or sc.grade < 60 then 0 else c.cCredit end credit from student s
left join sc on s.sno = sc.sno
left join c on sc.con = c.con) t
group by t.sno, t.sname
select sname, sum(Ccredit) Ccredit
from
(
select a.sname, case when c.grade<60 then 0 else Ccredit end Ccredit
from student a, course b, sc c
where a.sno=c.sno
and c.cno=b.cno
)
group by sname
对, 有可能出现重名时,把几人的学分算一起的情况;
可以加上学号;select sname, sno, sum(Ccredit) Ccredit
from
(
select a.sname, a.sno, case when c.grade<60 then 0 else Ccredit end Ccredit
from student a, course b, sc c
where a.sno=c.sno
and c.cno=b.cno
)
group by sname, sno
select d.Sname, c.credit
from (select a.sno,
sum(decode(sign(a.grade - 60), -1, 0, b.Ccredit)) credit
from sc a, course b
where a.cno = b.cno
group by a.sno) c,
student d
where c.sno = d.sno