3

My table contains string in json format. I need to get the sum and average of each key.

+----+------------------------------------------------------------------------------------+------------+
| id | json_data                                                                          | subject_id |
+----+------------------------------------------------------------------------------------+------------+
| 1  | {"id": "a", "value": "30"}, {"id": "b", "value": "20"}, {"id": "c", "value": "30"} | 1          |
+----+------------------------------------------------------------------------------------+------------+
| 2  | {"id": "a", "value": "40"}, {"id": "b", "value": "50"}, {"id": "c", "value": "60"} | 1          |
+----+------------------------------------------------------------------------------------+------------+
| 3  | {"id": "a", "value": "20"}                                                         | 1          |
+----+------------------------------------------------------------------------------------+------------+

Expected result is

    {"id": "a", "sum": 90, "avg": 30},
    {"id": "b", "sum": 70, "avg": 35},
    {"id": "c", "sum": 120, "avg": 40}

I've tried

SELECT (
  JSON_OBJECT('id', id, 'sum', sum_data, 'avg', avg_data)
) FROM (
  SELECT 
    JSON_EXTRACT(json_data, "$.id") as id, 
    SUM(JSON_EXTRACT(json_data, "$.sum_data")) as sum_data, 
    AVG(JSON_EXTRACT(json_data, "$.avg_data")) as avg_data
  FROM Details 
  GROUP BY JSON_EXTRACT(json_data, "$.id")
) as t 

But no luck. How can I sort this out?

9
  • Your json is not proper. Commented May 24, 2019 at 9:50
  • Where are $.sum_data and $.avg_data in the JSON? Commented May 24, 2019 at 9:59
  • Are those supposed to be JSON arrays? If so, they need [] around them. Commented May 24, 2019 at 10:00
  • As far as I know, there's no easy way to extract the elements of a JSON array by a property value, and there's no way to group that way. Commented May 24, 2019 at 10:01
  • @Barmar sir, I read some references which says that Aliases sum_data, avg_data maps the JSON_OBJECT() Commented May 24, 2019 at 10:09

1 Answer 1

2

Input json needs to correct

create table json_sum (id int primary key auto_increment, json_data json);

insert into json_sum values (0,'[{"id": "a", "value": "30"}, {"id": "b", "value": "20"}, {"id": "c", "value": "30"}]');
insert into json_sum values (0,'[{"id": "a", "value": "40"}, {"id": "b", "value": "50"}, {"id": "c", "value": "60"}]');
insert into json_sum values (0,'[{"id": "a", "value": "20"}]');

select 
 json_object("id", jt.id, "sum", sum(jt.value), "avg", avg(jt.value))
 from json_sum, json_table(json_data, "$[*]" columns (
        row_id for ordinality,
        id varchar(10) path "$.id",
        value varchar(10) path "$.value")
) as jt
group by jt.id

Output:

json_object("id", jt.id, "sum", sum(jt.value), "avg", avg(jt.value))
{"id": "a", "avg": 30.0, "sum": 90.0}
{"id": "b", "avg": 35.0, "sum": 70.0}
{"id": "c", "avg": 45.0, "sum": 90.0}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.