關於Oracle自定義分組函式

bq_wang發表於2008-02-13
專案內需要解決主表a和明細表b的連線問題,要求將b表中的不同列部分進行合併起來;其實也是網上搜出來的解決辦法,:),只要自己理解了就成為自己的知識了,:)

Create Table wbq_test (a varchar2(10),b int);
Insert Into wbq_test values ('aa',1);
Insert Into wbq_test values ('bb',1);
Insert Into wbq_test values ('cc',1);
Insert Into wbq_test values ('dd',2);
Insert Into wbq_test values ('ee',2);
Insert Into wbq_test values ('ff',3);
Insert Into wbq_test values ('gg',3);
Insert Into wbq_test values ('hh',3);
Commit;
select * from wbq_test;

create or replace function f_GroupByColumn(GroupColumnValue in Varchar2)
return varchar2 is
type cur_type is ref cursor;
myCur cur_type;
v_Column1Value varchar2(20);
v_Result varchar2(2000);
begin
Open myCur for Select a from wbq_test where b=GroupColumnValue;
Loop
Fetch myCur Into v_Column1Value;
Exit When myCur%notfound;
v_Result:=v_Result||v_Column1Value||',';
End Loop;
Close myCur;
return(v_Result);
end ;

select b,f_GroupByColumn(b) from wbq_test group by b;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6517/viewspace-145567/,如需轉載,請註明出處,否則將追究法律責任。

相關文章