listagg postgresql
时间: 2023-06-21 16:23:42 浏览: 146
Listagg is a built-in aggregate function in PostgreSQL that concatenates values from multiple rows into a single string.
The basic syntax of the listagg function is as follows:
```
listagg(expression, delimiter) WITHIN GROUP (ORDER BY sort_expression)
```
- `expression`: The column or expression to aggregate.
- `delimiter`: The separator to use between each value in the concatenated string.
- `sort_expression`: Optional. Specifies the order of the rows before concatenation.
Here's an example of using the listagg function:
```
SELECT department, listagg(name, ', ') WITHIN GROUP (ORDER BY name)
FROM employees
GROUP BY department;
```
This query will concatenate the names of employees in each department separated by a comma and space. The output will be grouped by department.
阅读全文