Oracle可用鏈接函數會介紹如下幾個sql
LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)
用法就像聚合函數同樣,經過Group by語句,把每一個Group的一個字段,拼接起來。
其中LISTAGG函數第一個參數爲要拼接的字段,第二個參數爲用什麼字符串進行鏈接
eg : listagg(city,’,’)
後面GROUP()中爲對鏈接以後的行數據按什麼字段進行排序
eg : order by cityoracle
with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nation ,'Shanghai' city from dual union all select 'China' nation ,'Beijing' city from dual union all select 'USA' nation ,'New York' city from dual union all select 'USA' nation ,'Bostom' city from dual union all select 'Japan' nation ,'Tokyo' city from dual ) select nation,listagg(city,',') within GROUP (order by city) as Cities from temp group by nation
運行結果:函數
就是over(partition by XXX)
也就是說,在你不實用Group by語句時候,也能夠使用LISTAGG函數:.net
with temp as( select 500 population, 'China' nation ,'Guangzhou' city from dual union all select 1500 population, 'China' nation ,'Shanghai' city from dual union all select 500 population, 'China' nation ,'Beijing' city from dual union all select 1000 population, 'USA' nation ,'New York' city from dual union all select 500 population, 'USA' nation ,'Bostom' city from dual union all select 500 population, 'Japan' nation ,'Tokyo' city from dual ) select population, nation, city, listagg(city,',') within GROUP (order by city) over (partition by nation) rank from temp
運行結果:code
2.3總結blog
listagg()函數支持最低版本須要Oracle 11gR2,查詢本身Oracle版本sql以下,排序
SELECT v.VERSION FROM v$instance v;
若是版本低於11g,查詢會報錯 [未找到要求的 FROM 關鍵字] token
with temp as( select 'China' nation ,'Guangzhou' city from dual union all select 'China' nation ,'Shanghai' city from dual union all select 'China' nation ,'Beijing' city from dual union all select 'USA' nation ,'New York' city from dual union all select 'USA' nation ,'Bostom' city from dual union all select 'Japan' nation ,'Tokyo' city from dual ) select nation,strcat(city) from temp group by nation
結果爲:ci
ORACLE 字符串聚合函數 strCat字符串
create or replace type strcat_type as object ( currentstr varchar2(4000), currentseprator varchar2(8), static function ODCIAggregateInitialize(sctx IN OUT strcat_type) return number, member function ODCIAggregateIterate(self IN OUT strcat_type,value IN VARCHAR2) return number, member function ODCIAggregateTerminate(self IN strcat_type,returnValue OUT VARCHAR2, flags IN number) return number, member function ODCIAggregateMerge(self IN OUT strcat_type,ctx2 IN strcat_type) return number ); create or replace type body strcat_type is static function ODCIAggregateInitialize(sctx IN OUT strcat_type) return number is begin sctx := strcat_type('',','); return ODCIConst.Success; end; member function ODCIAggregateIterate(self IN OUT strcat_type, 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 strcat_type, returnValue OUT VARCHAR2, flags IN number) return number is begin returnValue := self.currentstr; return ODCIConst.Success; end; member function ODCIAggregateMerge(self IN OUT strcat_type, ctx2 IN strcat_type) 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 strcat (input VARCHAR2) RETURN VARCHAR2 PARALLEL_ENABLE AGGREGATE USING strcat_type;
注意:11gr2和12C上已經摒棄了wm_concat函數,因此要用鏈接函數,建議使用以前介紹的兩種.若是以前老項目使用了這個函數,須要重建該函數或者在當前運行oracle版本中沒有這個函數請看這 「WM_CONCAT」: 標識符無效
with temp as( select 1 grp, 'a1' str from dual union select 1 grp, 'a2' str from dual union select 2 grp, 'b1' str from dual union select 2 grp, 'b2' str from dual union select 2 grp, 'b3' str from dual ) select grp, wmsys.wm_concat(str) from temp group by grp