Skip to content

Commit 17dea17

Browse files
authored
docs: add WITHIN GROUP for string_agg & array_agg (#1683)
1 parent a3914ff commit 17dea17

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The ARRAY_AGG function (also known by its alias LIST) transforms all the values,
88
## Syntax
99

1010
```sql
11-
ARRAY_AGG(<expr>)
11+
ARRAY_AGG(<expr>) [ WITHIN GROUP ( <orderby_clause> ) ]
1212

1313
LIST(<expr>)
1414
```
@@ -19,6 +19,12 @@ LIST(<expr>)
1919
|-----------| -------------- |
2020
| `<expr>` | Any expression |
2121

22+
## Optional
23+
24+
| Optional | Description |
25+
|-------------------------------------|-------------------------------------------------------|
26+
| 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 |
27+
2228
## Return Type
2329

2430
Returns an [Array](../../00-sql-reference/10-data-types/array.md) with elements that are of the same type as the original data.
@@ -52,4 +58,14 @@ GROUP BY movie_title;
5258
| movie_title | ratings |
5359
|-------------|------------|
5460
| Inception | [5, 4, 5] |
61+
62+
-- List all ratings for Inception in an array Using `WITHIN GROUP`
63+
SELECT movie_title, ARRAY_AGG(rating) WITHIN GROUP ( ORDER BY rating DESC ) AS ratings
64+
FROM movie_ratings
65+
WHERE movie_title = 'Inception'
66+
GROUP BY movie_title;
67+
68+
| movie_title | ratings |
69+
|-------------|------------|
70+
| Inception | [5, 5, 4] |
5571
```

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ title: STRING_AGG
44

55
Aggregate function.
66

7-
The STRING_AGG() function converts all the non-NULL values of a column to String, separated by the delimiter.
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.
88

99
## Syntax
1010

11-
```sql
12-
STRING_AGG(<expr>)
13-
STRING_AGG(<expr> [, delimiter])
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> ) ]
1415
```
1516

1617
:::info
@@ -32,7 +33,13 @@ SELECT string_agg(number::VARCHAR, '|') AS s FROM numbers(5);
3233
| Arguments | Description |
3334
|-------------|---------------------------------------------------------------------|
3435
| `<expr>` | Any string expression (if not a string, use `::VARCHAR` to convert) |
35-
| `delimiter` | Optional constant String, if not specified, use empty String |
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 |
3643

3744
## Return Type
3845

@@ -64,8 +71,19 @@ FROM programming_languages;
6471

6572
**Result**
6673
```sql
67-
| concatenated_languages |
74+
| concatenated_languages |
6875
|------------------------------------------|
69-
| Python, JavaScript, Java, C#, Ruby |
76+
| Python, JavaScript, Java, C#, Ruby |
7077
```
7178

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+
```

0 commit comments

Comments
 (0)