这个sql如何写?
现有表结构如下:
year month value code
2005 01 100 123123
2005 02 300 123123
2005 03 300 123123
2005 04 40 123123
2005 05 40 123123
2005 01 200 5241
2005 02 200 5241
2005 01 200 241
2005 03 200 241
如何得到如下的结果:
code 1月 2月 3 月 4月 5月 6月 7月 。。。
123123 100 300 300 40 40 0 0
5241 200 200 0 0 0 0 0
241 200 0 200 0 0 0 0
没有数值得月份数据为0、
如何实现???
问题点数:20、回复次数:1Top
1 楼wgsasd311(自强不息)回复于 2005-11-16 18:49:53 得分 20
create table tb(year varchar2(4),month varchar2(2),
value number,code varchar2(10));
insert into tb values(2005,'01',100,123123);
insert into tb values(2005,'02',300,123123);
insert into tb values(2005,'03',300,123123);
insert into tb values(2005,'04',40, 123123);
insert into tb values(2005,'05',40, 123123);
insert into tb values(2005,'01',200,5241);
insert into tb values(2005,'02',200,5241);
insert into tb values(2005,'01',200,241);
insert into tb values(2005,'03',200,241);
select code,
sum(case month when '01' then value else 0 end) "1月",
sum(case month when '02' then value else 0 end) "2月",
sum(case month when '03' then value else 0 end) "3月",
sum(case month when '04' then value else 0 end) "4月",
sum(case month when '05' then value else 0 end) "5月",
sum(case month when '06' then value else 0 end) "6月",
sum(case month when '07' then value else 0 end) "7月",
sum(case month when '08' then value else 0 end) "8月",
sum(case month when '09' then value else 0 end) "9月",
sum(case month when '10' then value else 0 end) "10月",
sum(case month when '11' then value else 0 end) "11月",
sum(case month when '12' then value else 0 end) "12月"
from tb group by code;
drop table tb;Top




