MySQL SUM json values grouped by json keys

前端 未结 4 1378
情深已故
情深已故 2021-01-01 04:36

Is it possible to calculate sum of json values grouped by json keys?

Mysql version is 5.7.17 on Google cloud sql.

Example_1: A short exampl

4条回答
  •  迷失自我
    2021-01-01 04:46

    SQL for the "short example":

    SELECT col1,
           JSON_OBJECT('key1', SUM(value1), 'key2', SUM(value2)) AS col2
    FROM
        (SELECT col1,
                JSON_EXTRACT(col2, '$.key1') AS value1,
                JSON_EXTRACT(col2, '$.key2') AS value2
         FROM tbl) subq
    GROUP BY col1;
    

提交回复
热议问题