`
dacoolbaby
  • 浏览: 1253719 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Oracle 列转行函数 Listagg()

 
阅读更多

这是一个Oracle的列转行函数:LISTAGG()

 

先看示例代码:

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)
from temp
group by nation

 这是最基础的用法:

LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)

 

用法就像聚合函数一样,通过Group by语句,把每个Group的一个字段,拼接起来。

非常方便。

 

同样是聚合函数,还有一个高级用法:

就是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

 

总结:LISTAGG()把它当作SUM()函数来使用就可以了。

 

 

 

 

分享到:
评论
5 楼 pktangshao 2017-03-08  
a_bun 写道
iijjll 写道
使用wmsys.wm_concat()函数也行

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,wmsys.wm_concat(city)
from temp 
group by nation

wm_concat函数慎用,性能很差,会产生很多block的

wm_concat实在是少用为好,今天是遇到了,SQL中执行很快,到了存储过程超级慢.慢10倍以上.找了好久才找到原来是这个函数这里慢
谢谢博主提供的这个LISTAGG函数.
本来我在最后GROUP的,看了楼主的写法改造成OVER(PARTITAION BY)了,在同样的数据情况下比在最后GROUP快了6秒.
4 楼 a_bun 2016-08-11  
iijjll 写道
使用wmsys.wm_concat()函数也行

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,wmsys.wm_concat(city)
from temp 
group by nation

wm_concat函数慎用,性能很差,会产生很多block的
3 楼 iijjll 2015-01-20  
使用wmsys.wm_concat()函数也行

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,wmsys.wm_concat(city)
from temp 
group by nation
2 楼 dacoolbaby 2013-01-15  
Eric.Yan 写道
listagg作为分析函数(即实用over(partition by XXX)关键字)用到的很少,楼主的sql:
select population,  
       nation,  
       city,  
       listagg(city,',') within GROUP (order by city) over (partition by nation) rank  
from temp;

意思是选出跟当前city一样的属于同一个nation的city,合并显示;

你说的没错。
使用或不使用over(partition by xxx)根据你的SQL的复杂度来进行判断。
1 楼 Eric.Yan 2013-01-15  
listagg作为分析函数(即实用over(partition by XXX)关键字)用到的很少,楼主的sql:
select population,  
       nation,  
       city,  
       listagg(city,',') within GROUP (order by city) over (partition by nation) rank  
from temp;

意思是选出跟当前city一样的属于同一个nation的city,合并显示;

相关推荐

    【Oracle】LISTAGG函数的使用.pdf

    【Oracle】LISTAGG函数的使用.pdf

    Oracle函数之LISTAGG

    近在学习的过程中,发现一个挺有意思的函数,它可实现对列值的拼接。下面我们来看看其具体用法。  用法:  对其作用,官方文档的解释如下:  For a specified measure, LISTAGG orders data within each ...

    Oracle10g自定义聚合函数(字符串拼接)

    * 自定义聚合函数 wmsys.wm_concat 替换办法 * 超大字符串拼接,单个字符串4000、分隔符100... * Oracle11g Release2版本引入了LISTAGG 函数,使得聚集连接字符串变得很容易。并且允许使用我们指定连接串中的字段顺序

    oracle材料

    含listagg函数 (行列转换) ,Oracle-SQL-Developer-使用简要说明,oracle导入导出语句,Oracle远程登录,rownum分组排序,wm_concat列转行

    ACCESS 分组合并

    由于ACCESS 没有oracle的listagg函数,也没有sql server这种 for xml path 这种, 要实现分组合并需要自定义一个函数,理解了 for xml path 这个就很好理解了。

    oracle收集

    oracle日常收集内容,平时工作积累listagg行转列

    Oracle SQL高级编程(资深Oracle专家力作,OakTable团队推荐)--随书源代码

    作者通过总结各自多年的软件开发和教学培训经验,与大家分享了掌握Oracle SQL所独有的丰富功能的技巧所在,内容涵盖SQL执行、联结、集合、分析函数、子句、事务处理等多个方面。读者可以学习到以下几个方面的技巧:...

    oracle 10g 11g 12c 19c 21c 23c 重建wm-concat函数脚本

    oracle 10g 11g 12c 19c 21c 23c 重建wm_concat函数脚本 varchar类型clob类型报错不用listagg ORA-01790: 表达式必须具有与对应表达式相同的数据类型 ORA-00904: “WM_CONCAT“: invalid identifier 解决 owmaggrb....

    12C开始_wm_concat函数.sql

    Oracle从12C版本开始,不支持wm_concat函数,我们可以采取的办法有使用listagg函数代替wm_concat函数,或者为了减小修改程序的工作量,可以通过手工创建wm_concat函数来解决这个问题。

    SQL袖珍参考手册(第3版)

    Functions new to Oracle, such as LISTAGG, NTH_VALUE, and more PostgreSQL’s support of recursive WITH and some window functions DB2 syntax and datatypes, some compatible with Oracle MySQL features ...

    Oracle SQL实用讲解,最基本最实用的相关讲解

    WITH –-小九九算法 t_base AS (SELECT LEVEL AS lv FROM dual CONNECT BY LEVEL ), ...SELECT listagg(t_join.text, ' ') within GROUP(ORDER BY t_join.lv_b) AS m99 FROM t_join GROUP BY t_join.lv_a;

    数据结构————二差排序树标准写法

    二差排序树树,关于数据结构的二差排序树,仅供参考!!!!!!!!!!!!!!!!!!!

    SQL删除多列语句的写法

    最近在写SQL过程中发现需要对一张表结构作调整(此处是SQL Server),其中需要删除多列,由于之前都是一条SQL语句删除一列,于是猜想是否可以一条语句同时删除多列,如果可以,怎么写法? 第一次猜想如下(注意:...

    C#俄罗斯方块程序设计

    文档说明了基本的俄罗斯方块的设计过程以及代码!

    Access数据库-List列表框控件应用

    Access数据库-List列表框控件应用

    【SQL 必知必会】 性能篇 01. 如何考虑数据库调优

    1. 数据库调优的对象与目标 对象:整个DBMS,包括 SQL查询,数据库部署配置,架构等 目标:数据库响应时间快,吞吐量更大。 2. 调优考虑的维度有哪些 用户反馈:是最及时的,是最直接的。 日志分析:通过数据库日志...

    data_access:使用内存数据库的可扩展、低延迟、高并发数据访问层实现

    要添加到内置函数列表中的 VoltDB 存储过程,例如:像 group_concat(又名 listagg)这样的 UDF 像爆炸的 UDTF 通过 JDBC 动态执行 DDL 将多个 SQL 语句绑定到一个事务中创建表插入少量记录(使用 Insert into ...

    psftpp:PA和PeopleSoft

    PSFT_PP 修改后的PeopleSoft现金会计解决方案PeopleSoft中的收入确认如何在PeopleSoft中创建新的业务单元如何在PSQuery中使用LISTAGG什么是PS Financial GatewayPeopleCode / SQL可能会很方便地在Integration Broker...

Global site tag (gtag.js) - Google Analytics