Sql oracle 10G中的行到列
如何在oracle中将行转换为列 数据如下所示Sql oracle 10G中的行到列,sql,oracle,oracle10g,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 7
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
假设每个区号的前缀数为10,表名为table_name,则该查询可以在10g中使用
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))的列表更改为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
您可以使用查询而不是此输出。e、 g.
从[TABLE_NAME]中选择*,其中AREA_code=21
反之亦然。只有在生成的列数是固定的情况下,才有可能这样做,但根据您的要求,它似乎不是固定的。因此,这是不可能的。您可以通过从表组中按前缀选择区域代码WM\U CONACT(前缀)来接近您的目标,尽管格式不好