在oracle 10G中排到列

时间:2014-01-24 10:52:51

标签: sql oracle oracle10g

如何在oracle中将行转换为列

数据在

 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

1 个答案:

答案 0 :(得分: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