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 3957434
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:29:26+00:00 2026-05-20T02:29:26+00:00

If I need to get five+ different SUM values with similar WHERE conditions, is

  • 0

If I need to get five+ different SUM values with similar WHERE conditions, is the best (maybe only) way to do it through one query with five subqueries? For example, my query is:

SELECT user, 
(SELECT SUM(amount) FROM table t, table2 t2 WHERE t.table2_id = t2.id AND table2.type = 1 AND user = 1 AND date = x) AS one,
(SELECT SUM(amount) FROM table t, table2 t2 WHERE t.table2_id = t2.id AND table2.type = 2 AND user = 1 AND date = x) AS two, 
(SELECT SUM(amount) FROM table t, table2 t2 WHERE t.table2_id = t2.id AND table2.type = 3 AND user = 1 AND date = x) AS three, 
(SELECT SUM(amount) FROM table t, table2 t2 WHERE t.table2_id = t2.id AND table2.type = 4 AND user = 1 AND date = x) AS four, 
(SELECT SUM(amount) FROM table t, table2 t2 WHERE t.table2_id = t2.id AND table2.type = 5 AND user = 1 AND date = x) AS five 
FROM table 
WHERE user = 1

So far my queries have been very quick but I’m worried about the implication of five+ sub-queries when there’s many more records.

And out of curiosity does one query like this with five sub-queries have a performance advantage / dis-advantage over five individual queries? I was assuming it’s faster because it’s only one trip to the database vs 5?

EDIT: There is a table 2 of about 60 records that are used to classify the thousands of records in table 1. Each record in table 2 is categorized by a type value of 1 – 5 (which is what I’m using to group items I want the SUM of).

  • 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-05-20T02:29:27+00:00Added an answer on May 20, 2026 at 2:29 am
    1. Use ANSI JOINS
    2. You are better writing your query in PIVOT form like the query below

    Rewritten:

    SELECT t.user, 
    SUM(CASE table2.type WHEN 1 then amount else 0 end) As one,
    SUM(CASE table2.type WHEN 2 then amount else 0 end) As two,
    SUM(CASE table2.type WHEN 3 then amount else 0 end) As three,
    SUM(CASE table2.type WHEN 4 then amount else 0 end) As four,
    SUM(CASE table2.type WHEN 5 then amount else 0 end) As five
    FROM table t
    INNER JOIN table2 t2 ON t.table2_id = t2.id 
    WHERE t.user = 1
      AND table2.type in (1,2,3,4,5)
    

    I understand your query is pseudo but it has a hanging WHERE user without a FROM clause. As for performance, the above goes through the table in one pass. The 5x subquery form would have taken 5 passes, but should not be significantly slower if proper indexes are available.

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

Sidebar

Related Questions

I need to query the MYSQL with some condition, and get five random different
I need to get two summations in the same query based on different criteria.
I need 3 different queries in one page. When I do it I get
So I have one date as a string: 2011/06/01 I need to get the
I have 5 strings, such as: one, two, three, four, and five. I need
I have two arrays: keys_array = [2,4,5,8,9] values_array = ['two','four','five','eight','nine'] I need to get:
Just need get some vals located in application.ini(main ini) in the Controller plugin I
I need get all items these have no categories int? categoryId = null; var
I have two tables with a weak relation. I need get a text value
Need to get the 10 word before and 10 words after for the given

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.