赞
踩
- create table SC(SId varchar2(10),CId varchar2(10),score decimal(18,1));
- insert into SC values('01' , '01' , 80);
- insert into SC values('01' , '02' , 90);
- insert into SC values('01' , '03' , 99);
- insert into SC values('02' , '01' , 70);
- insert into SC values('02' , '02' , 60);
- insert into SC values('02' , '03' , 80);
- insert into SC values('03' , '01' , 80);
- insert into SC values('03' , '02' , 80);
- insert into SC values('03' , '03' , 80);
- insert into SC values('04' , '01' , 50);
- insert into SC values('04' , '02' , 30);
- insert into SC values('04' , '03' , 20);
- insert into SC values('05' , '01' , 76);
- insert into SC values('05' , '02' , 87);
- insert into SC values('06' , '01' , 31);
- insert into SC values('06' , '03' , 34);
- insert into SC values('07' , '02' , 89);
- insert into SC values('07' , '03' , 98);
1.SELECT SID ,sum(case when CID='01' then '1' else '0' end) ,avg(sid) FROM SC group by SID order by SID
2.SELECT SID ,sum(case when CID='01' then score else 0 end) ,avg(sid) FROM SC group by SID order by SID
在使用case when 条件1 then 结果1 else 结果2 end 时 要注意返回结果的数据类型要一致。
SELECT SID ,sum(case when CID='01' then score else 0 end) as "语文" ,sum(case when CID='02' then score else 0 end) as "数学" ,sum(case when CID='03' then score else 0 end) as "英语" , cast(avg(score) as number(10,2)) FROM SC group by SID order by avg(score)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。