Skip to content

Instantly share code, notes, and snippets.

@danbeaulieu
Created November 8, 2012 21:20
Show Gist options
  • Save danbeaulieu/4041689 to your computer and use it in GitHub Desktop.
Save danbeaulieu/4041689 to your computer and use it in GitHub Desktop.
SELECT st.id, sum(r.number_of_people) as people_total
FROM scheduled_tours as st
LEFT JOIN reservations AS r ON st.id = r.scheduled_tour_id
LEFT JOIN products AS p ON p.id = st.product_id
WHERE st.date = '2012-11-08'
GROUP BY st.id, p.max_capacity
HAVING SUM(r.number_of_people) IS null OR SUM(r.number_of_people) < p.max_capacity;
ScheduledTour.
joins(:reservations, :product).
where("date = ?", '2012-11-08').
group("scheduled_tours.id, scheduled_tours.product_id, scheduled_tours.created_at, scheduled_tours.updated_at, scheduled_tours.date, scheduled_tours.comments, scheduled_tours.cancelled, scheduled_tours.on_the_fly, scheduled_tours.product_schedule_id, scheduled_tours.latest_api_reservation, products.max_capacity").
having("sum(reservations.number_of_people) < products.max_capacity")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment