You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| WITHIN GROUP [<orderby_clause>](https://docs.databend.com/sql/sql-commands/query-syntax/query-select#order-by-clause)| Defines the order of values in ordered set aggregates |
27
+
22
28
## Return Type
23
29
24
30
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;
52
58
| movie_title | ratings |
53
59
|-------------|------------|
54
60
| 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
Copy file name to clipboardExpand all lines: docs/en/sql-reference/20-sql-functions/07-aggregate-functions/aggregate-string-agg.md
+25-7Lines changed: 25 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,13 +4,14 @@ title: STRING_AGG
4
4
5
5
Aggregate function.
6
6
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.
8
8
9
9
## Syntax
10
10
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> ) ]
14
15
```
15
16
16
17
:::info
@@ -32,7 +33,13 @@ SELECT string_agg(number::VARCHAR, '|') AS s FROM numbers(5);
|`delimiter`| Optional constant String, if not specified, use empty String |
42
+
| WITHIN GROUP [<orderby_clause>](https://docs.databend.com/sql/sql-commands/query-syntax/query-select#order-by-clause)| Defines the order of values in ordered set aggregates |
36
43
37
44
## Return Type
38
45
@@ -64,8 +71,19 @@ FROM programming_languages;
64
71
65
72
**Result**
66
73
```sql
67
-
| concatenated_languages |
74
+
| concatenated_languages |
68
75
|------------------------------------------|
69
-
| Python, JavaScript, Java, C#, Ruby |
76
+
| Python, JavaScript, Java, C#, Ruby |
70
77
```
71
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
0 commit comments