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 values (2, 'v12', 'v22', null);
insert into t_col_row values (3, 'v13', null, 'v33');
insert into t_col_row values (4, null, 'v24', 'v34');
insert into t_col_row values (5, 'v15', null, null);
insert into t_col_row values (6, null, null, 'v35');
insert into t_col_row values (7, null, null, null);
commit;
select * from t_col_row;
1)union
適用範圍:8i,9i,10g及以後版本
select id, 'c1' cn, c1 cv
from t_col_row
union
select id, 'c2' cn, c2 cv
from t_col_row
union
select id, 'c3' cn, c3 cv from t_col_row;
若空行不需要轉換,只需加乙個where條件,
where column is not null 即可。
2)model
適用範圍:10g及以後
select id, cn, cv from t_col_row
model
return updated rows
partition by (id)
dimension by (0 as n)
measures ('xx' as cn,'yyy' as cv,c1,c2,c3)
rules upsert all
(cn[1] = 'c1',
cn[2] = 'c2',
cn[3] = 'c3',
cv[1] = c1[0],
cv[2] = c2[0],
cv[3] = c3[0]
)order by id,cn;
3)collection
適用範圍:8i,9i,10g及以後版本
要建立乙個物件和乙個集合:
create type cv_pair as object(cn varchar2(10),cv varchar2(10));
create type cv_varr as varray(8) of cv_pair;
select id, t.cn as cn, t.cv as cv
from t_col_row,
table(cv_varr(cv_pair('c1', t_col_row.c1),
cv_pair('c2', t_col_row.c2),
cv_pair('c3', t_col_row.c3))) t
order by 1, 2;
Oracle行列轉換
行轉列 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,...
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...