postgresql - 具有多个空值的多个 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 ““: ““标签””}]” | “[]” |
解决方案
推荐阅读
- c++ - 访问被拒绝后 C++ 中的故障安全代码
- sql - Postgres 不执行仅索引扫描
- javascript - 如何在没有输入字段的移动网站上进行输入
- javascript - 滚动到顶部时无法识别
- java - 在 spring-cloud 项目中,可以完全用 bootstrap.yml 替换 application.yml 吗?
- python - 无法安装避风港 R
- continuous-integration - 在多个存储库之间共享 github 工作流的最有效方式是什么?
- apache2 - 未找到 在此服务器上未找到请求的 URL。Apache/2.4.29 (Ubuntu) 服务器在 localhost 端口 9080
- amazon-web-services - 从弹性beantalk获取日志文件
- asp.net - 如何在使用 IdentityServer4 的 ASP.Net Core SPA 模板中为剃须刀页面添加身份验证