oracle分组结果变成行列,利用sum来实现行列转换-Oracle

利用sum来实现行列转换

原始表如下:  www.2cto.com

qqyAba.jpg

预期想要将表中的每一个IDX_CODE分别在查询结果中各显示一列,结果表如下:

3YFrui.jpg

建表语句如下:

create table test

(

IDX_DATA_ID   NUMBER(12) not null,

org_no        NUMBER(2),

org_name      VARCHAR2(6),

idx_code      VARCHAR2(12),

stat_cycle    NUMBER(6),

data_value    NUMBER(12),

CHAIN_VALUE   NUMBER(6,2)

)

插入数据:  www.2cto.com

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025889, 1, ‘北京’, ‘ZH001360’, 201210, 100000, 0.00);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025890, 1, ‘北京’, ‘ZH001360’, 201211, 110000, 1.10);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025891, 1, ‘北京’, ‘ZH001360’, 201212, 90000, 0.82);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025892, 1, ‘北京’, ‘ZH001359’, 201210, 200000, 0.00);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025893, 1, ‘北京’, ‘ZH001359’, 201211, 210000, 1.05);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025894, 1, ‘北京’, ‘ZH001359’, 201212, 190000, 0.90);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025895, 2, ‘上海’, ‘ZH001360’, 201210, 100000, 0.00);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025896, 2, ‘上海’, ‘ZH001360’, 201211, 110000, 1.10);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025897, 2, ‘上海’, ‘ZH001360’, 201212, 90000, 0.82);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025898, 2, ‘上海’, ‘ZH001359’, 201210, 200000, 0.00);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025899, 2, ‘上海’, ‘ZH001359’, 201211, 210000, 1.05);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025900, 2, ‘上海’, ‘ZH001359’, 201212, 190000, 0.90);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025901, 3, ‘天津’, ‘ZH001360’, 201210, 100000, 0.00);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025902, 3, ‘天津’, ‘ZH001360’, 201211, 110000, 1.10);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025903, 3, ‘天津’, ‘ZH001360’, 201212, 90000, 0.82);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025904, 3, ‘天津’, ‘ZH001359’, 201210, 200000, 0.00);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025905, 3, ‘天津’, ‘ZH001359’, 201211, 210000, 1.05);

insert into TEST (IDX_DATA_ID, ORG_NO, ORG_NAME, IDX_CODE, STAT_CYCLE, DATA_VALUE, CHAIN_VALUE)

values (1300025906, 3, ‘天津’, ‘ZH001359’, 201212, 190000, 0.90);

实现预期结果的查询语句如下:

www.2cto.com

SELECT T1.ORG_NAME,

T1.STAT_CYCLE,

SUM(CASE

WHEN T1.IDX_CODE = ‘ZH001359’ THEN

T1.DATA_VALUE

END) AS DV1,

SUM(CASE

WHEN T1.IDX_CODE = ‘ZH001360’ THEN

T1.DATA_VALUE

END) AS DV2,

CAST(SUM(CASE

WHEN T1.IDX_CODE = ‘ZH001359’ THEN

T1.CHAIN_VALUE

END) AS NUMBER(6, 2)) AS DV3

FROM TEST T1

WHERE T1.IDX_CODE IN (‘ZH001359’, ‘ZH001360’)

AND T1.STAT_CYCLE >= ‘201201’

AND T1.STAT_CYCLE <= ‘201212’

AND T1.ORG_NO = ‘1’

GROUP BY T1.STAT_CYCLE, T1.ORG_NAME

ORDER BY STAT_CYCLE DESC;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>