假設每個區域代碼的前綴數爲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
您可以使用查詢,而不是這個輸出。例如'SELECT * FROM [TABLE_NAME] WHERE AREA_CODE = 21',反之亦然。 – unknown
只有當生成的列數是固定的時候,這是可能的,但是從您的需求來看,它似乎不是固定的。因此,不可能。 – San
喲可以使用SELECT AREA_CODE,WM_CONCACT(PREFIX)FROM yourtable GROUP BY PREFIX繪製您的目標,但格式不正確 – sqlab