I have the following table structure and data:
DumpTime | ProcId | NodeId | Stats |
---|---|---|---|
2022-07-07 13:03:03 | 9 | NODE_IDD02 | {"First":1,"Second":5,"Third":2} |
2022-07-07 13:17:35 | 8 | NODE_IDD03 | {"First":2,"Second":3,"Third":4} |
2022-07-07 13:22:35 | 9 | NODE_IDD02 | {"First":0,"Second":3,"Third":0} |
2022-07-07 13:03:03 | 0 | NODE_IDD01 | {"First":0,"Second":0,"Third":5} |
2022-07-07 13:17:35 | 8 | NODE_IDD03 | {"First":4,"Second":1,"Third":1} |
2022-07-07 13:22:35 | 0 | NODE_IDD01 | {"First":3,"Second":3,"Third":3} |
I have to group the rows using ProcId and NodeId and add the respective values of the JSON elements in Stats (The DumpTime has no significance).
The output should be:
DumpTime | ProcId | NodeId | Stats |
---|---|---|---|
2022-07-07 13:03:03 | 9 | NODE_IDD02 | {"First":1,"Second":8,"Third":2} |
2022-07-07 13:17:35 | 8 | NODE_IDD03 | {"First":6,"Second":4,"Third":5} |
2022-07-07 13:03:03 | 0 | NODE_IDD01 | {"First":3,"Second":3,"Third":8} |
I am able to get the sum of respective json elements but it is the total sum and not grouped. I used sum(json_extract())
for it.
Also, output has to be in the specified form so that it can be unmarshaled into a struct. What should be the sql query to achieve the same?