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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 批处理、VBS实现自动设置IP、默认网关、DNS、WINS、IE代理!(二)

    批处理、VBS实现自动设置IP、默认网关、DNS、WINS、IE代理!(二)四、自动设置IE代理美国IE代理.bat@echoofftitle自动设置代理服务器echo自动设置代理服务器remecho正在清空代理服务器设置……remregadd”HKCU\Software\Microsoft\Windows\CurrentVersion\InternetSettings”/vProxyEnable/tREG_DWOR…

    2022年6月21日
    40
  • 互斥锁、自旋锁、读写锁、悲观锁、乐观锁的应用场景[通俗易懂]

    互斥锁、自旋锁、读写锁、悲观锁、乐观锁的应用场景[通俗易懂]最常用的就是互斥锁,当然还有很多种不同的锁,比如自旋锁、读写锁、乐观锁等,不同种类的锁自然适用于不同的场景。如果选择了错误的锁,那么在一些高并发的场景下,可能会降低系统的性能,这样用户体验就会非常差了。所以,为了选择合适的锁,我们不仅需要清楚知道加锁的成本开销有多大,还需要分析业务场景中访问的共享资源的方式,再来还要考虑并发访问共享资源时的冲突概率。

    2022年8月12日
    6
  • Java: Hook技术[通俗易懂]

    Java: Hook技术[通俗易懂]一、什么是Hook  Hook翻译成中文就是勾子的意思,在java中它表示在事件到达终点前进行拦截或监控的一种行为。 二、Hook的实现实现hook我们必须要知道java的反射和动态代理。 1、反射  反射是java的查看、检测、修改自身的一种行为。  在编译阶段,编译器将我们编写的java文件编译成.class文件。而在运行期,jvm又将.class文件通过…

    2022年5月9日
    146
  • 40个容易上瘾的HTML5网页游戏,总有一款适合你[通俗易懂]

    40个容易上瘾的HTML5网页游戏,总有一款适合你[通俗易懂]我记得姐姐家的孩子在刚刚才学会走路,说话还不能完整的时候就已经能自己用小手点出小游戏的网站来一个人自娱自乐。我一直在想这一代跟着计算机一起茁壮成长的孩子会不会也和美国那一代人一样,出现9岁的黑客和计算机天才。但是并不是信息的成长就能让教育同步。很多时候我们会发现教育在发展的大环境中并没有什么创新的思考。不管怎么说,我们还是需要小盆友们能有足够的想象力。不要被束缚!今天分享的是40个HTML5的网页

    2022年5月24日
    32
  • cubieboard mysql_Cubieboard开发笔记[通俗易懂]

    cubieboard mysql_Cubieboard开发笔记[通俗易懂]原创作品,转载请注明出处,谢谢!写在前面:亲测可用的搭建流程,但是此流程是针对32位系统的,如果您是64位系统,请勿采用本博文的方式。我搭这个环境居然折腾了一周时间,本来用32位系统好好的,但是官网非要推荐我用64位系统。因为最终打包必须在64位环境下实现。于是乎我一直在不断换系统版本,希望能按照对方工程师所描述的状况来实现,但是我将Error发给他时,他也无计可施。因而还是回到最熟悉的方式进行编…

    2022年7月22日
    10
  • 数组去重的5种方法[通俗易懂]

    数组去重的5种方法[通俗易懂]双重for循环去重 原理两两比较如果相等的话就删除第二个 例如:11132124 先让第一个1即arr[0]与后面的一个个比较如果后面的值等于arr[0]删除后面的值 第一次结束后的结果是13224删除了后面所有的1 同理第二次第三会删除与自己相同的元素详细介绍看代码注释1 2functionnoRepeat1(arr){…

    2022年6月22日
    53

发表回复

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

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