通过组的复杂JSON B聚集
我有一个表格的表格,
STUDENT JSONB Column
1 {"total":8,"healthy": 2,"unhealthy":5,"X":7}
1 {"total":12,"healthy": 4"unhealthy":3,"X":9}
2 {"total":3,"healthy": 4}
2 {"total":4,"healthy": 1}
我想
1 {"total":20,"healthy": 6,"unhealthy":8,"X":16}
2 {"total":7,"healthy": 5}
对此进行分组并总结JSON中的值。我尝试使用jsonb_obj_agg 我知道如何使其与硬编码一起使用。但是我的问题是钥匙的NO可以是6-9。我无法将SQL中的密钥编码。
I have a table something like this
STUDENT JSONB Column
1 {"total":8,"healthy": 2,"unhealthy":5,"X":7}
1 {"total":12,"healthy": 4"unhealthy":3,"X":9}
2 {"total":3,"healthy": 4}
2 {"total":4,"healthy": 1}
Expected
1 {"total":20,"healthy": 6,"unhealthy":8,"X":16}
2 {"total":7,"healthy": 5}
I want to group by and sum up the value within the JSON. I tried using JSONB_OBJ_AGG
I know how to get it to work with hardcoding. But my problem is the no of keys can be 6-9. I cannot hardcode the keys in my SQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用这样的功能使用
jsonb_object_agg
来获取所有密钥的总和而无需声明它们:dbfiddle
You can use the
jsonb_object_agg
function like this to get the sum of all keys without declaring them:Demo in DBfiddle