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
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版本中没有这个函数请看这 “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

Oracle各种连接函数总结的更多相关文章

  1. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  2. oracle 自定义 聚合函数

    Oracle自定义聚合函数实现字符串连接的聚合   create or replace type string_sum_obj as object ( --聚合函数的实质就是一个对象      sum ...

  3. [转]ORACLE日期时间函数大全

    本文转自:http://www.cnblogs.com/chuncn/archive/2009/04/29/1381282.html ORACLE日期时间函数大全 TO_DATE格式(以时间: ::2 ...

  4. ORACLE SQL单行函数(三)【weber出品必属精品】

    16.L:代表本地货币符,这个和区域有关.这个时候我们想来显示一下人民币的符号:¥ $ vi .bash_profile ---写入如下内容: export NLS_LANG='SIMPLIFIED ...

  5. Oracle/Mysql/SqlServer函数区别

    mysql日期和时间格式转换 Linux scp 使用详解 Oracle/Mysql/SqlServer函数区别 2011-07-01 12:34:36|  分类: Mysql技术 |  标签:mys ...

  6. Oracle经常使用函数

    Oracle经常使用函数 --TRUNC,TO_DATE,TO_CHAR,TO_NUMBER, SUBSTR,REPLACE.NVL .TRIM,wm_concat,upper, lower,leng ...

  7. Oracle内置函数

    单行函数:当查询表或试图时每行都能返回一个结果,可用于select,where,order by等子句中. 对于没有目标的select查询用dual表,这个表时真实存在的,每个用户都可以读取. 单行函 ...

  8. Oracle时间日期函数

    ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02   13:45:25为例)           Year:              yy two digits 两位年 ...

  9. [转载]ORACLE日期时间函数大全

    ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02   13:45:25为例)           Year:              yy two digits 两位年 ...

随机推荐

  1. Redis Streams 介绍

    Stream是Redis 5.0版本引入的一个新的数据类型,它以更抽象的方式模拟日志数据结构,但日志仍然是完整的:就像一个日志文件,通常实现为以只附加模式打开的文件,Redis流主要是一个仅附加数据结 ...

  2. CandyCrush 糖果传奇源码+素材+教程

    在这里你将深入学习C#语言和Unity开发游戏的技术.在游戏项目开发中深入学习并掌握Unity开发中的刚体,模型等等 共14讲,TS格式,大小395MB 共14讲,TS格式,大小395MB 扫码时备注 ...

  3. Awesome Knowledge-Distillation

    Awesome Knowledge-Distillation 2019-11-26 19:02:16 Source: https://github.com/FLHonker/Awesome-Knowl ...

  4. pg数据库中时间查询的方式

    方法一:select * from user_info where create_date>= '2015-07-01' and create_date < '2015-08-15'; 方 ...

  5. CentOS7.6 X64搭建mysql服务

    1.官方安装文档 http://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 2.下载 Mysql yum包 http://dev.mysql.co ...

  6. Linux如何将未分配的硬盘挂载出来

    情景说明 客户给了几台服务器,说500G硬盘,但到手操作的时候,使用命令查看,发现只有不到200的硬盘 [root@localhost ~]# df -h Filesystem Size Used A ...

  7. ubuntu apt-get 安装jdk

    参考地址:https://blog.csdn.net/ywueoei/article/details/80335799 . https://blog.csdn.net/inhumming/articl ...

  8. OSI七层模型、TCP/IP五层模型

    OSI网络互连的七层框架:物理层.数据链路层.网络层.传输层.会话层.表示层.应用层: <1>应用层 OSI参考模型中最靠近用户的一层,是为计算机用户提供应用接口,为用户直接提供各种网络服 ...

  9. Java 各种时间日期相关的操作

    目录 1.获取当前时间的时间戳 1.1.时间进制 1.2.获取毫秒级时间戳 1.3.获取纳秒级时间戳 2.java.util包 2.1.Data 2.2.Calendar 3.java.time包 3 ...

  10. LeetCode 101. Symmetric Tree(镜像树)

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...