Oracle中的列轉行例子詳解

2022-07-03 21:06:13 字數 1505 閱讀 1421

資料如下:

name id

張三 1,2,3

要求實現:

name id

張三 1

張三 2

張三 3

--

建立臨時表

create

table tmp as(select'張三

' name, '

1,2,3

' id from

dual);

--寫法1

select name,regexp_substr(id,'

[^,]+

',1,level) as

idfrom

tmpconnect

bylevel

<= regexp_count(id,'

,')+

1order

byid

--寫法2:

select name,trim(regexp_substr(id,'

[^,]+

',1,level)) as

idfrom

tmpconnect

by name =

prior name

and prior dbms_random.value is

notnull

andlevel

<= length(regexp_replace(id, '

[^,]

'))+1;

--寫法3

select

name,

--regexp_replace(id,'(\w+)\,(\w+)\,(\w+)',level) id

regexp_replace(id,'

(\w+)\,(\w+)\,(\w+)

','\

'||to_char(level

)) id

from

tmpconnect

bylevel

<= regexp_count(id,'

,')+1;

--寫法4:

select

name,

substr(',

'||id||',

',instr(','

||id||',

', '

,', 1, level) +

1, instr(','

||id||',

', '

,', 1, level

+1) -( instr(','

||id||',

', '

,', 1, level) +1))

from

tmpconnect

bylevel

<= regexp_count(id,'

,')+

1order

bylevel

此外,列轉行還可以使用union all和unpivot(oracle 11g新特性)等,待後續補充

oracle的列轉行

首先建立測試環境 create table test1 user account varchar2 100 signup date date,user email varchar2 100 friend1 email varchar2 100 friend2 email varchar2 100 f...

oracle列轉行操作

這裡有兩張表,一張是錯誤類別表 ac dict option 另外一張是錯誤資訊表 mr meterdata 分別需要統計基礎資訊表中 性質變更 正常 霧珠 三種狀態的裝置數量,在group by 之後使用pivot 進行行列的變換.select from select from select me...

oracle列轉行的問題

今天群裡市討論乙個列轉行的問題,開始沒有一點頭緒的,後來有位大哥說到是列轉行的問題,好像以前看到過就沒沒有仔細去看,又查了下je裡面的貼子,找到了解決的辦法如下 現有兩張表,分別為 a table,b table 分別儲存資料如下 a table id name 1 zhang 2 li 3 wan...