--公式
STRING_AGG(expression, separator)
CREATE TABLE employees ( id SERIAL PRIMARY KEY, name VARCHAR(50), department VARCHAR(50) ); INSERT INTO employees (name, department) VALUES ('John', 'Sales'), ('Jane', 'HR'), ('David', 'Finance'), ('Sarah', 'Sales');
SELECT department, STRING_AGG(name, ',') AS employees_list FROM employees GROUP BY department; SQL 上述查询将返回类似于以下结果: department | employees_list --------------+-------------------- Sales | John,Sarah HR | Jane Finance | David
借鉴地址:https://deepinout.com/sql/sql-questions/76_sql_postgresql_query_to_return_results_as_a_comma_separated_list.html
标签:多行,逗号,name,employees,list,Sales,pgsql,sql,department From: https://www.cnblogs.com/of-course/p/17814619.html