關(guān)于oracle字符串匯總
來源:易賢網(wǎng) 閱讀:1309 次 日期:2014-09-17 10:49:45
溫馨提示:易賢網(wǎng)小編為您整理了“關(guān)于oracle字符串匯總”,方便廣大網(wǎng)友查閱!

使用的代碼

代碼如下:

-- sample data

drop table t_collect purge;

create table t_collect as

select mod(rownum,30) as flag, lpad(dbms_random.string('l',3),4,' ') as val

from dual connect by rownum<=10000;

collect函數(shù)(Oracle10g)

-- 1: use collect funtion

select flag,

my_tk.f_list2str(cast(collect(trim(val)) as my_tk_str_tab_type)) as ename

from t_collect sample(10)

group by flag

order by 1;

sys_connect_by_path

-- 2: use sys_connect_by_path and row_number function

select t1.flag, substr(max(sys_connect_by_path(t1.val, ',')), 2) q

from (select a.flag,

trim(a.val) as val,

row_number() over(partition by a.flag order by a.val) rn

from t_collect sample(10) a) t1

start with t1.rn = 1

connect by t1.flag = prior t1.flag

and t1.rn - 1 = prior t1.rn

group by t1.flag

order by 1;

user-defined-function

-- 3: use user-defined-function

select flag,

string_agg(trim(val)) as ename

from t_collect sample(10)

group by flag

order by 1;

輔助用的my_tk包代碼片段

代碼如下:

create or replace type my_tk_str_tab_type is table of varchar2(100);

---------------------------------------------------------------------

function f_list2str

(

p_list my_tk_str_tab_type,

p_separator varchar2 default ',',

p_sort integer default 1

) return varchar2 is

l_idx pls_integer := 0;

l_str varchar2(32767) := null;

l_spt varchar2(10) := null;

l_list my_tk_str_tab_type := p_list;

begin

if p_sort = 1 then

l_list := f_sort_list(p_list);

end if;

l_idx := l_list.first;

while l_idx is not null loop

l_str := l_str || l_spt || l_list(l_idx);

l_spt := p_separator;

l_idx := l_list.next(l_idx);

end loop;

return l_str;

end;

自定義聚合函數(shù)

------------------------------------------------------------------

代碼如下:

-- user-defined-function

CREATE OR REPLACE TYPE t_string_agg AS OBJECT

(

g_string VARCHAR2(32767),

STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT t_string_agg)

RETURN NUMBER,

MEMBER FUNCTION ODCIAggregateIterate(self IN OUT t_string_agg,

value IN VARCHAR2 )

RETURN NUMBER,

MEMBER FUNCTION ODCIAggregateTerminate(self IN t_string_agg,

returnValue OUT VARCHAR2,

flags IN NUMBER)

RETURN NUMBER,

MEMBER FUNCTION ODCIAggregateMerge(self IN OUT t_string_agg,

ctx2 IN t_string_agg)

RETURN NUMBER

);

/

CREATE OR REPLACE TYPE BODY t_string_agg IS

STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT t_string_agg)

RETURN NUMBER IS

BEGIN

sctx := t_string_agg(NULL);

RETURN ODCIConst.Success;

END;

MEMBER FUNCTION ODCIAggregateIterate(self IN OUT t_string_agg,

value IN VARCHAR2 )

RETURN NUMBER IS

BEGIN

SELF.g_string := self.g_string || ',' || value;

RETURN ODCIConst.Success;

END;

MEMBER FUNCTION ODCIAggregateTerminate(self IN t_string_agg,

returnValue OUT VARCHAR2,

flags IN NUMBER)

RETURN NUMBER IS

BEGIN

returnValue := RTRIM(LTRIM(SELF.g_string, ','), ',');

RETURN ODCIConst.Success;

END;

MEMBER FUNCTION ODCIAggregateMerge(self IN OUT t_string_agg,

ctx2 IN t_string_agg)

RETURN NUMBER IS

BEGIN

SELF.g_string := SELF.g_string || ',' || ctx2.g_string;

RETURN ODCIConst.Success;

END;

END;

/

CREATE OR REPLACE FUNCTION string_agg (p_input VARCHAR2)

RETURN VARCHAR2

PARALLEL_ENABLE AGGREGATE USING t_string_agg;

/

最后給個參考表

在數(shù)據(jù)量10000的表上進行三種方法的測試,運行時間單位秒

數(shù)據(jù)量PCT Collect SYS_Connect UDF

1% 0.017 0.018 0.017

10% 0.026 0.050 0.029

50% 0.057 2.45 0.065

100% 0.090 5.00 1.06

更多信息請查看IT技術(shù)專欄

更多信息請查看數(shù)據(jù)庫
易賢網(wǎng)手機網(wǎng)站地址:關(guān)于oracle字符串匯總
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)