2016-04-21 17 views
1

我有一個表(表A)是主鍵選擇獲取3行數據與3列(可樂,COLB,COLC)</p> <p>可樂COLB 3分不同的列

select * 
from TableA 
where ColA = '001'; 

給我這

COLA COLB  COLC 
---- ----- -------- 
001 AA1  460 
001 AB1  380 
001 AC1  950 

我需要這個結果在這個格式

COLA   AA1   AB1   AC1 
----------- ----------- ----------- ----------- 
001   460   380   950 

即所有結果行成一列到不同的對應列。

+0

PIVOT是你在找什麼:) .. https://oracle-base.com/articles/11g/pivot -and-unpivot-operators-11gr1 – William

+0

搜索'數據透視表' – HoneyBadger

+2

[Oracle數據透視運算符]的可能重複(http://stackoverflow.com/questions/19280591/oracle-pivot-operator) –

回答

2

而且PIVOT版本:

SELECT * 
FROM 
(
    SELECT 
     * 
    FROM 
     TableA 
    WHERE ColA = '001' 
) 
PIVOT 
(
    MAX(ColC) 
    FOR ColB IN ('AA1','AB1','AC1') 
) 
1

試試這個

select 
COLA, 
max(case when COLB='AA1' then COLC end) as AA1 
max(case when COLB='AB1' then COLC end) as AB1 
max(case when COLB='AC1' then COLC end) as AC1 
from table 
group by COLA