我想要Pivot多列。有什么用oracle数据透视表?
SQL:
SELECT * FROM
(
SELECT *
FROM IRO_SIM A
WHERE A.COM_CODE = 'AAQ'
AND A.PCODE = 'AKIOP'
)
PIVOT
(
LISTAGG(SIMTYPE,',')
WITHIN GROUP (ORDER BY SIMTYPE)
FOR SIMTYPE IN ('H','V')
)
示例数据:
COM_CODE | PCODE | L_VALUE | A_SIM | AMT_SIM | SIMTYPE
A | AKIOP | 1700 | TOTAL | 50 | H
A | AKIOP | 500 | EACH | 100 | V
A | BHUIO | 200 | TOTAL | 500 | H
A | BHUIO | 600 | TOTAL | 400 | V
我需要结果:
COM_CODE | PCODE | H_VALUE | H_ASIM | H_AMTSIM | V_VALUE | V_ASIM | V_AMTSIM
A | AKIOP | 1700 | TOTAL | 50 | 500 | EACH | 100
A | BHUIO | 200 | TOTAL | 500 | 600 | TOTAL | 400
感谢提前:)
答案 0 :(得分:2)
只列出多列。 PIVOT
子句中的每个表达式都将与FOR
子句中的每个值匹配。那么,你想要的是:
SELECT * FROM d
PIVOT ( sum(l_value) as value, max(a_sim) as asim, sum(amt_sim) as amtsim
FOR simtype in ('H' AS "H", 'V' AS "V") )
使用数据......
with d as (
SELECT 'A' com_code, 'AKIOP' pcode, 1700 l_value, 'TOTAL' a_sim, 50 amt_sim, 'H' simtype FROM DUAL UNION ALL
SELECT 'A' com_code, 'AKIOP' pcode, 500 l_value, 'EACH' a_sim, 100 amt_sim, 'V' simtype FROM DUAL UNION ALL
SELECT 'A' com_code, 'BHUIO' pcode, 200 l_value, 'TOTAL' a_sim, 500 amt_sim, 'H' simtype FROM DUAL UNION ALL
SELECT 'A' com_code, 'BHUIO' pcode, 600 l_value, 'TOTAL' a_sim, 400 amt_sim, 'V' simtype FROM DUAL)
SELECT * FROM d
PIVOT ( sum(l_value) as value, max(a_sim) as asim, sum(amt_sim) as amtsim
FOR simtype in ('H' AS "H", 'V' AS "V") )