如何return只有一行一列的名字?
How to return only one row and one column name?
正在自学Psql函数,想return只有一行一列名:dep_people不知道如何一行一解决这道题列名称。请帮我理解Psql函数。
预览table
SELECT * FROM test_sch.apollo_org_job_function;
|-------------------------------------------|
|organization_id |department |no_people|
|-------------------------------------------|
| 2a |accounting | 3 |
| 1a |engineering | 2 |
| 1a |entrepreneurship| 1 |
| 1a |human resources | 4 |
|-------------------------------------------|
我在这里
SELECT
department, no_people
FROM test_sch.apollo_org_job_function
GROUP BY department, no_people
ORDER BY department;
我的return是:
|--------------------------|
|department |no_people|
|--------------------------|
|accounting | 3 |
|engineering | 2 |
|entrepreneurship| 1 |
|human resources | 4 |
|--------------------------|
调用函数
select * from test_sch.return_dep_people('1a')
as f(dep_people text);
预期输出
------------------------------------------------------|
|dep_people |
------------------------------------------------------|
|Engineering:2 , human_resources:4, entrepreneurship:1|
-------------------------------------------------------
注意:结果应该只包含 1 个名为“dep_people”的列和字符串中的 1 行,“Engineering:2,human_resources:4,entrepreneurship:1”是一个字符串)
您可以使用 JSON_BUILD_OBJECT
和 JSON_AGG
实现此目的:
WITH data AS (
SELECT department, COUNT(*) as "no_people"
FROM apollo_org_job_function GROUP BY department ORDER BY department
) SELECT
JSON_AGG(JSON_BUILD_OBJECT(department, no_people)) as "dep_people"
FROM data;
我想,STRING_AGG 可能会对您有所帮助。像这样:
SELECT
STRING_AGG(d.department || ': ' || CAST(d.no_people AS text), ', ') AS dep_people
FROM ([your initial query]) AS d
create function happy_return_dep_people(organisation_id text)
returns text
language plpgsql
as
$$
declare
dep_people text;
begin
SELECT STRING_AGG(d.department || ': ' || CAST(d.no_people AS text), ', ') into dep_people AS dep_people
FROM (
SELECT department, no_people
FROM test_sch.apollo_org_job_function
WHERE organization_id = organisation_id
GROUP BY department, no_people ORDER BY department) AS d;
return dep_people;
end;
$$;
select * from test_sch.happy_return_dep_people('1a')
as dep_people;
正在自学Psql函数,想return只有一行一列名:dep_people不知道如何一行一解决这道题列名称。请帮我理解Psql函数。
预览table
SELECT * FROM test_sch.apollo_org_job_function;
|-------------------------------------------|
|organization_id |department |no_people|
|-------------------------------------------|
| 2a |accounting | 3 |
| 1a |engineering | 2 |
| 1a |entrepreneurship| 1 |
| 1a |human resources | 4 |
|-------------------------------------------|
我在这里
SELECT
department, no_people
FROM test_sch.apollo_org_job_function
GROUP BY department, no_people
ORDER BY department;
我的return是:
|--------------------------|
|department |no_people|
|--------------------------|
|accounting | 3 |
|engineering | 2 |
|entrepreneurship| 1 |
|human resources | 4 |
|--------------------------|
调用函数
select * from test_sch.return_dep_people('1a')
as f(dep_people text);
预期输出
------------------------------------------------------|
|dep_people |
------------------------------------------------------|
|Engineering:2 , human_resources:4, entrepreneurship:1|
-------------------------------------------------------
注意:结果应该只包含 1 个名为“dep_people”的列和字符串中的 1 行,“Engineering:2,human_resources:4,entrepreneurship:1”是一个字符串)
您可以使用 JSON_BUILD_OBJECT
和 JSON_AGG
实现此目的:
WITH data AS (
SELECT department, COUNT(*) as "no_people"
FROM apollo_org_job_function GROUP BY department ORDER BY department
) SELECT
JSON_AGG(JSON_BUILD_OBJECT(department, no_people)) as "dep_people"
FROM data;
我想,STRING_AGG 可能会对您有所帮助。像这样:
SELECT
STRING_AGG(d.department || ': ' || CAST(d.no_people AS text), ', ') AS dep_people
FROM ([your initial query]) AS d
create function happy_return_dep_people(organisation_id text)
returns text
language plpgsql
as
$$
declare
dep_people text;
begin
SELECT STRING_AGG(d.department || ': ' || CAST(d.no_people AS text), ', ') into dep_people AS dep_people
FROM (
SELECT department, no_people
FROM test_sch.apollo_org_job_function
WHERE organization_id = organisation_id
GROUP BY department, no_people ORDER BY department) AS d;
return dep_people;
end;
$$;
select * from test_sch.happy_return_dep_people('1a')
as dep_people;