Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate JSON in PostgreSQL

I have a json column with entries that look like this:

{
  "pages": "64",
  "stats": {
    "1": { "200": "55", "400": "4" },
    "2": { "200": "1" },
    "3": { "200": "1", "404": "13" },
  }
}

The 'stats' are collections (of various sizes) containing http status codes versus counts.

I would like to aggregate the stats into two calculated columns - one for the total number of 200 responses and the other for the total number of responses (including 200s).

like image 544
Will Jenkins Avatar asked Sep 24 '26 03:09

Will Jenkins


1 Answers

You can use two lateral joins to unnest the inner objects, then do conditional aggregation:

select 
    sum(z.cnt::int) no_responses,
    sum(z.cnt::int) filter(where z.code::int = 200) no_200_responses
from mytable t
cross join lateral jsonb_each(t.data -> 'stats') as x(kx, obj)
cross join lateral jsonb_each_text(x.obj) as z(code, cnt)

Demo on DB Fiddle:

no_responses | no_200_responses
-----------: | ---------------:
          74 |               57
like image 96
GMB Avatar answered Sep 26 '26 00:09

GMB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!