WITH CTE AS (
SELECT *,
	row_number() OVER (PARTITION BY scrape_data._id ORDER BY scrape_data.time_stamp DESC) AS row_number
FROM knoebels."LZ_attractions_io" scrape_data
)
SELECT CTE.time_stamp
	,CTE._id
	,poi.name
	,CTE.is_operational
	,CTE.is_open
	,CTE.queue_status_message
	,last_online.time_stamp AS last_online
FROM CTE
JOIN knoebels."LZ_attractions_io_poi" AS poi ON CTE._id = poi._id
JOIN knoebels.ride_master rm ON CTE._id = rm.attractions_dot_io_id
LEFT JOIN (
	SELECT *,
	row_number() OVER (PARTITION BY scrape_data._id ORDER BY scrape_data.time_stamp DESC) AS row_number
	FROM knoebels."LZ_attractions_io" scrape_data
	where is_operational = true and is_open = true
) AS last_online
	ON CTE._id = last_online._id and last_online.row_number = 1
WHERE CTE.row_number = 1
	AND (CTE.is_operational = false OR (CTE.queue_status_message IS NOT NULL AND CTE.queue_status_message != ''))
ORDER BY name;