I have a problem and I hope there is a simple solution. I will try to make it as simple as possible:
- The ticket belongs to the participant
- Example:
select * from tickets JOIN attendees ON attendee.id = tickets.attendee_id
- The visitor has a decimal column called income
However, I need to run a query that will return a variety of ticket information, including total revenue. The problem is that if 2 tickets belong to the same participant, he calculates their income twice. How can I sum a memberβs income only once?
I do not want to use subqueries as my ORM makes this difficult. Also, the subquery solution does not scale if I want to do this for multiple columns.
Here is what I have:
- 1 participant with income from 100
- 2 tickets owned by this member
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 member in the database with existing state_100. I DO NOT want the visitor to be counted twice.
Please let me know if possible.
sql duplicate-removal aggregate-functions postgresql window-functions
Binary Logic Nov 01 2018-12-11T00: 00Z
source share