postgresql - Sum of json array -


i have json type field, this

data

{"age": 44, "name": "jun"}  {"age": 19, "name": "pablo", "attempts": [11, 33, 20]}  {"age": 33, "name": "maria", "attempts": [77, 10]} 

here json data have "attempts" array, not. when json have array, need sum of array elements in different field, need result like

data , sum_of_array

{"age": 44, "name": "jun"} , (nothing here)  {"age": 19, "name": "pablo", "attempts": [11, 33, 20]} , 64  {"age": 33, "name": "maria", "attempts": [77, 10]} , 87 

select attempts.id,        sum(vals.v::integer) sum_attempts attempts    left join lateral jsonb_array_elements_text(val->'attempts') vals(v)       on true group attempts.id; 

use json_array_elements_text if using json instead of jsonb.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -