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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:57:05+00:00 2026-06-09T05:57:05+00:00

I am having trouble with a query that I want to retrieve counts for

  • 0

I am having trouble with a query that I want to retrieve counts for multiple columns from the same table in the subqueries as the main query.

Here is my query:

SELECT 
    CM.entityId,
    (SELECT COUNT(*) 
     FROM _credit_control_main 
     WHERE invoiceAge < 14 AND entityId = CM.entityId) AS under14,
    (SELECT COUNT(*) 
     FROM _credit_control_main 
     WHERE invoiceAge >= 14 AND invoiceAge < 30 AND entityId = CM.entityId) AS under14to30,
    (SELECT COUNT(*) 
     FROM _credit_control_main 
     WHERE invoiceAge >= 30 AND invoiceAge < 60 AND entityId = CM.entityId) AS under30to60,
    (SELECT COUNT(*) 
     FROM _credit_control_main 
     WHERE invoiceAge >= 60 AND invoiceAge < 90 AND entityId = CM.entityId) AS under60to90,
    (SELECT COUNT(*) 
     FROM _credit_control_main 
     WHERE invoiceAge > 90 AND entityId = CM.entityId) AS over90,
    COUNT(*) AS iCount
FROM
    _credit_control_main AS CM
WHERE 
    ((CM.invoiceNet + CM.invoiceVat) - (creditNet+creditVat)) - (CM.paymentTotal - (CM.creditNet + CM.creditVat)) > 0.00
GROUP BY 
    entityId 
ORDER BY 
    `CM`.`entityId` ASC

If I create a basic query based on one of the subqueries I get the correct count, but as it is I get vastly inflated counts.

What am I doing wrong?

  • 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-09T05:57:07+00:00Added an answer on June 9, 2026 at 5:57 am

    I don’t know why your query is wrong but I know how you should write it instead. All these subcount are killers, try understanding this trick and you’ll gain a lot in readability and performance:

    SELECT
        CM.entityId,
        SUM(invoiceAge < 14) as under14,
        SUM(invoiceAge >= 14 AND invoiceAge < 30) as under14to30,
        SUM(invoiceAge >= 30 AND invoiceAge < 60) as under30to60,
        SUM(invoiceAge >= 60 AND invoiceAge < 90) as under60to90,
        SUM(invoiceAge > 90) as over90,
        COUNT(*) AS iCount
    FROM _credit_control_main AS CM
    WHERE ((CM.invoiceNet+CM.invoiceVat)-(creditNet+creditVat))-(CM.paymentTotal-(CM.creditNet+CM.creditVat)) > 0.00
    GROUP BY entityId
    ORDER BY `CM`.`entityId` ASC;
    

    And by the way, the invoiceAge = 90 fits in no category here.

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

Sidebar

Related Questions

so I'm having trouble properly creating this query. Basically, I want to insert multiple
I am having trouble getting teh results i want from this mysql query. And
I am having trouble pulling a query where i want to return results that
I am having trouble writing query so that I can query the content of
I'm having trouble coming up with a query that will find all customers who
I am having trouble with the following MYSQL query and are hoping that there
I'm having some trouble with finalizing my MySQL query. Imagine that i combine the
I'm having trouble building a query. I can do what I want in 3
I'm having trouble trying to define the SQL query for this table: There's a
I'm having trouble constructing an efficient query in EF4 using type per table (TPT)

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.