title: MySQL SUM json values grouped by json keys
body: <p>Is it possible to calculate sum of json values grouped by json keys? </p>
  
  <p>Mysql version is 5.7.17 on Google cloud sql.</p>
  
  <p><strong>Example_1:</strong> A short example of my point:</p>
  
  <pre><code>col1 | col2
  -----|-----------------------
  aaa  | {"key1": 1, "key2": 3}
  -----|-----------------------
  bbb  | {"key1": 0, "key2": 2}
  -----|-----------------------
  aaa  | {"key1": 50, "key2": 0}
  </code></pre>
  
  <p>SQL query should produce:</p>
  
  <pre><code>col1 | col2
  -----|-----------------------
  aaa  | {"key1": 51, "key2": 3}
  -----|-----------------------
  bbb  | {"key1": 0, "key2": 2}
  </code></pre>
  
  <p>OR </p>
  
  <p>will it be possible with any of the below schema?</p>
  
  <p><strong>Example_2:</strong></p>
  
  <pre><code>col1 | col2
  -----|-----------------------
  aaa  | {{"key_name" : "key1", "key_value" : 1}, {"key_name" : "key2", "key_value" : 3}}
  -----|-----------------------
  bbb  | {{"key_name" : "key1", "key_value" : 0}, {"key_name" : "key2", "key_value" : 2}}
  -----|-----------------------
  aaa  | {{"key_name" : "key1", "key_value" : 50}, {"key_name" : "key2", "key_value" : 0}}
  </code></pre>
  
  <p><strong>Example_3:</strong></p>
  
  <pre><code>col1 | col2
  -----|-----------------------
  aaa  | [{"key_name" : "key1", "key_value" : 1}, {"key_name" : "key2", "key_value" : 3}]
  -----|-----------------------
  bbb  | [{"key_name" : "key1", "key_value" : 0}, {"key_name" : "key2", "key_value" : 2}]
  -----|-----------------------
  aaa  | [{"key_name" : "key1", "key_value" : 50}, {"key_name" : "key2", "key_value" : 0}]
  </code></pre>
  
  <p><strong>Example_4:</strong></p>
  
  <pre><code>col1 | col2
  -----|-----------------------
  aaa  | {"key1": {"key_name" : "key1", "key_value" : 1}, "key2": {"key_name" : "key2", "key_value" : 3}}
  -----|-----------------------
  bbb  | {"key1": {"key_name" : "key1", "key_value" : 0}, "key2": {"key_name" : "key2", "key_value" : 2}}
  -----|-----------------------
  aaa  | {"key1": {"key_name" : "key1", "key_value" : 50}, "key2": {"key_name" : "key2", "key_value" : 0}}
  </code></pre>