先看示例代码:

  1. with temp as(
  2. select 'China' nation ,'Guangzhou' city from dual union all
  3. select 'China' nation ,'Shanghai' city from dual union all
  4. select 'China' nation ,'Beijing' city from dual union all
  5. select 'USA' nation ,'New York' city from dual union all
  6. select 'USA' nation ,'Bostom' city from dual union all
  7. select 'Japan' nation ,'Tokyo' city from dual
  8. )
  9. select nation,listagg(city,',') within GROUP (order by city)
  10. from temp
  11. group by nation

这是最基础的用法:

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

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

非常方便。

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

就是over(partition by XXX)

也就是说,在你不实用Group by语句时候,也可以使用LISTAGG函数:

  1. with temp as(
  2. select 500 population, 'China' nation ,'Guangzhou' city from dual union all
  3. select 1500 population, 'China' nation ,'Shanghai' city from dual union all
  4. select 500 population, 'China' nation ,'Beijing' city from dual union all
  5. select 1000 population, 'USA' nation ,'New York' city from dual union all
  6. select 500 population, 'USA' nation ,'Bostom' city from dual union all
  7. select 500 population, 'Japan' nation ,'Tokyo' city from dual
  8. )
  9. select population,
  10. nation,
  11. city,
  12. listagg(city,',') within GROUP (order by city) over (partition by nation) rank
  13. from temp

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

转:Oracle的列转行函数:LISTAGG()的更多相关文章

  1. oracle 的列转行函数 listagg()

    当你的表X中有A,B两列,数据如下 A B a 1 a 2 a 3 b 1 b 2 b 3 想让数据以 a|1|2|3 , b|1|2|3 格式显示可使用listagg() 1.使用listagg() ...

  2. Oracle列转行函数LISTAGG()

    --Oracle列转行函数LISTAGG() with tb_temp as( select 'China' 国家,'Wuhan' 城市 from dual union all select 'Chi ...

  3. Oracle列转行函数Listagg以及pivot查询示例

    简单的Oracle列转行函数Listagg示例: CREATE TABLE tbl_test (catalog VARCHAR(1),product VARCHAR(2),amount NUMBER) ...

  4. Oracle 列转行函数 Listagg()

    这是最基础的用法: LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX) 例: select listagg(oeid,',') within GROUP (ord ...

  5. Oracle列转行函数使用

    一.业务场景 今天需要实现一个table,有一列的效果是:用户姓名A(账号a),用户姓名B(账号b)...这种格式.这就想到oracle的列转行函数vm_concat. 可以用类似这种格式wm_con ...

  6. Oracle列转行函数版本不兼容解决方案

    业务场景 本博客记录一下Oracle列转行函数在Oracle11的一些不兼容问题,vm_concat在一些业务场景是必须的.不过这个函数使用要谨慎,底层实现应该也是group by等等实现的,性能并不 ...

  7. oracle中行转列、列转行函数

    多行转字符串 这个比较简单,用||或concat函数可以实现 select concat(id,username) str from app_user select id||username str ...

  8. oracle列转行 WM_CONCAT LISTAGG

    开发给个SQL说给某个条件时报ORA-22922 代码段: SELECT 袋号, SUM(实际重量) AS 实际重量, SUM(材积重量) AS 材积重量, COUNT(运单号) AS 件数, TO_ ...

  9. Oracle wm_concat(列转行函数)实际使用

    接触到了一个开发需求.其中是要把NC单据表体行的字段拼成一个字符串.例如: id name work age 1 王一 搬运工 20 2 李二 清洁工 21 3 张三 洗脚工 22 出现结果字符串为: ...

随机推荐

  1. 网页解析 -- bs4 和 xpath 的简单使用

    bs4 BeautifulSoup 是一个可以从HTML或XML文件中提取数据的Python库,它的使用方式相对于正则来说更加的简单方便 中文文档:https://beautifulsoup.read ...

  2. nginx 的 proxy_cache 缓存配置

    开头:某个项目涉及到 视频切片下载到本地,然后提供服务给客户端.一个视频有多个文件,存储在本地路径下.当客户端请求的视频在本地没有就会回源, 回源拿到的视频文件再返回给客户端,同时在本地缓存一份,提供 ...

  3. 02vuex-modules

    01===> module的理解:将一个大的系统进行拆分 拆分成若干个细的模块 给个模块都有自己的 state mutations 等属性 这样可以在自己的小模块中进行修改 方便维护 modul ...

  4. undefined reference to symbol 'dlclose@@GLIBC_2.2.5'

    解决方法 在QT工程的.pro文件中添加如下内容: LIBS=-ldl

  5. [C7] Andrew Ng - Sequence Models

    About this Course This course will teach you how to build models for natural language, audio, and ot ...

  6. tensorboard运行使用(排坑)记录一

    首先运行如下代码(路径根据你自己的要求更改) writer = tf.summary.FileWriter("G:/tensorflow/graph", tf.get_defaul ...

  7. Appium 解决微信公众号、小程序切换 webview 后无法定位元素的问题

    如何切换webview进入小程序请参考https://testerhome.com/topics/12003 脚本思路:进入webview后会存在多个handle同Web页签一样,获取所有的handl ...

  8. C语言前置知识汇编基础--总线【地址总线,控制总线,数据总线】练习题

    寻址就是用一个数字表示一个字节存储单元的地址,而不是表示这个存储单元中的内容,所以不用乘以8. 1. 1个CPU的寻址能力为8KB,那么它的地址总线宽度是多少? 8*1024=8192     共有8 ...

  9. JAVA中a++ 和 ++a 的区别

                         

  10. 补充: SpringBoot

    SpringBoot Spring Boot  约定大于配置 Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程. 该框 ...