I'm aware of using ORDER BY in window queries when you use array aggregations, but how do you order this in combination with a group by?
Specifically, I want to make sure rooms and dates are ordered so that I can use that in a subsequent query. For example in the query below they need to be of descending order to make the search work :)
Alternatively, are maps ordered, or do they have no order?
WITH mapped as (
SELECT
guestid,
map_agg(room, startdate) as startdates,
array_agg(startdate) as dates,
array_agg(room) as rooms
FROM (VALUES
(1, 'room 1', date('2024-01-01')),
(1, 'room 2', date('2024-01-10')),
(1, 'out', date('2024-01-20')),
(2, 'room 2', date('2024-01-10')),
(2, 'room 1', date('2024-01-25')),
(2, 'out', date('2024-02-10'))
) AS checkins(guestid, room, startdate)
GROUP BY guestid
)
SELECT
guestid,
dt_utc,
rooms,
dates,
array_position(transform(dates, d -> d <= dt_utc), true) as p
-- , rooms[p]
FROM mapped
CROSS JOIN (VALUES
(date('2024-01-02')),
(date('2024-01-12')),
(date('2024-02-11'))
) AS dates(dt_utc)