Oracle多行資料顯示為一行

2021-06-16 04:38:23 字數 2482 閱讀 8315

最近在做一新專案涉及到複雜的查詢,其中就包括需要將多行資料轉化為一行顯示,在網上google了一把然後自己改了一點就可以用了,記錄下來以為後用.

第一步: 新建types型別:

create or replace type combstrtype as object

(currentstr varchar2(4000),

currentseprator varchar2(8),

static function odciaggregateinitialize(sctx in out combstrtype) 

return number,

member function odciaggregateiterate(self in out combstrtype, 

value in varchar2) return number,

member function odciaggregateterminate(self in combstrtype, 

returnvalue out varchar2, flags in number) return number,

member function odciaggregatemerge(self in out combstrtype, 

ctx2 in combstrtype) return number

);create or replace type body combstrtype is

static function odciaggregateinitialize(sctx in out combstrtype)

return number is

begin

sctx := combstrtype('',' / ');

return odciconst.success;

end;

member function odciaggregateiterate(self in out combstrtype, value in varchar2) return number is

begin

if self.currentstr is null then

self.currentstr := value;

else

self.currentstr := self.currentstr ||currentseprator || value;

end if;

return odciconst.success;

end;

member function odciaggregateterminate(self in combstrtype, returnvalue out varchar2, flags in number) return number is

begin

returnvalue := self.currentstr;

return odciconst.success;

end;

member function odciaggregatemerge(self in out combstrtype, ctx2 in combstrtype) return number is

begin

if ctx2.currentstr is null then

self.currentstr := self.currentstr;

elsif self.currentstr is null then

self.currentstr := ctx2.currentstr;

else

self.currentstr := self.currentstr || currentseprator || ctx2.currentstr;

end if;

return odciconst.success;

end;

end;

第二步:新建函式

create or replace function

combs

tr (input varchar2) return varchar2

parallel_enable aggregate using combstrtype;

第三步:查詢

select tm_teamname, combs

tr (tm_subteam)  from as02tm

group by tm_teamname;

tm_teanname        combs

tr (tm_subteam)

aaa                     a / b / c

bbb                     a / c

ps:  oracle 10g 後提供了乙個聚集函式 wm_concat 可以實現此類功能

select tm_teamname, wm_concat(tm_subteam)  from as02tm    group by tm_teamname;

oracle 多行資料 轉為 一行資料,用逗號分隔

oracle 多行資料 轉為 一行資料,用逗號分隔 有時候我們需要將多行資料轉為一行,多個數值用逗號分隔,其實很簡單,只需要乙個oracle 提供的函式即可,wm concat 函式 即可實現該功能 普通查詢 如下 select crm.d ringname,czm.d zonename,cc.d ...

mysql 一行資料拆分多行

查詢出被逗號分隔字段需要拆分的最大數量 select max length 逗號分隔的字段 length replace 逗號分隔的字段,1 from 處理表 where 條件 建立一張臨時表用於聯合查詢,方便把處理表單行記錄分隔為多行 create temporary table incre ta...

ORACLE 一行轉多行

解決方法的核心是 產生出1 10的乙個列,作為 輔助列 select level l from dual connect by level 10 問題 兩個表 a b a 表 id pid a1 1 a2 2 a3 3 b 表 pid pnumber 1 22 3 3 5要根據pnumber的數量生...