2014-01-24 90 views
-3

柱如何行轉換爲列在Oracle行到在Oracle 10g

數據作爲下

AREA_CODE PREFIX 
    21  48 
    21  66 
    21  80 
    21  86 
    21  58 
    21  59 
    21  51 
    21  81 
    21  35 
    21  56 
    21  78 
    21  34 
    21  49 
    21  79 
    21  36 
    21  99 
    21  82 
    21  38 
    21  32 
    21  65 
    22  26 
    22  20 
    22  27 
    22  34 
    22  33 
    22  21 
    22  38 
    22  36 
    232  22 
    232  26 
    232  27 
    233  88 
    233  86 
    233  85 
    233  87 
    233  89 
    233  82 
    235  56 
    235  53 
    235  87 
    235  86 

所需的輸出將b

AREA_CODE P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13 
21 48 66 80 86 58 59 51 81 35 56 78 34 49 
22 26 20 27 34 33 21 38 36     
232 22 26 27 88 86 85 87 89 82 56 53 87 86 
+0

您可以使用查詢,而不是這個輸出。例如'SELECT * FROM [TABLE_NAME] WHERE AREA_CODE = 21',反之亦然。 – unknown

+0

只有當生成的列數是固定的時候,這是可能的,但是從您的需求來看,它似乎不是固定的。因此,不可能。 – San

+0

喲可以使用SELECT AREA_CODE,WM_CONCACT(PREFIX)FROM yourtable GROUP BY PREFIX繪製您的目標,但格式不正確 – sqlab

回答

0

假設每個區域代碼的前綴數爲10,表名爲table_name,該查詢可用於10 G

with tab as (select AREA_CODE, 
     PREFIX, 
     row_NUMBER() over(partition by AREA_CODE order by null) rn 
     from table_name) 
select AREA_CODE, 
     min(decode(rn, 1, PREFIX, null)) as PREFIX1, 
     min(decode(rn, 2, PREFIX, null)) as PREFIX2, 
     min(decode(rn, 3, PREFIX, null)) as PREFIX3, 
     min(decode(rn, 4, PREFIX, null)) as PREFIX4, 
     min(decode(rn, 5, PREFIX, null)) as PREFIX5, 
     min(decode(rn, 6, PREFIX, null)) as PREFIX6, 
     min(decode(rn, 7, PREFIX, null)) as PREFIX7, 
     min(decode(rn, 8, PREFIX, null)) as PREFIX8, 
     min(decode(rn, 9, PREFIX, null)) as PREFIX9, 
     min(decode(rn, 10, PREFIX, null)) as PREFIX10 
    from tab 
group by AREA_CODE 

而在11G

with tab as (select AREA_CODE, 
     PREFIX, 
     row_NUMBER() over(partition by AREA_CODE order by null) rn 
     from table_name) 
select * 
    from tab 
pivot (max(PREFIX) as PREFIX for RN in (1,2,3,4,5,6,7,8,9,10)) 

輸出:

| AREA_CODE | 1_PREFIX | 2_PREFIX | 3_PREFIX | 4_PREFIX | 5_PREFIX | 6_PREFIX | 7_PREFIX | 8_PREFIX | 9_PREFIX | 10_PREFIX | 
|-----------|----------|----------|----------|----------|----------|----------|----------|----------|----------|-----------| 
|  21 |  58 |  86 |  80 |  66 |  56 |  59 |  51 |  81 |  35 |  48 | 
|  22 |  33 |  34 |  27 |  20 |  26 |  21 |  36 |  38 | (null) | (null) | 
|  232 |  27 |  26 |  22 | (null) | (null) | (null) | (null) | (null) | (null) | (null) | 
|  233 |  85 |  86 |  88 |  87 |  82 |  89 | (null) | (null) | (null) | (null) | 
|  235 |  56 |  53 |  87 |  86 | (null) | (null) | (null) | (null) | (null) | (null) | 

更多的價值,你可以改變增加min(decode(rn, 1, PREFIX, null)) as PREFIX1列表。

我的測試數據是:

select 21,48 from dual union all 
select 21,66 from dual union all 
select 21,80 from dual union all 
select 21,86 from dual union all 
select 21,58 from dual union all 
select 21,59 from dual union all 
select 21,51 from dual union all 
select 21,81 from dual union all 
select 21,35 from dual union all 
select 21,56 from dual union all 
select 22,26 from dual union all 
select 22,20 from dual union all 
select 22,27 from dual union all 
select 22,34 from dual union all 
select 22,33 from dual union all 
select 22,21 from dual union all 
select 22,38 from dual union all 
select 22,36 from dual union all 
select 232,22 from dual union all 
select 232,26 from dual union all 
select 232,27 from dual union all 
select 233,88 from dual union all 
select 233,86 from dual union all 
select 233,85 from dual union all 
select 233,87 from dual union all 
select 233,89 from dual union all 
select 233,82 from dual union all 
select 235,56 from dual union all 
select 235,53 from dual union all 
select 235,87 from dual union all 
select 235,86 from dual