面试题 行转列

create table T_TEMP
(
  year   VARCHAR2(4),
  season VARCHAR2(10),
  count  NUMBER(4)
)
insert into T_TEMP (year, season, count)
values ('2010', '一季度', 100);
insert into T_TEMP (year, season, count)
values ('2010', '二季度', 200);
insert into T_TEMP (year, season, count)
values ('2010', '三季度', 300);
insert into T_TEMP (year, season, count)
values ('2010', '四季度', 400);
insert into T_TEMP (year, season, count)
values ('2011', '一季度', 150);
insert into T_TEMP (year, season, count)
values ('2011', '二季度', 250);
insert into T_TEMP (year, season, count)
values ('2011', '三季度', 350);
insert into T_TEMP (year, season, count)
values ('2011', '四季度', 450);

要求一条sql语句得出以下格式

year一季度二季度三季度四季度
2010100200300400
2011150250350450


sql如下:


select year,
       sum(decode(season, '一季度', count)) as "一季度",
       sum(decode(season, '二季度', count)) as "二季度",
       sum(decode(season, '三季度', count)) as "三季度",
       sum(decode(season, '四季度', count)) as "四季度"
  from T_TEMP
 group by year;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值