LISTAGG

 Syntax 语法

listagg_overflow_clause::=

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.

对于指定的度量, LISTAGGORDER  BY 子句中指定的每个组中的数据排序,然后连接度量列的值。

•  As a single-set aggregate function, LISTAGG operates on all rows and returns a single output row.

•  作为单个集合聚合函数,LISTAGG对所有行进行操作并返回单个输出行。

•  As a group-set aggregate, the function operates on and returns an output row for each group defined by the GROUP BY clause.

•  作为组集聚合,函数对由group by子句定义的每个组进行操作并返回一个输出行。

•  As an analytic function, LISTAGG partitions the query result set into groups based on one or more expression in the query_partition_clause.

•  作为一个分析函数,LISTAGG根据query_partition_clause中的一个或多个表达式将查询结果集划分为多个组。

The arguments to the function are subject to the following rules:

函数的参数受以下规则约束:

• The ALL keyword is optional and is provided for semantic clarity.

• ALL关键字是可选的,用于语义清晰。

• The measure_expr is the measure column and can be any expression. Null values in the measure column are ignored.

• 度量表达式是度量列,可以是任何表达式。忽略度量列中的空值。

• The delimiter designates the string that is to separate the measure column values. This clause is optional and defaults to NULL.

• 分隔符指定用于分隔度量列的字符串。此子句是可选的,默认为NULL

If measure_expr is of type RAW, then the delimiter must be of type RAW. You can achieve this by specifying the delimiter as a character string that can be implicitly converted to RAW, or by explicitly converting the delimiter to RAW, for example, using the UTL_RAW.CAST_TO_RAW function.

如果measure_expr的类型为raw,则分隔符的类型必须为RAW。您可以通过将分隔符指定为可以隐式转换为RAW的字符串,或者通过将分隔符显式转换为RAW来实现此目的,例如,使用UTL_RAW.CAST_TO_RAW函数。

• 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.

order_by_clause确定返回连接值的顺序。仅当ORDER BY 列列表实现唯一排序时,函数才具有确定性。

If the measure column is of type RAW, then the return data type is RAW. Otherwise, the return data type is VARCHAR2.

如果度量值列的类型为RAW,则返回数据类型为RAW。否则,返回数据类型为VARCHAR2

The maximum length of the return data type depends on the value of the MAX_STRING_SIZE initialization parameter. If MAX_STRING_SIZE = EXTENDED, then the maximum length is 32767 bytes for the VARCHAR2 and RAW data types. If MAX_STRING_SIZE = STANDARD, then the maximum length is 4000 bytes for the VARCHAR2 data type and 2000 bytes for the RAW data type. A final delimiter is not included when determining if the return value fits in the return data type.

返回数据类型的最大长度取决于MAX_STRING_SIZE初始化参数的值。如果MAX_STRING_SIZE = EXTENDED,则VARCHAR2和原始数据类型的最大长度为32767字节。如果MAX_STRING_SIZE = STANDARD,则VARCHAR2数据类型的最大长度为4000字节,原始数据类型的最大长度为2000字节。在确定返回值是否适合返回数据类型时,不包括最终分隔符。

listagg_overflow_clause

This clause controls how the function behaves when the return value exceeds themaximum length of the return data type.

此子句控制当返回值超过返回数据类型的最大长度时函数的行为。

ON OVERFLOW ERROR If you specify this clause, then the function returns anORA-01489 error. This is the default.

ON OVERFLOW ERROR 如果指定此子句,则函数返回anora-01489错误。这是默认设置。

ON OVERFLOW TRUNCATE If you specify this clause, then the function returns a truncated list of measure values.

ON OVERFLOW TRUNCATE 如果您指定了这个子句,那么函数将返回被取消标记的度量值列表。

• The truncation_indicator designates the string that is to be appended to the truncated list of measure values. If you omit this clause, then the truncation indicator is an ellipsis (...).

truncation_indicator 指定要附加到被截断的度量值列表中的字符串。如果省略此子句,则截断指示符是省略号(…)。

If measure_expr is of type RAW, then the truncation indicator must be of type RAW. You can achieve this by specifying the truncation indicator as a character string that can be implicitly converted to RAW, or by explicitly converting the truncation indicator to RAW, for example, using the UTL_RAW.CAST_TO_RAW function.

如果measure_expr的类型为RAW,则截断指示器的类型必须为RAW。您可以通过将截断指示符指定为可以隐式转换为RAW的字符串,或者通过将截断指示符显式转换为RAW来实现这一点,例如,使用UTL_RAW.CAST_TO_RAW函数。

• If you specify WITH COUNT, then after the truncation indicator, the database appends the number of truncated values, enclosed in parentheses. In this case, the database truncates enough measure values to allow space in the return value for a final delimiter, the truncation indicator, and 24 characters for the number value enclosed in parentheses.

• 如果指定WITH COUNT,那么在截断指示符之后,数据库将附加截断值的数量,并用括号括起来。在这种情况下,数据库会截断足够的度量值,以便在返回值中为最后一个分隔符、截断指示符留出空间,并为括在括号中的数值留出24个字符。

• If you specify WITHOUT COUNT, then the database omits the number of truncated values from the return value. In this case, the database truncates enough measure values to allow space in the return value for a final delimiter and the truncation indicator.

• 如果指定WITHOUT COUNT,则数据库会从返回值中省略截断值的数量。在这种情况下,数据库截断足够的度量值,以便在返回值中为最后一个分隔符和截断指示器留出空间。

If you do not specify WITH COUNT or WITHOUT COUNT, then the default is WITH COUNT.

如果不指定WITH COUNTWITHOUT COUNT,则默认值为WITH COUNT

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:

以下单个集合聚合示例列出了hr.employees表中30部门中按雇用日期和姓氏排序的所有员工:

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:

以下组为hr.employees表中的每个部门ID按雇用日期的顺序设置聚合示例列表:

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
. . .

The following example is identical to the previous example, except it contains the ON OVERFLOW TRUNCATE clause. For the purpose of this example, assume that the maximum length of the return value is an artificially small number of 200 bytes. Because the list of employees for department 50 exceeds 200 bytes, the list is truncated and appended with a final delimiter '; ', the specified truncation indicator '...', and the number of truncated values '(23)'.

下面的示例与前面的示例相同,只是它包含ON OVERFLOW TRUNCATE子句。在本例中,假设返回值的最大长度是人为的200字节的小数字。由于部门50的员工列表超过200个字节,因此该列表将被截断并附加最后的分隔符“;”、指定的截断指示符“…”,以及截断值的数目(“23”)。

SELECT department_id "Dept.",
LISTAGG(last_name, '; ' ON OVERFLOW TRUNCATE '...')
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; ... (23)
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:

下面的分析示例显示,对于2003年9月1日之前雇用的每个员工,该员工的部门、雇用日期以及该部门中的所有其他员工也在2003年9月1日之前雇用:

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

SQL LISTAGG 合并行的更多相关文章

  1. SQL Server 合并行

    select a.*,b.Organization_Name,c.User_Name sgry,c.renNum,d.User_Name fzr,e.pic_url from dbo.TB_ZYM_L ...

  2. Reapter合并行

    html文件: <asp:Repeater ID="rptEmployee" runat="server"> <HeaderTemplate& ...

  3. Vim常用操作-合并行。

    刚接触 Vim 会觉得它的学习曲线非常陡峭,要记住很多命令.所以这个系列的分享,不会教你怎么配置它,而是教你怎么快速的使用它. 在开发时为了代码美观,经常会把属性用换行的方式显示. <el-di ...

  4. easyui生成合并行,合计计算价格

    easyui生成合并行,合计计算价格 注:本文来源: 原创 一:图样你效果图 二:代码实现 1:datagrid 列展示: window.dataGrid = $("#dataGrid&qu ...

  5. sed行处理详解(交换行,合并行,删除行等)

    1.合并行 zj@zj:~/Script/blog_script$ cat test11234合并上下两行zj@zj:~/Script/blog_script$ sed '$!N;s/\n/\t/' ...

  6. 【editplus经常用的快捷键】Editplus 选中一行ctrl+r,Edit 合并行 Ctrl+Shift+J 合并选定行 删除当前行

    Editplus 选中一行: ctrl+rEditplus 复制一行: ctrl+r选择行,然后ctrl+c复制.复制一行到下一行中:Editplus有:Ctrl+j 复制上一行的一个字符到当前行Ed ...

  7. oracle 查看并行sql语句的并行数量和如何开并行

    1.执行sql:select /*+ parallel(a,4) */ * from tf_f_user a where rownum<100000; 2.如何查看该sql语句的并行数量: se ...

  8. SQL调优(SQL TUNING)并行查询提示(Hints)之pq_distribute的使用

    pq_distribute提示通常被用于提升数据仓库中分区表间的连接操作性能. pq_distribute提示允许你确定参与连接的表数据行在生产和消费并行查询服务进程间如何分配. pq_distrib ...

  9. mysql 行变列(多行变成一行/多行合并成一行/多行合并成多列/合并行)

    数据库结构如图: 而我想让同一个人的不同成绩变成此人在这一行不同列上显示出来,此时分为2中展现: 第一种展现如图----[多行变一列](合并后的数据在同一列上): sql如下: select name ...

随机推荐

  1. Linux(CentOS)启动时自动执行脚本(rc.local)

    一.Linux开机启动有多种方法,比如我设置mysql开机启动为:chkconfig --level 35 mysqld on 二.下面说说通过rc.local文件进行开机启动: 1.首先创建一个启动 ...

  2. 生命游戏(python实现,pygame显示图形)

    # 游戏规则:# 生命游戏(Game of Life),或者叫它的全称John Conway's Game of Life.是英国数学家约翰·康威在1970年代所发明的一种元胞自动机.# 1. 活细胞 ...

  3. EasyNVR网页摄像机无插件H5、谷歌Chrome直播方案之使用ffmpeg保存快照数据方法与代码

    背景分析 EasyNVR主要功能模块有设备发现与接入.实时直播.摄像机控制.录像与管理.设备快照与状态维护.第三方平台对接,其中设备快照与状态维护主要包括定时检测通道设备的在线状态.定时对通道摄像机进 ...

  4. JAVA SpringBoot2 整合 ueditor 的 并实现前后端分离

    1,下载 jsp 版本的 百度编辑器,点击此处下载 2,解压文件,解压后目录如下 3,我们将 jsp 目录暂时移动到别的地方,剩下的文件作为一个 H5 前端项目使用,笔者这边导入到 idea 进行开发 ...

  5. cocoapods安装 Unable to download data from http://ruby.taobao.org/ & don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

    安装cocoapods,记录两个问题! 1.镜像已经替换成了 http://ruby.taobao.org/, 还是不能不能安装cocoapods, 报错:Unable to download dat ...

  6. SQL Server 2019 中标量用户定义函数性能的改进

    在SQL Server中,我们通常使用用户定义的函数来编写SQL查询.UDF接受参数并将结果作为输出返回.我们可以在编程代码中使用这些UDF,并且可以快速编写查询.我们可以独立于任何其他编程代码来修改 ...

  7. 资源对象的池化, java极简实现,close资源时,自动回收

    https://www.cnblogs.com/piepie/p/10498953.html 在java程序中对于资源,例如数据库连接,这类不能并行共享的资源对象,一般采用资源池的方式进行管理. 资源 ...

  8. day49——圆形头像、定位、z-index、js

    day49 今日内容 圆形头像 <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  9. MySQL恩恩怨怨

    数据库基础 Windows安装MySQL Mac安装MySQL Linux安装MySQL MySQL存储引擎概述 MySQL表操作 MySQL支持的数据类型 MySQL表的完整性约束 MySQL记录操 ...

  10. bootstrap-wizard向导插件的使用

    引用文件 <link rel="stylesheet" href="bootstrap-wizard/bootstrap-wizard.css"> ...