|
| 1 | +--- |
| 2 | +title: LISTAGG |
| 3 | +--- |
| 4 | +import FunctionDescription from '@site/src/components/FunctionDescription'; |
| 5 | + |
| 6 | +<FunctionDescription description="Introduced or updated: v1.2.725"/> |
| 7 | + |
| 8 | +Concatenates values from multiple rows into a single string, separated by a specified delimiter. This operation can be performed using two different function types: |
| 9 | +- Aggregate Function: The concatenation happens across all rows in the entire result set. |
| 10 | +- Window Function: The concatenation happens within each partition of the result set, as defined by the `PARTITION BY` clause. |
| 11 | + |
| 12 | +## Syntax |
| 13 | + |
| 14 | +```sql |
| 15 | +-- Aggregate Function |
| 16 | +LISTAGG([DISTINCT] <expr> [, <delimiter>]) |
| 17 | + [WITHIN GROUP (ORDER BY <order_by_expr>)] |
| 18 | + |
| 19 | +-- Window Function |
| 20 | +LISTAGG([DISTINCT] <expr> [, <delimiter>]) |
| 21 | + [WITHIN GROUP (ORDER BY <order_by_expr>)] |
| 22 | + OVER ([PARTITION BY <partition_expr>]) |
| 23 | +``` |
| 24 | + |
| 25 | +| Parameter | Description | |
| 26 | +|---------------------------------|---------------------------------------------------------------------------------------------------| |
| 27 | +| `DISTINCT` | Optional. Removes duplicate values before concatenation. | |
| 28 | +| `<expr>` | The expression to concatenate (typically a column or an expression). | |
| 29 | +| `<delimiter>` | Optional. The string to separate each concatenated value. Defaults to an empty string if omitted. | |
| 30 | +| `ORDER BY <order_by_expr>` | Defines the order in which the values are concatenated. | |
| 31 | +| `PARTITION BY <partition_expr>` | Divides rows into partitions to perform aggregation separately within each group. | |
| 32 | + |
| 33 | +## Aliases |
| 34 | + |
| 35 | +- [STRING_AGG](aggregate-string-agg.md) |
| 36 | +- [GROUP_CONCAT](aggregate-group-concat.md) |
| 37 | + |
| 38 | +## Return Type |
| 39 | + |
| 40 | +String. |
| 41 | + |
| 42 | +## Examples |
| 43 | + |
| 44 | +In this example, we have a table of customer orders. Each order belongs to a customer, and we want to create a list of all products each customer has purchased. |
| 45 | + |
| 46 | +```sql |
| 47 | +CREATE TABLE orders ( |
| 48 | + customer_id INT, |
| 49 | + product_name VARCHAR |
| 50 | +); |
| 51 | + |
| 52 | +INSERT INTO orders (customer_id, product_name) VALUES |
| 53 | +(1, 'Laptop'), |
| 54 | +(1, 'Mouse'), |
| 55 | +(1, 'Laptop'), |
| 56 | +(2, 'Phone'), |
| 57 | +(2, 'Headphones'); |
| 58 | +``` |
| 59 | + |
| 60 | +The following uses `LISTAGG` as an aggregate function with GROUP BY to concatenate all products purchased by each customer into a single string: |
| 61 | + |
| 62 | +```sql |
| 63 | +SELECT |
| 64 | + customer_id, |
| 65 | + LISTAGG(product_name, ', ') WITHIN GROUP (ORDER BY product_name) AS product_list |
| 66 | +FROM orders |
| 67 | +GROUP BY customer_id; |
| 68 | +``` |
| 69 | + |
| 70 | +```sql |
| 71 | +┌─────────────────────────────────────────┐ |
| 72 | +│ customer_id │ product_list │ |
| 73 | +├─────────────────┼───────────────────────┤ |
| 74 | +│ 2 │ Headphones, Phone │ |
| 75 | +│ 1 │ Laptop, Laptop, Mouse │ |
| 76 | +└─────────────────────────────────────────┘ |
| 77 | +``` |
| 78 | + |
| 79 | +The following uses `LISTAGG` as a window function, so each row keeps its original details but also displays the full product list for the customer's group: |
| 80 | + |
| 81 | +```sql |
| 82 | +SELECT |
| 83 | + customer_id, |
| 84 | + product_name, |
| 85 | + LISTAGG(product_name, ', ') WITHIN GROUP (ORDER BY product_name) |
| 86 | + OVER (PARTITION BY customer_id) AS product_list |
| 87 | +FROM orders; |
| 88 | +``` |
| 89 | + |
| 90 | +```sql |
| 91 | +┌────────────────────────────────────────────────────────────┐ |
| 92 | +│ customer_id │ product_name │ product_list │ |
| 93 | +├─────────────────┼──────────────────┼───────────────────────┤ |
| 94 | +│ 2 │ Phone │ Headphones, Phone │ |
| 95 | +│ 2 │ Headphones │ Headphones, Phone │ |
| 96 | +│ 1 │ Laptop │ Laptop, Laptop, Mouse │ |
| 97 | +│ 1 │ Mouse │ Laptop, Laptop, Mouse │ |
| 98 | +│ 1 │ Laptop │ Laptop, Laptop, Mouse │ |
| 99 | +└────────────────────────────────────────────────────────────┘ |
| 100 | +``` |
0 commit comments