3

我的查询有问题。

我想在我的 WHERE 中使用 SUM,但它一直给我无效使用组函数。

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)

> 的右侧工作完美,但我怎样才能实现我想要用左侧做的事情?

这是完整的子查询:

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
                             )

尝试使用 IS NULL 实现您的建议,但这会使查询速度变慢。

仍然有很多麻烦,希望我能尽快破解它。

4

2 回答 2

13

“聚合函数用户俱乐部”的第一条规则是:

您不要在 WHERE 中使用聚合函数

“聚合函数用户俱乐部”的第二条规则是:

您不要在 WHERE 中使用聚合函数

HAVING在这种情况下必须使用从句:这是一个很好的主题来研究它的一些复杂性。

而且我真的建议重组您的查询(例如,为什么要使用嵌套子查询来签jobs.id入三个不同的表而不是LEFT JOIN ... ON id IS NULL?)或将其划分为具有临时表的较小的查询。没有冒犯,但是当看到你引用的内容时,我实际上可以听到你的 DB 哭声。

于 2012-06-22T11:16:27.430 回答
8

您必须使用HAVING聚合函数:

select ...
from ...
where ...
group by ...
having sum(...) > ...
于 2012-06-22T11:11:56.643 回答