Got a PostgreSQL query that works well but doesn't seem very efficient...
SELECT
_id,
location,
day,
title,
teacher,
canceled,
CASE
WHEN day <= EXTRACT(dow from CURRENT_DATE)::integer THEN CURRENT_DATE + day + (6 - EXTRACT(dow from CURRENT_DATE)::integer) + "startTime"
WHEN day > EXTRACT(dow from CURRENT_DATE)::integer THEN CURRENT_DATE + (day - EXTRACT(dow from CURRENT_DATE)::integer - 1) + "startTime"
END AS "startTime",
CASE
WHEN day <= EXTRACT(dow from CURRENT_DATE)::integer THEN CURRENT_DATE + day + (6 - EXTRACT(dow from CURRENT_DATE)::integer) + "endTime"
WHEN day > EXTRACT(dow from CURRENT_DATE)::integer THEN CURRENT_DATE + (day - EXTRACT(dow from CURRENT_DATE)::integer - 1) + "endTime"
END AS "endTime"
FROM "Schedules"
ORDER BY location, day, "startTime";
In this case, day column is the day of the week (1-7, not 0-6). It displays dates from the current date onward so if it's Wednesday (day 4), it shows records with day = 4 as the current date, day = 5 is current date + 1, etc. If the day of week is lower, it shows next week's dates.
Would anyone have any suggestions regarding optimizing this?