In Postgres 11.x I am trying to aggregate elements in a nested jsonb object which has an array field into a single row per device_id. Here's example data for a table called configurations.
| id | device_id | data |
|---|---|---|
| 1 | 1 | "{""sensors"": [{""other_data"": {}, ""sensor_type"": 1}], ""other_data"": {}}" |
| 2 | 1 | "{""sensors"": [{""other_data"": {}, ""sensor_type"": 1}, {""other_data"": {}, ""sensor_type"": 2}], ""other_data"": {}}" |
| 3 | 1 | "{""sensors"": [{""other_data"": {}, ""sensor_type"": 3}], ""other_data"": {}}" |
| 4 | 2 | "{""sensors"": [{""other_data"": {}, ""sensor_type"": 4}], ""other_data"": {}}" |
| 5 | 2 | "{""sensors"": null, ""other_data"": {}}" |
| 6 | 3 | "{""sensors"": [], ""other_data"": {}}" |
My goal output would have a single row per device_id with an array of distinct sensor_types, example:
| device_id | sensor_types |
|---|---|
| 1 | [1,2,3] |
| 2 | [4] |
| 3 | [ ] null would also be fine here |
Tried a bunch of things but running into various problems, here's some SQL to set up a test environment:
CREATE TEMPORARY TABLE configurations(
id SERIAL PRIMARY KEY,
device_id SERIAL,
data JSONB
);
INSERT INTO configurations(device_id, data) VALUES
(1, '{ "other_data": {}, "sensors": [ { "sensor_type": 1, "other_data": {} } ] }'),
(1, '{ "other_data": {}, "sensors": [ { "sensor_type": 1, "other_data": {} }, { "sensor_type": 2, "other_data": {} }] }'),
(1, '{ "other_data": {}, "sensors": [ { "sensor_type": 3, "other_data": {} }] }'),
(2, '{ "other_data": {}, "sensors": [ { "sensor_type": 4, "other_data": {} }] }'),
(2, '{ "other_data": {}, "sensors": null }'),
(3, '{ "other_data": {}, "sensors": [] }');
Quick note, my real table has about 100,000 rows and the jsonb data is much more complicated but follows this general structure.