I've got the following data structure:
[
{
"postID": 1,
"images": [
{"imageID": 1, "pos": 1, "uploaded": "2022-01-01", "tags": []},
{"imageID": 2, "pos": 2, "uploaded": "2022-01-01", "tags": []}
]
},
{
"postID": 2,
"images": [
{"imageID": 3, "pos": 1, "uploaded": "2022-01-01", "tags": []},
{"imageID": 4, "pos": 2, "uploaded": "2022-01-01", "tags": []}
]
}
]
How can I ORDER BY the most recent (or oldest) dates for each post, where the date is selected to be the most recent/oldest from each of the images? Note that I still want to maintain the order of the images according to the "pos" column.
This is my query that generates this data:
WITH image_tags AS (
SELECT images."post id",
json_build_object (
'imageID', images."image id",
'uploaded', images."uploaded",
'tags', json_agg("tag map".tag) ) AS image
FROM images
JOIN "tag map" ON images."image id" = "tag map"."image id"
GROUP BY images."post id", images."image id"
ORDER BY images."pos" DESC
)
SELECT posts."post id" AS "postID",
json_agg(image_tags.image) AS images
FROM posts
JOIN image_tags ON posts."post id" = image_tags."post id"
GROUP BY posts."post id"
--ORDER BY ?
Possible alternatives: Instead I move uploaded date to the post table, but these means that I won't be able to find the individual upload dates for each of the images. So that is a last resort only if this isn't possible to do.