Skip to content

Instantly share code, notes, and snippets.

@197291
Created January 5, 2020 14:08
Show Gist options
  • Save 197291/5a3508a5e738c1f445eb3cfe1170f6e8 to your computer and use it in GitHub Desktop.
Save 197291/5a3508a5e738c1f445eb3cfe1170f6e8 to your computer and use it in GitHub Desktop.
SQL_QUERY
SELECT b.id as build_id, b.name, c.city_name
FROM public."Buildings" b LEFT JOIN
public."Cities" c
ON c.id = b.city_id LEFT JOIN
public."OperatingHours" h
ON h."BuildingId" = b.id LEFT JOIN
public."Reservations" r
on r."BuildingId"=b.id
WHERE c.city_name = $city AND a.size = $size
GROUP BY b.id, c.city_name
HAVING COUNT(*) FILTER (WHERE h.day = 1 AND '15:00:00' BETWEEN h.start AND h.end) > 0
AND
COUNT(*) FILTER (WHERE h.day = 1 AND '15:30:00' BETWEEN h.start AND h.end) > 0
AND
COUNT(*) FILTER (
WHERE r.during && tstzrange('2020-01-20 15:00:00+00', '2020-01-20 15:30:00+00', '[)')
) = 0;
SELECT b.id as build_id, b.name, c.city_name, b.rating, b.price
FROM public."Buildings" b LEFT JOIN
public."Cities" c
ON c.id = b.city_id LEFT JOIN
public."OperatingHours" h
ON h."BuildingId" = b.id LEFT JOIN
public."Reservations" r
ON r."BuildingId"=b.id
public."Attendees" a
ON a."BuildingId" = b.id
WHERE c.id = 3
GROUP BY b.id, c.city_name
HAVING COUNT(*) FILTER (WHERE h.day = 1 AND '15:00:00' BETWEEN h.start AND h.end) > 0
AND
COUNT(*) FILTER (WHERE h.day = 1 AND h.end='23:59:00') > 0
AND
COUNT(*) FILTER (WHERE h.day = 2 AND '11:00:00' BETWEEN h.start AND h.end) > 0
AND
COUNT(*) FILTER (WHERE h.day = 2 AND h.start ='00:00:00') > 0
AND
COUNT(*) FILTER (
WHERE r.during && tstzrange('2020-01-20 15:00:00+00', '2020-01-21 11:00:00+00', '[)')
) = 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment