利用函式實現的oracle行列轉換

2021-04-13 12:13:57 字數 1977 閱讀 1037

方式二,資料內容為不可**的,則需要借助儲存過程或者函式來實現

如下面所述的表內容,在name欄位內容不確定時,合併相同id的那麼值可以有兩種實現方式,其一為利用函式,其二使用表變數並編寫儲存過程實現。下面描述了用函式實現的方式。

table1(id varchar2(10), name varchar2(10))

id  name

1   aa

1   bb

1   cc

2   xx

3   yy

3   zz

...想得到乙個結果集如下:

id  names

1   aa+bb+cc

2   xx

3   yy+zz

--建立測試環境

create table table1(

id varchar(3),

name varchar2(10)

);--插入測試資料

insert into table1 values('001','sd');

insert into table1 values('001','vf');

insert into table1 values('001','rt');

insert into table1 values('002','3r');

insert into table1 values('002','gf');

insert into table1 values('003','vf');

insert into table1 values('003','hji');

--檢視當前資料表內容

select * from table1;

commit;

create or replace function can_table1(asql varchar2)

return varchar2

is

type typ_cursor is ref cursor;

cur_table1 typ_cursor;

atemp varchar2(10);

aresult varchar2(100):= '';

begin

open cur_table1 for asql;

loop

fetch cur_table1 into atemp;

exit when cur_table1%notfound;

aresult := aresult || atemp;

end loop;

return aresult;

end;

selectid,can_table1(

'select name from table1 where id = '''

||id||

''''

)from

(selectdistinctidfromtable1) t;

oracle實現行列轉換

ql select from student idname chinese math english 1a 90 70 80 2b 80 70 90 3c 80 90 70 select id,name,chinese 課程,chinese 分數 from student union all sel...

Oracle 行列轉換函式pivot使用

描述 在專案中,需要將表中日期資料行,用檢視轉列顯示月報表 注意 多聚合必須重新命名。主鍵 varchar2 50 default sys guid not null 日期 varchar2 10 學生姓名 varchar2 20 學生分組 varchar2 10 學號 varchar2 10 in...

Oracle的行列轉換

b 行轉列 b 1.列固定的情況,通過max decode變換。2.列不固定的時候,通過自定義function轉換。3.通過層次查詢,將行轉換成字串。oracle 9i中沒有connect by isleaf,可以使用分析函式實現 select n.tid,max n.typename keep d...