Postgres JSON查询匿名Rray

如何在Postgresql中查询以下JSON?每行将具有相似的布局。

列名: actions [{"A": 10, "action": "fly"}, {"B": 2, "action": "swim"}]

我想用key获得所有值的总和A

我可以通过以下方式获取数据:

select actions from t

但是以下错误 ERROR: column "actions" does not exists

select actions #> 'A' from t

a_horse_with_no_name

您需要取消嵌套数组元素并仅选择具有键的元素A

select sum( (t.o ->> 'A')::int )
from the_table d
  join lateral jsonb_array_elements(d.actions) as t(o) on o ? 'A'

在线示例:https//rextester.com/MYGMI38108

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章