当前位置:   article > 正文

oracle 中分类统计:case when..then..else..end用法_oracle case when then else 统计

oracle case when then else 统计
  1. create table SC(SId varchar2(10),CId varchar2(10),score decimal(18,1));
  2. insert into SC values('01' , '01' , 80);
  3. insert into SC values('01' , '02' , 90);
  4. insert into SC values('01' , '03' , 99);
  5. insert into SC values('02' , '01' , 70);
  6. insert into SC values('02' , '02' , 60);
  7. insert into SC values('02' , '03' , 80);
  8. insert into SC values('03' , '01' , 80);
  9. insert into SC values('03' , '02' , 80);
  10. insert into SC values('03' , '03' , 80);
  11. insert into SC values('04' , '01' , 50);
  12. insert into SC values('04' , '02' , 30);
  13. insert into SC values('04' , '03' , 20);
  14. insert into SC values('05' , '01' , 76);
  15. insert into SC values('05' , '02' , 87);
  16. insert into SC values('06' , '01' , 31);
  17. insert into SC values('06' , '03' , 34);
  18. insert into SC values('07' , '02' , 89);
  19. 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)
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/485175
推荐阅读
相关标签
  

闽ICP备14008679号