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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:05:26+00:00 2026-06-06T06:05:26+00:00

I am having a problem with my query. I want to use SUM in

  • 0

I am having a problem with my query.

I want to use SUM in my WHERE but it keeps giving me Invalid use of group function.

AND SUM((invoices_out.net+invoices_out.vat)/currencies.rateVsPound)
>
(SELECT SUM(jobs_payments_accounts.amount/currencies.rateVsPound)
 FROM jobs_payments_accounts, jobs, enquiries, currencies
 WHERE jobs_payments_accounts.jobRef = jobs.id
 AND jobs_payments_accounts.currencyRef = currencies.id
 AND enquiries.id = jobs.enquiryRef
 AND enquiries.entityRef = ed.id)

The right side of > works perfectly, but how can I achieve what I’m trying to do with the left side?

Here is the full subquery:

IF(ed.paymentTermsRef = 3,
(SELECT SUM((invoices_out.net+invoices_out.vat)/currencies.rateVsPound)
FROM enquiries, jobs, (SELECT * FROM invoices_out_reference GROUP BY invoiceRef) AS iorUnique,
                                (SELECT enquiryRef, sellingCurrency FROM parts_trading GROUP BY enquiryRef) AS PTU,
                                currencies,
                                invoices_out
                                    WHERE enquiries.entityRef = ed.id
                                    AND enquiries.id = jobs.enquiryRef
                                    AND PTU.enquiryRef = enquiries.id
                                    AND PTU.sellingCurrency = currencies.id
                                    AND jobs.id = iorUnique.jobRef
                                    AND iorUnique.invoiceRef = invoices_out.id
                                    AND invoices_out.paid = 0
                                AND SUM((invoices_out.net+invoices_out.vat)/currencies.rateVsPound)
                                    >
                                    (SELECT SUM(jobs_payments_accounts.amount/currencies.rateVsPound)
                                         FROM jobs_payments_accounts, jobs, enquiries, currencies
                                         WHERE jobs_payments_accounts.jobRef = jobs.id
                                         AND jobs_payments_accounts.currencyRef = currencies.id
                                         AND enquiries.id = jobs.enquiryRef
                                         AND enquiries.entityRef = ed.id)
                                #AND jobs.id NOT IN
                                #       (SELECT jobRef FROM jobs_payments_accounts)
                                #   AND jobs.id NOT IN
                                #       (SELECT jobRef FROM jobs_payments_advance)
                                AND jobs.id IN
                                    (SELECT jobRef FROM invoices_out_reference)
                                AND DATEDIFF(NOW(), invoices_out.date) >= 30),
                                NULL
                             )

Tried implementing what you suggested with IS NULL but it makes the query much slower.

Still having a lot of trouble with this, hopefully I will crack it soon somehow.

  • 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-06T06:05:32+00:00Added an answer on June 6, 2026 at 6:05 am

    The first rule of the “Aggregate functions users club” is:

    You don’t use aggregate functions in WHERE

    The second rule of the “Aggregate functions users club” is:

    You don’t use aggregate functions in WHERE

    HAVING clause has to be used in such cases: here’s a great topic to study some intricacies of it.

    And I really suggest restructuring your query (why use nested subqueries to check jobs.id in three different tables instead of LEFT JOIN ... ON id IS NULL, for example?) or dividing it into smaller ones with temporary tables. No offence, but when looking at what you’ve quoted, I actually can hear your DB cries.

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

Sidebar

Related Questions

I'm having problem restricting a query in mdx, using except function at where clause.
I'm having a misunderstanding problem with this use of Linq Query I do have
I'm having a problem with a query prepared in PHP with PDO. The code:
I'm having problem with sorting my query results by the count field. My simplified
I'm having a problem with this query that takes several seconds to complete. I
i am having a problem adding COUNT to my query. the query works fine
I'm having problem with language mappings. The way I want it to work is
i'm having a problem with a Cursor and a SimpleCursorAdapter. What i want to
I have a problem regarding a query where i want to only display data
I'm having trouble with an ExpandableListView. What I want is for each group to

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.