Oracle各种连接函数总结1.前⾔
Oracle可⽤连接函数会介绍以下⼏个
1. Oracle列转⾏函数 Listagg()
2. strcat()
3. wmsys.wm_concat()
2.Oracle列转⾏函数 Listagg()
2.1最基础的⽤法:
LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)
⽤法就像聚合函数⼀样,通过Group by语句,把每个Group的⼀个字段,拼接起来。
其中LISTAGG函数第⼀个参数为要拼接的字段,第⼆个参数为⽤什么字符串进⾏连接
eg : listagg(city,’,’)
后⾯GROUP()中为对连接之后的⾏数据按什么字段进⾏排序
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
运⾏结果:
2.2同样是聚合函数,还有⼀个⾼级⽤法:
就是over(partition by XXX)
也就是说,在你不实⽤Group by语句时候,也可以使⽤LISTAGG函数:
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
oracle切割字符串函数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
运⾏结果:
2.3总结
listagg()函数⽀持最低版本需要Oracle 11gR2,查询⾃⼰Oracle版本sql如下,
SELECT v.VERSION FROM v$instance v;
如果版本低于11g,查询会报错 [未到要求的 FROM 关键字]
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
结果为:
注意:如果执⾏报错,报错内容为 strcat标识符⽆效,则你的版本缺少这个函数,⼿动执⾏下⾯的strcat源码即可
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;
4.wmsys.wm_concat()
注意:11gr2和12C上已经摒弃了wm_concat函数,所以要⽤连接函数,建议使⽤之前介绍的两种.如果之前⽼项⽬使⽤了这个函数,需要重建该函数或者在当前运⾏oracle版本中没有这个函数请看这
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
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论