SQL LISTAGG 合并行

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

 

posted @ 2019-07-09 16:05  赵春义  阅读(2634)  评论(0编辑  收藏  举报