This is a SQL question involving a join giving me more than I want.
I have 2 tables, an events table and singers table
EVENTS event_id event_name num_attendees tickets_sold tickets_promo event_date ... many other fields
SINGERS event_id singer
I’m trying to get event statistics from the events table if ANY of a list of singers performed.
EVENTS: event_id num_attendees tickets_sold 1 15 2500 2 5 1575 Singers at the event: event_id singer 1 bob 1 jane 2 bob
I’m interested in the statistics of events that either bob or jane sang in:
SELECT count(e.event_id) as count_events, sum(num_attendees) as sum_attentees,
sum(tickets_sold) as sum_tickets_sold
FROM `events` e
INNER JOIN singers s ON s.event_id = e.event_id
AND s.singer IN ("bob", "jane")
WHERE event_date ...
Gives me a wrong count and sums since the join will give me 2 records for event 1, and 1 record for event 2 and the count of events will be 3.
How should I write this SQL? Thanks.
1 Answer