Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8759733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:52:40+00:00 2026-06-13T14:52:40+00:00

I have a problem here, and I’m hoping there is an easy solution. I’ll

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:52:42+00:00Added an answer on June 13, 2026 at 2:52 pm

    To get the result without subquery, you have to resort to advanced window function trickery:

    SELECT sum(count(*))       OVER () AS tickets_count
         , sum(min(a.revenue)) OVER () AS atendees_revenue
    FROM   tickets   t
    JOIN   attendees a ON a.id = t.attendee_id
    GROUP  BY t.attendee_id
    LIMIT  1;
    

    sqlfiddle

    How does it work?

    The key to understanding this is the sequence of events in the query:

    aggregate functions -> window functions -> DISTINCT -> LIMIT

    More details:

    • Best way to get result count before LIMIT was applied

    Step by step:

    1. I GROUP BY t.attendee_id – which you would normally do in a subquery.

    2. Then I sum over the counts to get the total count of tickets. Not very efficient, but forced by your requirement. The aggregate function count(*) is wrapped in the window function sum( ... ) OVER () to arrive at the not-so-common expression: sum(count(*)) OVER ().

      And sum the minimum revenue per attendee to get the sum without duplicates.

      You could also use max() or avg() instead of min() to the same effect as revenue is guaranteed to be the same for every row per attendee.

      This could be simpler if DISTINCT was allowed in window functions, but PostgreSQL has not (yet) implemented this feature. Per documentation:

      Aggregate window functions, unlike normal aggregate functions, do not
      allow DISTINCT or ORDER BY to be used within the function argument list.

    3. Final step is to get a single row. This could be done with DISTINCT (SQL standard) since all rows are the same. LIMIT 1 will be faster, though. Or the SQL-standard form FETCH FIRST 1 ROWS ONLY.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem here and no have ideia to solution this. I have
I have a problem here when I try to load these functions in this
I have a problem here with this code. I'm opening a socket, then listening
i have a problem here, i want to make a multiple toolbar which works
I have some problem here. Here it is: I have this class public class
I have another problem here. I have few repeating groups of divs. There is
i have a problem here. I need it so if there is no text
I have problem here, I need all the stuffs inside this $_SESSION['cart'] thing and
Hi I have a problem here This is my code, its working when I
So I have this problem here: If we list all the natural numbers below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.