Skip to content
Advertisement

PostgreSQL – Adding up json values

This is my json column in pgSQL and I need to add up all of the “sum” amounts using pgSQL.

So the query should return “50” using the data below.

I did find something like this (Get aggregate sum of json array in Postgres NOSQL json data) that kinda works if I had my values in array, but I dont, so this gives me an error.

Advertisement

Answer

Because your JSON value is nested objects, We can try to use jsonb_each_text function to get all nested object which is contain sum field before SUM by that.

sqlfiddle

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement