Oracle行列倒置

  没事看了下Oracle奇奇怪怪的笔试题,发现一个很有意思的行列倒置,于是写了点代码测试了下,不过可能写的不好,但是功能能实现。

select t.name,
       sum(decode(t.course, '语文', t.score, '0')) 语文,
       sum(decode(t.course, '数学', t.score, '0')) 数学,
       sum(decode(t.course, '英语', t.score, '0')) 英语
  from score t
 group by t.name;
select * from (
select t.stunum 学号, (select '语文' from dual) 课程, t.chinese 分数 from gaokao t
union all
select t.stunum 学号, (select '数学' from dual) 课程, t.math 分数 from gaokao t
union all
select t.stunum 学号, (select '英语' from dual) 课程, t.english 分数 from gaokao t
union all
select t.stunum 学号, (select '物理' from dual) 课程, t.phisics 分数 from gaokao t
union all
select t.stunum 学号, (select '化学' from dual) 课程, t.chemistry 分数 from gaokao t) tt order by 学号;

 

posted @ 2014-08-30 15:17  神一样的存在  阅读(742)  评论(0)    收藏  举报