I have a problem here, and I'm hoping there is an easy solution. I'll try to make this as simple as possible:
- A ticket belongs to an attendee
- Example:
select * from tickets JOIN attendees ON attendee.id = tickets.attendee_id
- An attendee has a decimal column called "revenue"
That said, I need to run a query that will return a variety of information about the tickets, including the total revenue. The problem is that if 2 tickets belong to the same attendee, it counts their revenue twice. How can I sum the attendee revenue only once?
I don't want to use subqueries as my ORM makes this difficult. Plus a sub query solution doesn't scale if I want to do this for multiple columns.
Here's what I have:
- 1 attendees with a revenue of 100
- 2 tickets that both belong to that attendee
Select count(tickets.*) as tickets_count
, sum(attendees.revenue) as atendees_revenue
from tickets LEFT OUTER JOIN attendees ON attendees.id = tickets.attendee_id;
=> This tells me that attendees_revenue
is 200. I want it to be 100. Since there is one attendee in the database with an existing_revenue of 100. I do NOT want the attendee to be double counted.
Please let me know if this is possible.