WM_CONCAT LISTAGG函数,ORACLE列转行聚合的简单实现
2014-08-29 17:29
435 查看
列转行聚合的简单实现ORACLE WM_CONCAT LISTAGG函数
项目中有个需求,将关联出来的数据以特定的字段将其他某一自动聚合。例子如下:
1 B1
1 B2
1 B3
2 B4
2 B5
3 B6
to
www.2cto.com
1 B1,B2,B3
2 B4,B5
3 B6
拿到手就想到用存储过程,其实还有更简单的方式。那就是oracle 提供的字符串聚合函数。
WM_CONCAT Built-in Function (Not Supported)
If you are not running 11g Release 2, but are running a version of the database where the WM_CONCAT function is present, then it is a zero effort solution as it performs the aggregation for you. It is actually an example of a user defined aggregate function
described below, but
Oracle have done all the work for you.
www.2cto.com
LISTAGG Analystic Function in 11g Release 2
The LISTAGG analytic function was introduced in Oracle 11g Release 2, making it very easy to aggregate strings. The nice thing about this function is it also allows us to order the elements in the concatenated list. If you are using 11g Release 2 you should
use this function for string aggregation.
Example 如下:
CREATE TABLE "TESTAGG"
(
"A" VARCHAR2(20 BYTE),
"B" VARCHAR2(20 BYTE)
)
INSERT INTO "TESTAGG" (A, B) VALUES ('1', 'B1')
INSERT INTO "TESTAGG" (A, B) VALUES ('1', 'B2')
INSERT INTO "TESTAGG" (A, B) VALUES ('1', 'B3')
INSERT INTO "TESTAGG" (A, B) VALUES ('2', 'B4')
INSERT INTO "TESTAGG" (A, B) VALUES ('2', 'B5')
INSERT INTO "TESTAGG" (A, B) VALUES ('3', 'B6')
select * from testAgg
1 B1
1 B2
1 B3
2 B4
2 B5
3 B6
www.2cto.com
select a ,wm_concat (b) as policinams from testagg group by a order by a
select a ,LISTAGG(b,',' ) within group(order by a) as b from testagg group by
1 B1,B2,B3
2 B4,B5
3 B6
项目中有个需求,将关联出来的数据以特定的字段将其他某一自动聚合。例子如下:
1 B1
1 B2
1 B3
2 B4
2 B5
3 B6
to
www.2cto.com
1 B1,B2,B3
2 B4,B5
3 B6
拿到手就想到用存储过程,其实还有更简单的方式。那就是oracle 提供的字符串聚合函数。
WM_CONCAT Built-in Function (Not Supported)
If you are not running 11g Release 2, but are running a version of the database where the WM_CONCAT function is present, then it is a zero effort solution as it performs the aggregation for you. It is actually an example of a user defined aggregate function
described below, but
Oracle have done all the work for you.
www.2cto.com
LISTAGG Analystic Function in 11g Release 2
The LISTAGG analytic function was introduced in Oracle 11g Release 2, making it very easy to aggregate strings. The nice thing about this function is it also allows us to order the elements in the concatenated list. If you are using 11g Release 2 you should
use this function for string aggregation.
Example 如下:
CREATE TABLE "TESTAGG"
(
"A" VARCHAR2(20 BYTE),
"B" VARCHAR2(20 BYTE)
)
INSERT INTO "TESTAGG" (A, B) VALUES ('1', 'B1')
INSERT INTO "TESTAGG" (A, B) VALUES ('1', 'B2')
INSERT INTO "TESTAGG" (A, B) VALUES ('1', 'B3')
INSERT INTO "TESTAGG" (A, B) VALUES ('2', 'B4')
INSERT INTO "TESTAGG" (A, B) VALUES ('2', 'B5')
INSERT INTO "TESTAGG" (A, B) VALUES ('3', 'B6')
select * from testAgg
1 B1
1 B2
1 B3
2 B4
2 B5
3 B6
www.2cto.com
select a ,wm_concat (b) as policinams from testagg group by a order by a
select a ,LISTAGG(b,',' ) within group(order by a) as b from testagg group by
1 B1,B2,B3
2 B4,B5
3 B6
相关文章推荐
- oracle wm_concat函数简单实例(实现行转成用分隔符分隔的一列)
- 最简单的方法实现ORACLE的定时备份
- 一个简单的oracle分页存储过程的实现和调用
- oracle trim函数与字符串替换的内存实现简单猜测
- Oracle_C# 实现 Oracle Text(全文检索)的一个简单例子
- oracle 最简单的学习笔记,增删改查,PLSQL基本语法,游标,函数,存储过程的实现
- 一个简单的oracle分页存储过程的实现和调用
- Oracle自定义聚合函数实现字符串连接的聚合
- oracle 函数 实现简单的加密解密
- ASP.NET文件下载简单实现(也可以通过直接读取数据库 大字段文件,如oracle 中的bolg,long raw 等大字段文件)
- Oracle: wmsys.wm_concat、sys_connect_by_path、自定义函数实现行列转换
- oracle动态游标的简单实现方法
- 简单自适应的GPS Reader程序实现(WinCE,WM)
- 在写rss阅读实现的blog的聚合,简单的聚合功能,发现的一点问题
- Oracle_C# 实现 Oracle Text(全文检索)的一个简单例子
- 一个简单的oracle分页存储过程的实现和调用
- 一个简单的oracle分页存储过程的实现和调用
- 一个简单的oracle分页存储过程的实现和调用
- Oracle逗号分隔列转行实现方法
- Oracle List 功能 《=》 Oracle: wmsys.wm_concat、sys_connect_by_path、自定义函数实现行列转换