gpt4 book ai didi

json - 获取 JSONB 属性的计数

转载 作者:行者123 更新时间:2023-11-29 13:10:36 25 4
gpt4 key购买 nike

我有 jsonb 列 answers 3 行可能看起来像这样:

{answer1: {label: "", value: "answer1_value1"}, answer2: {label: "", value: "answer2_value1"}}
{answer1: {label: "", value: "answer1_value2"}, answer2: {label: "", value: "answer2_value1"}}
{answer1: {label: "", value: "answer1_value2"}, answer2: {label: "", value: "answer2_value2"}}

一组行的答案属性名称可以不同。我想从上面的例子中得到这个结果(每个答案按值分组):

answer1: { answer1_value1: 1, answer1_value2: 2}
answer2: { answer2_value1: 2, answer2_value2: 1}

这可能吗?

我得到了像这样的字段名称 SELECT DISTINCT json_object_keys(answers::json) as field,但不知道下一步该怎么做。

最佳答案

使用 jsonb_each() 可以计算答案并将结果聚合回 json 对象:

select jsonb_build_object(key, jsonb_object_agg(val, count))
from (
select key, value->>'value' as val, count(*)
from my_table
cross join jsonb_each(answers)
group by key, val
) s
group by key;

Working example in rextester.

关于json - 获取 JSONB 属性的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55315640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com