orcle行转列

-----decode行转列
 
select name "姓名",
 
       sum(decode(subject, '语文', nvl(score, 0), 0)) "语文",
 
       sum(decode(subject, '数学', nvl(score, 0), 0)) "数学",
 
       sum(decode(subject, '英语', nvl(score, 0), 0)) "英语"
 
  from student_score
 
 group by name;
 
  
 
------ case when 行转列
 
  
 
select name "姓名",
 
       sum(case when subject='语文'
 
       then nvl(score,0)
 
       else 0
 
       end) "语文",
 
       sum(case when subject='数学'
 
       then nvl(score,0)
 
       else 0
 
       end) "数学",
 
       sum(case when subject='英语'
 
       then nvl(score,0)
 
       else 0
 
       end) "英语"
 
      from student_score
 
       group by name;

posted on 2016-07-10 20:37  静以修身!  阅读(295)  评论(0编辑  收藏  举报

导航