Get the distinct sum of a joined table column

前端 未结 4 2064
花落未央
花落未央 2020-11-27 23:14

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
  • Exampl
4条回答
  •  醉酒成梦
    2020-11-27 23:31

    Previous answer is nearly correct. You just need to make distinct work well in case identical revenues. You can fix this really simple if your id has numeric type:

    SELECT
      COUNT(*) AS ticketsCount,
      SUM(DISTINCT attendees.id + attendees.revenue) - SUM(DISTINCT attendees.id) AS revenueSum
    FROM
      tickets
    LEFT JOIN attendees ON
      attendees.id = tickets.attendee_id
    

提交回复
热议问题