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

  • Home
  • SEARCH
  • 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 7896067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:47:53+00:00 2026-06-03T07:47:53+00:00

I have calculated the sum of a count in different tables. This is done

  • 0

I have calculated the sum of a count in different tables. This is done twice, once for each performanceID. Now I want to get the sum of the two sums.

Below is the code for the two sums that I do at the moment:

    SELECT SUM((COUNT (BookingID) * CategoryPrice)) AS TotalAmount
    FROM Booking, Production
    WHERE Booking.PerformanceID IN(SELECT PerformanceID FROM Performance WHERE PerformanceID = '1')
    and Production.ProductionID IN 
    (SELECT ProductionID FROM Performance WHERE PerformanceID = '1') 
    GROUP BY BookingID, CategoryPrice
    UNION ALL
    SELECT SUM((COUNT (BookingID) * CategoryPrice)) AS TotalAmount
    FROM Booking, Production
    WHERE Booking.PerformanceID IN(SELECT PerformanceID FROM Performance WHERE PerformanceID = '2')
    and Production.ProductionID IN 
    (SELECT ProductionID FROM Performance WHERE PerformanceID = '2')
     GROUP BY BookingID, CategoryPrice

The results I get are:

TOTALAMOUNT
-----------
         70
         60 

How do I sum up the two sums here?

  • 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-03T07:47:54+00:00Added an answer on June 3, 2026 at 7:47 am

    I’m never going to compete with the FGITW but I have to say something about this query…

    If we add whitespace I hope you’ll see what I mean:

    SELECT SUM( (COUNT(BookingID) * CategoryPrice) ) AS TotalAmount
      FROM Booking
         , Production
     WHERE Booking.PerformanceID IN ( SELECT PerformanceID 
                                       FROM Performance 
                                      WHERE PerformanceID = '1')
       AND Production.ProductionID IN ( SELECT ProductionID FROM Performance 
                                         WHERE PerformanceID = '1') 
     GROUP BY BookingID, CategoryPrice
     UNION ALL
    SELECT SUM( (COUNT(BookingID) * CategoryPrice)) AS TotalAmount
      FROM Booking
         , Production
     WHERE Booking.PerformanceID IN ( SELECT PerformanceID 
                                        FROM Performance 
                                       WHERE PerformanceID = '2')
       AND Production.ProductionID IN ( SELECT ProductionID 
                                          FROM Performance 
                                         WHERE PerformanceID = '2')
     GROUP BY BookingID, CategoryPrice
    

    Breaking the query down the only reason that you got two rows returned were the analytic functions and the union all.

    1. You’re doing a cartesian join between booking and production, this means that you multiply the number of rows in each by each other.
    2. Your sub-selects on performance are returning one value, which is already known. There’s no reason to do them at all.
    3. You’re implicitly converting numbers into strings and back into numbers again.
    4. You’re scanning a table or index 8 times here!

    It appears as though you want the total amount taken for each performance in which case your query can be simplified to the following:

    SELECT SUM(bookings * CategoryPrice)
      FROM ( SELECT CategoryPrice , count(*) as bookings
               FROM Booking b
               JOIN performance per
                 ON p.performanceid =  per.performanceid
               JOIN Production p
                 ON p.productionid = per.productionid
              WHERE p.performanceid in (1, 2)
              GROUP BY CategoryPrice
                    )
    

    Please note the explicit join syntax, this has been around for a few decades, makes things a lot clearer and helps stop mistakes. This query will do two range scans, one of booking and one of production, assuming you have indexes on performanceid on both tables. It’ll also do a unique scan of performance assuming that performanceid is the primary key of this table.

    As an explanation of what this does, now I’ve finally managed to get your schema correct! We select the two performances, 1 and 2. We then select every production related to those performances and every booking related to those productions. You may be able to simplify this further depending on what table categoryprice is in. We then get the number of bookings per categoryprice and sum the product of these to give you the total value.

    As a bit of advice, I would always recommend understanding what values you expect to be returned from a query before you accept that your query is correct. The very best can and do make mistakes. Being able to catch them because you can see that the returned values are incorrect will help.

    Further Reading:

    • Join (SQL) – Wikipedia
    • A Visual Explanation of SQL Joins – Coding Horror
    • In Function – Tech on the Net
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a calculated field in a list with this formula: =CID & -
I want to have a model with calculated fields that I can apply sorting
I have a problem with SHA-1 performance on Android. In C# I get calculated
I want a linq query to return a calculated timespan, i have used the
I have this code which calculates the sum of "letter numbers" in a string,
I have a dataset which includes some numbers, I want to calculate the sum
I want to have a formula that calculates the sum of a given range.
I have two mysql tables and i want to do a select query: My
I have this query.. SELECT ClassId, Sum(TeachersCount) as NumCount FROM ClassSubject GROUP BY ClassId
Hi I have a query in PHP that calculates the sum of all the

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.