在 postgres 中将行聚合为 jsonb
aggregate rows as jsonb in postgres
key | value | col
----------------------------------------
color | red | g1
color | blue | g2
contrast | {'l':123456} | g1
setting | srnew | g2
contrast | {'s':123456} | g1
key | value | col
---------------------------------------------
color | ['red'] | g1
color | ['blue'] | g2
contrast | [{'l':123456}, {'s':456}]| g1
setting | ['srnew'] | g2
我如何将第一个 table 聚合到第二个 table?
列 value
是 jsonb 类型。我希望结果列 value
也是 jsonb。
您可以尝试使用 array_agg 通过连接进行分组,然后 array_to_json
到 json 数组。
SELECT array_to_json(array_agg(value))
FROM t
group by key,col
key | value | col
----------------------------------------
color | red | g1
color | blue | g2
contrast | {'l':123456} | g1
setting | srnew | g2
contrast | {'s':123456} | g1
key | value | col
---------------------------------------------
color | ['red'] | g1
color | ['blue'] | g2
contrast | [{'l':123456}, {'s':456}]| g1
setting | ['srnew'] | g2
我如何将第一个 table 聚合到第二个 table?
列 value
是 jsonb 类型。我希望结果列 value
也是 jsonb。
您可以尝试使用 array_agg 通过连接进行分组,然后 array_to_json
到 json 数组。
SELECT array_to_json(array_agg(value))
FROM t
group by key,col