I would like to know if there is a way or an existing function to perform an aggregation on a JSON field by doing the average of values if the key appears multiple times with PostgreSQL. The json's result keys are unknow and have to be add to it if these keys are meet during the aggregate function. They can't be define on the query.
datetime data
2018-05-06 18:17:00 {"speed":23.3,"orientation":1.3,"o2":75.2,"pm25":12.1}
2018-05-06 19:17:00 {"speed":20.3,"pm25":13.1}
2018-05-07 15:02:00 {"speed":21.3,"orientation":1.3,"pm10":72.2}
Imagine here we want the result aggregate by day
The result wanted :
day data
2018-05-06 {"speed":21.8,"orientation":1.3,"o2":75.2,"pm25":12.6}
2018-05-07 {"speed":21.3,"orientation":1.3,"pm10":72.2}
The first two rows has been aggregate doing an average on recurrent keys and kept all of the existing keys.
Best Answer
I don't know how to do this, so I'll learn myself as we go!
Test table and data:
First, we don't need the time component of the data, so we cast:
Next, we want to decompose the JSON object to its individual elements:
Now split out the key<>value pairs from the above:
Aggregate the above data:
Now turn the above resultset into JSON: