Skip to content

Commit f8b7898

Browse files
authored
LISTAGG (#1966)
1 parent 669251c commit f8b7898

File tree

3 files changed

+106
-85
lines changed

3 files changed

+106
-85
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: GROUP_CONCAT
3+
---
4+
5+
Alias for [LISTAGG](aggregate-listagg.md).
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
```

docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-string-agg.md

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,88 +2,4 @@
22
title: STRING_AGG
33
---
44

5-
Aggregate function.
6-
7-
The STRING_AGG() function (also known as GROUP_CONCAT or LISTAGG) concatenates all the non-NULL values of a column to a string, separated by delimiter.
8-
9-
## Syntax
10-
11-
```sql
12-
STRING_AGG(<expr> [, delimiter]) [ WITHIN GROUP ( <orderby_clause> ) ]
13-
GROUP_CONCAT(<expr> [, delimiter]) [ WITHIN GROUP ( <orderby_clause> ) ]
14-
LISTAGG(<expr> [, delimiter]) [ WITHIN GROUP ( <orderby_clause> ) ]
15-
```
16-
17-
:::info
18-
If `<expr>` is not a String expression, should use `::VARCHAR` to convert.
19-
20-
For example:
21-
```sql
22-
SELECT string_agg(number::VARCHAR, '|') AS s FROM numbers(5);
23-
+-----------+
24-
| s |
25-
+-----------+
26-
| 0|1|2|3|4 |
27-
+-----------+
28-
```
29-
:::
30-
31-
## Arguments
32-
33-
| Arguments | Description |
34-
|-------------|---------------------------------------------------------------------|
35-
| `<expr>` | Any string expression (if not a string, use `::VARCHAR` to convert) |
36-
37-
## Optional
38-
39-
| Optional | Description |
40-
|-------------------------------------|--------------------------------------------------------------|
41-
| `delimiter` | Optional constant String, if not specified, use empty String |
42-
| WITHIN GROUP [&lt;orderby_clause&gt;](https://docs.databend.com/sql/sql-commands/query-syntax/query-select#order-by-clause) | Defines the order of values in ordered set aggregates |
43-
44-
## Return Type
45-
46-
the String type
47-
48-
## Example
49-
50-
**Create a Table and Insert Sample Data**
51-
52-
```sql
53-
CREATE TABLE programming_languages (
54-
id INT,
55-
language_name VARCHAR
56-
);
57-
58-
INSERT INTO programming_languages (id, language_name)
59-
VALUES (1, 'Python'),
60-
(2, 'JavaScript'),
61-
(3, 'Java'),
62-
(4, 'C#'),
63-
(5, 'Ruby');
64-
```
65-
66-
**Query Demo: Concatenate Programming Language Names with a Delimiter**
67-
```sql
68-
SELECT STRING_AGG(language_name, ', ') AS concatenated_languages
69-
FROM programming_languages;
70-
```
71-
72-
**Result**
73-
```sql
74-
| concatenated_languages |
75-
|------------------------------------------|
76-
| Python, JavaScript, Java, C#, Ruby |
77-
```
78-
79-
**Query Demo: Concatenate Programming Language Names with a Delimiter Using `WITHIN GROUP`**
80-
```sql
81-
SELECT STRING_AGG(language_name, ', ') WITHIN GROUP ( ORDER BY language_name DESC ) AS concatenated_languages
82-
FROM programming_languages;
83-
```
84-
**Result**
85-
```sql
86-
| concatenated_languages |
87-
|------------------------------------------|
88-
| Ruby, Python, JavaScript, Java, C# |
89-
```
5+
Alias for [LISTAGG](aggregate-listagg.md).

0 commit comments

Comments
 (0)