I have a table like that:
id | lat | lon | date | speed
1 | 45 | 66 | 2018-02-01 | 139
1 | 44 | 63 | 2018-04-01 | 129
2 | 41 | 56 | 2018-01-01 | 109
2 | 44 | 45 | 2018-04-01 | 99
I wanna make a JSON like that:
{1:{path:[[45,66],[44,63]],speed:129}, 2:{path:[[41,56],[44,45]], speed:99}};
Where speed is taken at last date;
Without the speed parameter, the solution is
SELECT jsonb_object_agg(id,j)
FROM (
SELECT id, jsonb_agg(jsonb_build_array(lat,lon)) AS j
FROM foo
GROUP BY id
) AS t;
How can it be modified to include last speed?
- Postgresql 9.6