需要select from projects
并获得类似的内容:
[id] = int
[staff] = agg {users.data,staff.data,role.data}
[chief] = agg {users.data,staff.data,role.data}
[client] = agg {users.data,clients.data,role.data}
[涉及] = [agg {users.data,staff.data,role.data}],[agg {users.data,staff.data,role.data}],...
[other_project_data]
我知道array_agg(json_agg)
,但它只需要1个参数。我只得到[staff] = agg {user.data}
答案 0 :(得分:0)
你需要使用json_build_object / jsonb_build_object(key,value):
jsonb_agg(
jsonb_build_object(
'usersdata', users.data,
'staffdata', staff.data
))
使用两个表:
WITH users AS (
SELECT 1 as id, 'aaa'::text AS data, 10 as cnt
), staff AS (
SELECT 1 as user_id, 'bbbb'::text AS data, 5 as cnt
)
SELECT
jsonb_agg(
jsonb_build_object(
'usersdata', u.data || s.data,
'usersdataarray', ARRAY[ u.data, s.data],
'cntdata', u.cnt + s.cnt
))
FROM users AS u
JOIN staff AS s ON (s.user_id = u.id)
WITH子句模拟您的表。