oracle的连接函数,Oracle各种连接函数总结
1.前言
Oracle可用連接函數(shù)會(huì)介紹以下幾個(gè)
Oracle列轉(zhuǎn)行函數(shù) Listagg()
strcat()
wmsys.wm_concat()
2.Oracle列轉(zhuǎn)行函數(shù) Listagg()
2.1最基礎(chǔ)的用法:
LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)
用法就像聚合函數(shù)一樣,通過Group by語句,把每個(gè)Group的一個(gè)字段,拼接起來。
其中LISTAGG函數(shù)第一個(gè)參數(shù)為要拼接的字段,第二個(gè)參數(shù)為用什么字符串進(jìn)行連接
eg : listagg(city,’,’)
后面GROUP()中為對(duì)連接之后的行數(shù)據(jù)按什么字段進(jìn)行排序
eg : order by city
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
運(yùn)行結(jié)果:
2.2同樣是聚合函數(shù),還有一個(gè)高級(jí)用法:
就是over(partition by XXX)
也就是說,在你不實(shí)用Group by語句時(shí)候,也可以使用LISTAGG函數(shù):
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
運(yùn)行結(jié)果:
2.3總結(jié)
listagg()函數(shù)支持最低版本需要Oracle 11gR2,查詢自己Oracle版本sql如下,
SELECT v.VERSION FROM v$instance v;
如果版本低于11g,查詢會(huì)報(bào)錯(cuò) [未找到要求的 FROM 關(guān)鍵字]
3.strcat()
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
結(jié)果為:
注意:如果執(zhí)行報(bào)錯(cuò),報(bào)錯(cuò)內(nèi)容為 strcat標(biāo)識(shí)符無效,則你的版本缺少這個(gè)函數(shù),手動(dòng)執(zhí)行下面的strcat源碼即可
ORACLE 字符串聚合函數(shù) 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;
4.wmsys.wm_concat()
注意:11gr2和12C上已經(jīng)摒棄了wm_concat函數(shù),所以要用連接函數(shù),建議使用之前介紹的兩種.如果之前老項(xiàng)目使用了這個(gè)函數(shù),需要重建該函數(shù)或者在當(dāng)前運(yùn)行oracle版本中沒有這個(gè)函數(shù)請(qǐng)看這?“WM_CONCAT”: 標(biāo)識(shí)符無效
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
總結(jié)
以上是生活随笔為你收集整理的oracle的连接函数,Oracle各种连接函数总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑系统怎么进入磁盘管理(怎样进入磁盘管
- 下一篇: 蓝桥杯7届c语言 c组答案,第七届蓝桥杯