日期:2014-05-17  浏览次数:20876 次

还请高人指点写一个oracle的sql
year  month  amount
1991  1      1.1
1991  2      1.2
1991  3      1.3
1991  4      1.4
1992  1      2.1
1992  2           2.2
1992  3      2.3
1992  4      2.4
需要的结果:
   y   m1  m2  m3  m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4
还请大虾帮忙呀!感激不尽!!!!!!!!!!

------解决方案--------------------
11g以下的版本,写起来代码比较多:
select year y,
       sum(decode(month, 1, amount)) m1,
       sum(decode(month, 2, amount)) m2,
       sum(decode(month, 3, amount)) m3,
       sum(decode(month, 4, amount)) m4
  from t
 group by year
------解决方案--------------------
引用:
11g的版本可以用pivot函数:

select * from t pivot(sum(amount) for month in(1 m1, 2 m2, 3 m3, 4 m4));



第一次接触 挺好用的