假设你这个源表的表名为bd(表名随便起的),
字段:学生姓名为xm,课程名称为kc,考试成绩为cj。
1 :
select xm 学生姓名,
sum(decode(kc, '数学', cj, 0)) 数学成绩,
sum(decode(kc, '英语', cj, 0)) 英语成绩
from bd
where xm in (select b.xm
from (select xm, cj from bd where kc = '数学') b,
(select xm, cj from bd where kc = '英语') c
where b.xm = c.xm
and b.cj < c.cj)
group by xm;
2 :
select xm 学生姓名,
(case
when sx <= 59 then
'不及格'
when sx between 60 and 75 then
'及格'
when sx between 76 and 90 then
'良好'
when sx between 91 and 100 then
'优秀'
end) 数学成绩,
(case
when yy <= 59 then
'不及格'
when yy between 60 and 75 then
'及格'
when yy between 76 and 90 then
'良好'
when yy between 91 and 100 then
'优秀'
end) 英语成绩
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm);
3 :
a.英语最高成绩:
select xm, yy
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm)
where yy in (select max(yy)
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm));
b.英语最低成绩:
select xm, yy
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm)
where yy in (select min(yy)
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm));
c.数学最高成绩:
select xm, sx
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm)
where sx in (select max(sx)
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm));
d.数学最低成绩:
select xm, sx
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm)
where sx in (select min(sx)
from (select xm,
sum(decode(kc, '数学', cj, 0)) sx,
sum(decode(kc, '英语', cj, 0)) yy
from bd
group by xm));
以上语句全部在oracle数据库中实验成功了。你别告诉我你的数据库是微软的SQLSERVER。希望对你有帮助。当然了,有问题的话继续追问我。
温馨提示:内容为网友见解,仅供参考