首页 > 解决方案 > 具有多个空值的多个 jsonb_agg 结果数组

问题描述

我使用jsonb_agg了两次,根据列的不同值对数据进行分组。如果左列 ( jsonb_agg) 产生两行,右列 ( jsonb_agg) 返回两个空值,在 API 中被视为值(而不是空值)。我想要的是空值或空数组,即使右侧有多个值。

询问:

select  COALESCE(jsonb_agg(case  when t."machine_name" = 'tag' then t  end) FILTER (WHERE t IS NOT NULL), '[]') as tags, COALESCE(jsonb_agg(case  when t."machine_name" = 'category' then t else '' end) FILTER (WHERE t IS NOT NULL), '[]') as categories, p."precedentId", p."owner"  from 
(
select  p."precedentId",  t."machine_name", t."tId", t."parentId", t."tTitle"  from 
public.precedent p, public.index i, public.term_data t
where p."precedentId" = i."precedentId"
and i."tId" = t."tId" 
and t."machine_name" in ('tag','category')
    ) as t 
    right join
    public.precedent as p
    on p."precedentId" = t."precedentId"
    group by p."precedentId"

电流输出:

标签 类别
“[]” “[]”
"[{""tId"": 2, ""tTitle"": ""shuvo"", ""parentId"": 0, ""precedentId"": ""Shuvo"", ""machine_name"": " "tag""}, {""tId"": 1, ""tTitle"": ""software"", ""parentId"": 0, ""precedentId"": ""Shuvo"", ""machine_name ““: ““标签””}]” “[空,空]”

预期的:

电流输出 -

标签 类别
“[]” “[]”
"[{""tId"": 2, ""tTitle"": ""shuvo"", ""parentId"": 0, ""precedentId"": ""Shuvo"", ""machine_name"": " "tag""}, {""tId"": 1, ""tTitle"": ""software"", ""parentId"": 0, ""precedentId"": ""Shuvo"", ""machine_name ““: ““标签””}]” “[]”

标签: postgresqljsonb

解决方案


推荐阅读