listagg within group
时间: 2023-10-26 13:05:11 浏览: 122
group list view
`LISTAGG` is an aggregate function in SQL that concatenates the values of a specified column into a single string. The `WITHIN GROUP` clause allows you to specify the order in which the values should be concatenated.
Here's an example usage of `LISTAGG` with `WITHIN GROUP`:
```sql
SELECT department_id,
LISTAGG(last_name, ', ') WITHIN GROUP (ORDER BY last_name) AS employees
FROM employees
GROUP BY department_id;
```
In this example, the `LISTAGG` function concatenates the `last_name` values from the `employees` table for each department, with the names ordered alphabetically. The result is a comma-separated string of employees' last names for each department.
阅读全文