网站后台管理代码,友链交易平台,温州网络问政,网站只做静态页面安全受到影响1.前言Oracle可用连接函数会介绍以下几个Oracle列转行函数 Listagg()strcat()wmsys.wm_concat()2.Oracle列转行函数 Listagg()2.1最基础的用法#xff1a;LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)用法就像聚合函数一样#xff0c;通过Group by语句#xff0c;把每个Gr…1.前言Oracle可用连接函数会介绍以下几个Oracle列转行函数 Listagg()strcat()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 citywith temp as(select China nation ,Guangzhou city from dual union allselect China nation ,Shanghai city from dual union allselect China nation ,Beijing city from dual union allselect USA nation ,New York city from dual union allselect USA nation ,Bostom city from dual union allselect Japan nation ,Tokyo city from dual)select nation,listagg(city,,) within GROUP (order by city) as Citiesfrom tempgroup by nation运行结果2.2同样是聚合函数还有一个高级用法就是over(partition by XXX)也就是说在你不实用Group by语句时候也可以使用LISTAGG函数with temp as(select 500 population, China nation ,Guangzhou city from dual union allselect 1500 population, China nation ,Shanghai city from dual union allselect 500 population, China nation ,Beijing city from dual union allselect 1000 population, USA nation ,New York city from dual union allselect 500 population, USA nation ,Bostom city from dual union allselect 500 population, Japan nation ,Tokyo city from dual)select population,nation,city,listagg(city,,) within GROUP (order by city) over (partition by nation) rankfrom 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 allselect China nation ,Shanghai city from dual union allselect China nation ,Beijing city from dual union allselect USA nation ,New York city from dual union allselect USA nation ,Bostom city from dual union allselect Japan nation ,Tokyo city from dual)select nation,strcat(city) from tempgroup by nation结果为:注意:如果执行报错,报错内容为 strcat标识符无效,则你的版本缺少这个函数,手动执行下面的strcat源码即可ORACLE 字符串聚合函数 strCatcreate 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 isstatic function ODCIAggregateInitialize(sctx IN OUT strcat_type) return number isbeginsctx : strcat_type(,,);return ODCIConst.Success;end;member function ODCIAggregateIterate(self IN OUT strcat_type, value IN VARCHAR2) return number isbeginif self.currentstr is null thenself.currentstr : value;elseself.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 isbeginreturnValue : self.currentstr;return ODCIConst.Success;end;member function ODCIAggregateMerge(self IN OUT strcat_type, ctx2 IN strcat_type) return number isbeginif ctx2.currentstr is null thenself.currentstr : self.currentstr;elsif self.currentstr is null thenself.currentstr : ctx2.currentstr;elseself.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版本中没有这个函数请看这 “WM_CONCAT”: 标识符无效with temp as(select 1 grp, a1 str from dualunionselect 1 grp, a2 str from dualunionselect 2 grp, b1 str from dualunionselect 2 grp, b2 str from dualunionselect 2 grp, b3 str from dual)select grp, wmsys.wm_concat(str)from tempgroup by grp