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.
對於指定的度量, LISTAGG對ORDER 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 COUNT或WITHOUT 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