详解

listagg()函数可以实现多列记录聚合为一条记录,从而实现数据的压缩、致密化(data densification)

基本用法

像聚合函数一样,通过Group by语句,把每个Group的一个字段,拼接起来
LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)

示例:
with temp as(
select '中国' nation ,'江苏' city from dual union all
select '中国' nation ,'上海' city from dual union all
select '中国' nation ,'北京' city from dual union all
select '美国' nation ,'纽约' city from dual union all
select '美国' nation ,'波士顿' city from dual union all
select '日本' nation ,'东京' city from dual
)
select nation,listagg(city,',') within GROUP (order by city)  as Cities
from temp
group by nation
查询结果

高级用法

listagg(XXX,’,’) within GROUP (order by XXX) over (partition by XXX) rank

示例
with temp as(
select 500 population, '中国' nation ,'江苏' city from dual union all
select 1500 population, '中国' nation ,'上海' city from dual union all
select 500 population, '中国' nation ,'北京' city from dual union all
select 1000 population, '美国' nation ,'纽约' city from dual union all
select 500 population, '美国' nation ,'波士顿' city from dual union all
select 500 population, '日本' nation ,'东京' city from dual
)
select population,
nation,
city,
listagg(city,',') within GROUP (order by city) over (partition by nation) rank
from temp
运行结果

Oracle Database SQL Language Reference上有关listagg()函数的描述如下:


Purpose
For a specified measure, LISTAGG orders data within each group specified in the ORDER BY clause and then concatenates the values of the measure column.

  1. As a single-set aggregate function, LISTAGG operates on all rows and returns a single output row.
  2. As a group-set aggregate, the function operates on and returns an output row for each group defined by the GROUP BY clause.
  3. As an analytic function, LISTAGG partitions the query result set into groups based on one or more expression in the query_partition_clause.
    The arguments to the function are subject to the following rules:
  4. The measure_expr can be any expression. Null values in the measure column are ignored.
  5. The delimiter_expr designates the string that is to separate the measure values.
    This clause is optional and defaults to NULL.
  6. The order_by_clause determines the order in which the concatenated values are returned. The function is deterministic only if the ORDER BY column list achieved
    unique ordering.
    The return data type is RAW if the measure column is RAW; otherwise the return value is VARCHAR2.
    Aggregate Examples
    The following single-set aggregate example lists all of the employees in Department 30 in the hr.employees table, ordered by hire date and last name:
    SELECT LISTAGG(last_name, '; ')
    WITHIN GROUP (ORDER BY hire_date, last_name) “Emp_list”,
    MIN(hire_date) “Earliest”
    FROM employees
    WHERE department_id = 30;
    Emp_list Earliest

Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares 07-DEC-02
The following group-set aggregate example lists, for each department ID in the hr.employees table, the employees in that department in order of their hire date:

SELECT department_id “Dept.”,
LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hire_date) “Employees”
FROM employees
GROUP BY department_id
ORDER BY department_id;
Dept. Employees


10 Whalen
20 Hartstein; Fay
30 Raphaely; Khoo; Tobias; Baida; Himuro; Colmenares
40 Mavris
50 Kaufling; Ladwig; Rajs; Sarchand; Bell; Mallin; Weiss; Davie
s; Marlow; Bull; Everett; Fripp; Chung; Nayer; Dilly; Bissot
; Vollman; Stiles; Atkinson; Taylor; Seo; Fleaur; Matos; Pat
el; Walsh; Feeney; Dellinger; McCain; Vargas; Gates; Rogers;
Mikkilineni; Landry; Cabrio; Jones; Olson; OConnell; Sulliv
an; Mourgos; Gee; Perkins; Grant; Geoni; Philtanker; Markle
60 Austin; Hunold; Pataballa; Lorentz; Ernst
70 Baer
. . .
Analytic Example
The following analytic example shows, for each employee hired earlier than September 1, 2003, the employee’s department, hire date, and all other employees in
that department also hired before September 1, 2003:
SELECT department_id “Dept”, hire_date “Date”, last_name “Name”,
LISTAGG(last_name, '; ') WITHIN GROUP (ORDER BY hire_date, last_name)
OVER (PARTITION BY department_id) as “Emp_list”
FROM employees
WHERE hire_date < ‘01-SEP-2003’
ORDER BY “Dept”, “Date”, “Name”;
Dept Date Name Emp_list


30 07-DEC-02 Raphaely Raphaely; Khoo
30 18-MAY-03 Khoo Raphaely; Khoo
40 07-JUN-02 Mavris Mavris
50 01-MAY-03 Kaufling Kaufling; Ladwig
50 14-JUL-03 Ladwig Kaufling; Ladwig
70 07-JUN-02 Baer Baer
90 13-JAN-01 De Haan De Haan; King
90 17-JUN-03 King De Haan; King
100 16-AUG-02 Faviet Faviet; Greenberg
100 17-AUG-02 Greenberg Faviet; Greenberg
110 07-JUN-02 Gietz Gietz; Higgins
6-AUG-02 Faviet Faviet; Greenberg
100 17-AUG-02 Greenberg Faviet; Greenberg
110 07-JUN-02 Gietz Gietz; Higgins
110 07-JUN-02 Higgins Gietz; Higgins

Oracle列转行函数 Listagg()详解相关推荐

  1. Oracle 列转行函数 Listagg()

    本文来源于:dacoolbaby 的   <Oracle 列转行函数 Listagg()> 这是一个Oracle的列转行函数:LISTAGG() 1 with temp as( 2 sel ...

  2. oracle列转行查询,Oracle列转行函数Listagg以及pivot查询示例

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

  3. Oracle列转行函数listagg和wm_concat

    listagg函数 其函数在Oracle 11g 版本中推出,对分组后的数据按照一定的排序进行字符串连接. //用法如下 分隔符号可以自定义 listagg (字段, ',') WITHIN GROU ...

  4. oracle列转行函数listagg和vm_concat

    wm_concat 和listagg 函数都可以实现对(单个或组合)列的合并,也可以看成是对某一列的"SUM",这俩个函数功能相同,listagg是在11.2的版本中才首次出现的, ...

  5. Oracle列转行函数LISTAGG() WITHIN GROUP ()的使用方法

    前言:最近在写一些比较复杂的SQL,是一些统计分析类的,动不动就三四百行,也是首次写那么长的SQL,有用到一些奇形怪状的SQL函数,在这里结合网上的例子做一些笔记,以后用到不记得用法可以翻出来看! 1 ...

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

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

  7. oracle数据列转行排序,oracle 列转行函数 WMSYS.WM_CONCAT 排序不规则处理

    业务中做报表,需要将一列列数据汇总成一行,然后汇总,如下: 需要将每个产品进行汇总,通过ichartjs进行展示,图表中需要数据的顺序是: var data = [ { name : '产品1', v ...

  8. oracle pivot 列转行,Oracle 列转行函数pivot

    作为数据库应用开发人员,我们有很大的精力应付在处理各种各样的数据类型,展现各种维度的报表上面. [url=]行转列[/url].列转行是我们经常会遇到的"诡异"需求.标准SQL没有 ...

  9. oracle中split的使用方法,Oracle 自定义split 函数实例详解

    Oracle 自定义split 函数 Oracle没有提供split函数,但可以自己建立一个函数实现此功能.比如"abc defg  hijkl   nmopqr     stuvw  xy ...

最新文章

  1. 使用 JavaCSV api 读取和写入 csv 文件
  2. 0与1c语言编译,C语言程序设计(07776-1)第11章编译预处理课案.ppt
  3. 【BLE MIDI】MIDI 文件格式分析 ( FF 03 轨道名称 | FF 51 03 四分音符时长 )
  4. 根据方法名执行方法的例子
  5. [转载]使用.net 2003中的ngen.exe编译.net程序
  6. 数学建模 层次分析法
  7. 别以为程序员的工作就是写代码
  8. 我真out了,高端人士都这样玩儿?
  9. 安装软件后,在postinst中执行ldconfig无效?
  10. NOIP2013Day1
  11. iPhone设备字体详解
  12. 用计算机新字库打出的文字,为什么用五笔打字有很多字打不出来(GBK和GB2312字库的区别)...
  13. Windows Server2012搭建邮件服务器
  14. 如何复制Google云端硬盘文件夹
  15. 为什么沿着梯度相反的方向更新参数
  16. java12/13新特性
  17. ionic3 版本更新
  18. 浅析Windows2000/XP服务与后门技术(经典后门T-cmd的源码)
  19. 《AutoCAD 2014中文版实用教程》一一1.2 标题栏
  20. 基于Arduino 开发 MAX30102 LM35 SSD1306 观察血氧、心率和温度血氧仪

热门文章

  1. CUDA笔记-卷积计算
  2. java中判断list是否为空的方法区别
  3. lua--函数深入:闭合函数,局部函数,尾调用
  4. ubuntu下和进入根目录 查找etc等文件
  5. 活用Excel高级筛选解决实际问题
  6. 成都百择电商:抖音小店有哪些引流方法?
  7. 【HTML5】响应式图片
  8. ZuulFilter方法简介
  9. 现代化大学的标配理念之一是学术自由
  10. Java:(练习)一、基于面向对象方法的简单银行系统