获取PostgreSQL Jsonb列中Array字段的Group by和count



我的表格是这样的

<表类> user_Id 调查 1001 {"你对什么感兴趣;"[游戏]", "你打算如何使用Gamester "; "[玩游戏,看别人玩,和其他玩家联系]"} 1001 {"你对什么感兴趣;"[优惠券]", "你打算如何使用Gamester "; "[看别人玩]"} 1001 {"你对什么感兴趣;"[游戏]", "你打算如何使用Gamester "; "[玩游戏]"}

您的json数据数组在How do you plan to use Gamester?属性是错误的。因为您存储的是字符串而不是值数组。

按如下所示替换所有值:

来源格式:"[Play games, Watch other people play, Connect with other gamers]"

要求格式:["Play games", "Watch other people play", "Connect with other gamers"]


修复数据后,您可以使用jsonb_path_query函数并返回所需属性的值列表,然后对它们进行分组。

with cte_plan as (
select
jsonb_path_query(survey, '$."How do you plan to use Gamester?"[*]') as option
from my_table
)
select option, count(option) from cte_plan group by option;

结果

<表类>选择数tbody><<tr>看别人玩2玩游戏2与其他玩家连接1

最新更新