Skip to content
Advertisement

MySQL aggregated sum of JSON objects

I have created new table and having details as JSON datatype. I tried to get the aggregated sum of all records. I can able to get the each values but I don’t know how to get the sum using group by options.

Sample Data

I want the aggregated group of these 2 records

Output data

I tried using JSON_EXTRACT(details, “$[*]”) but doesn’t work out

Advertisement

Answer

Update: Okay

First, I would definitely recommend normalizing the data a bit. Have you tried storing just the objects into the details column? If you needed to store groups of data with each Sample id, you can use a relating table. IE:)

Sample

id int auto increment

Details

sample_id int record json

Populate your data

Then you can do something like

Results

If you don’t want to (or can’t) use a normalized dataset, then perhaps you might look into a writing a stored procedure that loops over your details columns and aggregates the data for each one, with a query that aggregates the two datasets.

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