I'm sure there must be a simple way to do this, but I've been tearing my hair out for hours now and I'm getting nowhere. Here is a working query from a customer listing utility:
SELECT c.customer_ID, title, surname, forenames, COUNT(booking_ID) AS bookings
FROM customer c
LEFT JOIN booking b
ON c.customer_ID = b.customer_ID
WHERE customer_Live
GROUP BY c.customer_ID, surname, forenames, title
ORDER BY surname;
Here is the problem: the COUNT returns all of the related bookings. But the booking table has a 'booking_Live' column which is set to false whenever a booking is cancelled. What I need to do is somehow exclude cancelled bookings from the count; so if all a customer has is cancelled bookings, it will return 0. I've tried putting a HAVING clause on the group, but that just ends up removing any customers with zero live booking from the output.