Oracle列转行函数 Listagg() 语法详解及应用实例「建议收藏」

Oracle列转行函数 Listagg() 语法详解及应用实例「建议收藏」工作中用到一段比较复杂的SQL查询脚本,使用了listagg()函数实现了具有多个值的字段的填充(即,列表聚合,listaggregation(我猜的))。说简单点,listagg()函数可以实现多列记录聚合为一条记录,从而实现数据的压缩、致密化(datadensification)。以下内容转载自http://dacoolbaby.iteye.com/blog/1698957,SQL脚本做了…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

工作中用到一段比较复杂的SQL查询脚本,使用了listagg()函数实现了具有多个值的字段的填充(即,列表聚合,list aggregation(我猜的))。

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

以下内容转载自http://dacoolbaby.iteye.com/blog/1698957,SQL脚本做了部分优化,增加了输出结果。

————————————————————————-

这是一个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)  as Cities
from temp  
group by nation

运行结果:

Oracle列转行函数 Listagg() 语法详解及应用实例「建议收藏」

这是最基础的用法:

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

运行结果:
Oracle列转行函数 Listagg() 语法详解及应用实例「建议收藏」

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

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

—————————————————————————————————————————–

Oracle列转行函数 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.
■ As a single-set aggregate function, LISTAGG operates on all rows and returns a single output row.
■ As a group-set aggregate, the function operates on and returns an output row for each group defined by the GROUP BY clause.
■ 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:
■ The measure_expr can be any expression. Null values in the measure column are ignored.
■ The delimiter_expr designates the string that is to separate the measure values.
This clause is optional and defaults to NULL.
■ 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
110 07-JUN-02 Higgins Gietz; Higgins


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197718.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Tess4J 简单使用入门[通俗易懂]

    Tess4J 简单使用入门[通俗易懂]Tesseract-OCR支持中文识别,并且开源和提供全套的训练工具,是快速低成本开发的首选。而Tess4J则是Tesseract在JavaPC上的应用。在英文和数字识别中性能还是不错的,但是在中文识别中,无论速度还是识别率还是较弱,建议有条件的话,针对场景进行训练,会获得较好结果,本文仅对目前Tess4J的用法进行介绍。———————本文来自jian_cheng_90的CSDN博客,全文地址请点击:https://blog.csdn.net/risky

    2022年6月11日
    27
  • mac mysql改密码_mac系统重置密码

    mac mysql改密码_mac系统重置密码MAC重置MySql密码步骤:1.关闭mysql服务2.打开终端按步骤输入:输入1:cd/usr/local/mysql/bin/输入2:sudo./mysqld_safe–skip-grant-tables3.打开另外一个终端窗口:第一步输入:cd/usr/local/mysql/bin/第二步输入:./mysql第三步输入:FLUSHPRIVILEGES;第四步输入:ALTERUSER‘root’@‘localhost’IDENTIFIEDBY‘1

    2022年10月11日
    4
  • A4988步进驱动

    A4988步进驱动基本知识绕组  常用的步进电机有四根线,1A1B2A2B,1A和1B是一个绕组,2A和2B是一个绕组,用万用表测试1A和1B之间是短路的,2A和2B之间是短路的,1A和1B,2A和2B是等效的。  通常状况下,步进电机可以自由转动(用手可以拧动),1A和1B接在一起的时候,用手拧会感到明显阻力,1A和1B,2A和2B分别接在一起,则阻力更大。步距角  所谓步进电机,就是可以…

    2022年6月29日
    37
  • 如何配置adb环境变量(环境变量在哪打开)

    配置ADB环境变量1.1:打开控制面板>系统和安全>系统>高级系统设置1.2:在系统变量中新建ANDROID_HMOE变量,赋值路径(D:\install\androidSDK)1.3.在系统变量path中添加%ANDROID_HOME%\platform-tools1.4.cmd进入终端验证adb配置是否成功如下图显示为失败如下图显示为成功2.ADB常用指令2.1.查看设备adbdevices这个命令是查看当前连接的设备,连接到计算机的andro

    2022年4月11日
    33
  • volatile关键字及其作用「建议收藏」

    volatile关键字及其作用「建议收藏」概述:本文主要介绍Java语言中的volatile关键字,内容涵盖volatile的保证内存可见性、禁止指令重排等。

    2022年5月31日
    30
  • 为什么必须做密评

    为什么必须做密评密评全称 商用密码应用安全性评估定义 对采用商用密码技术 产品和服务集成建设的网络和信息系统密码应用的合规性 正确性 有效性进行评估 1 密评发展史 2011 年 ZUC 序列算法成为了 4G 移动通信密码算法国际标准 2017 年 非对称算法 SM2 和 SM9 的数字签名算法成为国际标准 2018 年 密码杂凑算法 SM3 成为国际标准 2021 年 2 月 SM9 标识加密算法成为国际标准 6 月 SM4 分组密码算法成为国际标准 10 月 SM9 密钥交换协议成为国际标准我国自主研发的密码算法相继走出国门并受到国际上的认可 国密局

    2025年6月21日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号