我有一个JSON列telework
,存储在Postgres中,如下所示
"{ ...,
"biweeklyWeek1-locationMon": "alternative",
"biweeklyWeek1-locationTues": "agency",
"biweeklyWeek1-locationWeds": "alternative",
"biweeklyWeek1-locationThurs": "alternative",
"biweeklyWeek1-locationFri": "alternative",
... ,
"biweeklyWeek2-locationMon": "alternative",
"biweeklyWeek2-locationTues": "agency",
"biweeklyWeek2-locationWeds": "alternative",
"biweeklyWeek2-locationThurs": "alternative",
"biweeklyWeek2-locationFri": "alternative",
... }"
我需要**分别统计biweeklyWeek1-location*
字段和biWeeklyWeek2-location*
字段中“alternative”**的出现次数,并在主查询中选择这两个字段作为单独的字段。这些字段中的值可能是填充的、空白(""
)或null
。此外,这些字段在JSON中可能部分或全部缺失。
select a.id,
a.name,
a.telework->>??? as alternativePerWeek1,
a.telework->>??? as alternativePerWeek2,
...
非常奇怪的是,即使我使用->
这个硬编码的ID执行下面的示例,我也会得到一个NULL结果,尽管我看到它不应该是NULL:select telework, telework->'biweeklyWeek1-locationMon' from ets.agreement_t where id = 24763;
2条答案
按热度按时间dced5bon1#
使用
json_each()
取消嵌套JSON后,可以使用标量子查询顺便说一句:存储JSON的推荐数据类型是
jsonb
jw5wzhpr2#
假设:远程工作JSON列包含一个(键,值)对列表,键的格式为
biweekly?-location???
。1.使用json_each()和横向连接将JSON列拆分为每行的(key,value)对。
1.提取前缀和聚合,如下所示:
输出如下所示:
编辑:根据注解,json_each()也可用于SELECT
输出量:
您还可以解析
key_value
列以获取所需内容