Oracle行列轉換

2021-05-25 06:07:54 字數 1423 閱讀 5217

--行轉列

select count(*) over() cnt, rn, str

from (select rownum rn,

substr(a.str,

instr(a.str, ',', 1, a.n) + 1,

instr(a.str, ',', 1, a.n + 1) -

instr(a.str, ',', 1, a.n) - 1) str

from (select ',a,b,c,d,' str, rownum n

from dual

connect by rownum <= 2000) a) t0

where t0.str is not null

--列轉行

create table tmp_tab_column(table_name  varchar2(30),column_names varchar2(4000));

declare

vc_tab_name varchar2(30);

begin

for cur_tb in (select table_name

from dba_tables

where owner = 'fm'

and rownum < 10) loop

vc_tab_name := cur_tb.table_name;

insert into tmp_tab_column

(table_name, column_names)

(select vc_tab_name,

max(substr(sys_connect_by_path(t.column_name, ','), 2)) || ')'

from (select a.column_name, a.column_id

from dba_tab_columns a

where a.owner = 'fm'

and a.table_name = vc_tab_name) t

start with t.column_id = 1

connect by prior t.column_id + 1 = t.column_id);

commit;

end loop;

end;

select max(substr(sys_connect_by_path(t.column_name, ','), 2))

from (select column_name, column_id

from dba_tab_columns

where owner = 'ocis'

and table_name = 'g_khda') t

start with t.column_id = 1

connect by prior t.column_id + 1 = t.column_id

Oracle行列轉換

1.列轉行 create table t col row id int,c1 varchar2 10 c2 varchar2 10 c3 varchar2 10 insert into t col row values 1,v11 v21 v31 insert into t col row valu...

oracle 行列轉換

q 如何實現行列轉換 a 1 固定列數的行列轉換 如student subject grade student1 語文 80 student1 數學 70 student1 英語 60 student2 語文 90 student2 數學 80 student2 英語 100 轉換為 語文 數學 英...

oracle 行列轉換

wmsys.wm concat功能來行轉列 create table idtable id number,name varchar2 30 insert into idtable values 10,ab insert into idtable values 10,bc insert into id...