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

行转列
1年  2年 3年
 1   2   3
 4   5   6


的到

1年  5

2年   7

3年   9

------解决方案--------------------
这种情况 还是考虑union all吧 


select '1年' y,sum(1年) from tb
union all
select '2年' y,sum(2年) from tb
...

------解决方案--------------------
如果年份是连续的 那可以考虑存储了 循环拼接
------解决方案--------------------
如果是oracle 11g,可以用pivot,unpivot函数

with t as
 (select 1 as "1year", 2 as "2year", 3 as "3year"
    from dual
  union all
  select 4 as "1year", 5 as "2year", 6 as "3year" from dual)
select year, sum(counts)
  from (select *
          from t unpivot(counts for year in("1year", "2year", "3year")))
 group by year
 order by 1;