postgresql组列到json包含数组

postgresql组列到json包含数组,json,postgresql,Json,Postgresql,表_名称e id | name | cate | link ---+------+------+--------- 1 | a | A | link1 2 | a | B | link2 3 | a | B | link3 4 | b | B | link4 5 | c | A | link5 6 | d | A | link6 7 | e | B | link7 我想要回复: name |

表_名称e

id | name | cate | link  
---+------+------+---------
1  | a    |  A   | link1
2  | a    |  B   | link2
3  | a    |  B   | link3
4  | b    |  B   | link4
5  | c    |  A   | link5
6  | d    |  A   | link6
7  | e    |  B   | link7
我想要回复:

name | A                        | B
------+------+-------------------+------------
a    |  {id: 1, link: 'link1'}  | [{id: 2, link: 'link2'}, {id: 3, link: 'link3'}]
b    |                          | [{id: 4, link: 'link4'}]
c    |  {id: 5, link: 'link5'}  |
d    |  {id: 6, link: 'link6'}  |
e    |                          | [{id: 7, link: 'link7'}]
cate字段值仅为A、B、C、D。但值为B必须是数组

我的实验失败了

select name, format('{%s}', string_agg(format('"id": "%s", "name": "%s", "link":"%s"', id, name, link), ','))::json as A from elements where cate = 'A' group by name;
select name, string_to_array(format('[link: "%s", id: "%s", name: "%s"]', link, id, name)) as B from elements where cate = 'B' group by name;
select name, format('{%s}', string_agg(format('"id": "%s", "name": "%s", "link":"%s"', id, name, link), ','))::json as C from elements where cate = 'C' group by name;
select name, format('{%s}', string_agg(format('"id": "%s", "name": "%s", "link":"%s"', id, name, link), ','))::json as D from elements where cate = 'D' group by name;
如果连接其他表p

id | e_id | role_id 
---+------+----------
1  | 1    |  100
2  | 3    |  101
3  | 4    |  102
4  | 5    |  103
结果:

name   | checked | A                                     | B
-------+---------+---------------------------------------+-----------------------------------------------------------------
a      |   true  | {id: 1, link: 'link1', checked: true} | [{id: 2, link: 'link2'}, {id: 3, link: 'link3', checked: true}]
b      |   true  |                                       | [{id: 4, link: 'link4', checked: true}]
c      |   true  | {id: 5, link: 'link5', checked: true} |
d      |         | {id: 6, link: 'link6'}                |
e      |         |                                       | [{id: 7, link: 'link7'}]

首先,不要使用format函数来创建json字段,PostgreSQL拥有解析和创建json字段所需的所有函数。您没有发布示例ddl和数据,但您的查询应该如下所示:

select name, 
   json_agg(case cate when 'A' then json_build_object('id',id,'link',link) end) A, 
   json_agg(case cate when 'B' then json_build_object('id',id,'link',link) end) B 
from data group by name

首先,不要使用format函数来创建json字段,PostgreSQL拥有解析和创建json字段所需的所有函数。您没有发布示例ddl和数据,但您的查询应该如下所示:

select name, 
   json_agg(case cate when 'A' then json_build_object('id',id,'link',link) end) A, 
   json_agg(case cate when 'B' then json_build_object('id',id,'link',link) end) B 
from data group by name

你的尝试有什么问题?你有错误吗?结果不正确?如果是,哪个?postgresqlversion@JustMe9.4+我正在尝试,谢谢。你的尝试有什么问题?你有错误吗?结果不正确?如果是,哪个?postgresqlversion@JustMe9.4+我正在尝试,谢谢。字段A应该是return json。字段A应该是return json。