coryd.dev/queries/views/media/shows/scheduled_episodes.psql

26 lines
756 B
Text

CREATE OR REPLACE VIEW optimized_scheduled_episodes AS
SELECT
se.show_id,
se.season_number,
se.episode_number,
se.status,
se.air_date,
(
SELECT CONCAT('S', se2.season_number, 'E', se2.episode_number)
FROM scheduled_episodes se2
WHERE se2.show_id = se.show_id
AND se2.status IN ('upcoming', 'aired')
ORDER BY se2.air_date ASC
LIMIT 1
) AS next_scheduled_episode,
(
SELECT se2.air_date
FROM scheduled_episodes se2
WHERE se2.show_id = se.show_id
AND se2.status IN ('upcoming', 'aired')
ORDER BY se2.air_date ASC
LIMIT 1
) AS next_air_date
FROM scheduled_episodes se
WHERE se.status IN ('upcoming', 'aired')
GROUP BY se.show_id, se.season_number, se.episode_number, se.status, se.air_date;