嗨,有没有解决方法?
SELECT invoices.no, customers.name, invoices.mdcode,
SUM(iproducts.unitprice * iproducts.quantity) AS total,
SUM(iproducts.unitprice * iproducts.quantity) - SUM(rinvoices.payment + rinvoices.discount)
AS [Remaining Balance]
FROM invoices INNER JOIN
customers ON customers.id = invoices.customerid INNER JOIN
iproducts ON invoices.id = iproducts.invoiceid LEFT OUTER JOIN
rinvoices ON invoices.id = rinvoices.invoiceid
WHERE ([Remaining Balance] <> '0')
GROUP BY invoices.no, customers.name, invoices.mdcode
以下内容返回“[剩余余额]”不是有效列的错误,而我已经声明了它。我不能在WHERE和CASE子句中使用它。是否有任何解决方法,因为我真的只需要获得仍有剩余余额的发票。
感谢您的帮助!
答案 0 :(得分:1)
你必须像这样使用HAVING子句
HAVING SUM(rinvoices.payment) - SUM(rinvoices.discount) <> 0
而不是WHERE
即:
SELECT invoices.no, customers.name, invoices.mdcode,
SUM(iproducts.unitprice * iproducts.quantity) AS total,
SUM(iproducts.unitprice * iproducts.quantity) - SUM(rinvoices.payment + rinvoices.discount)
AS [Remaining Balance]
FROM invoices INNER JOIN
customers ON customers.id = invoices.customerid INNER JOIN
iproducts ON invoices.id = iproducts.invoiceid LEFT OUTER JOIN
rinvoices ON invoices.id = rinvoices.invoiceid
GROUP BY invoices.no, customers.name, invoices.mdcode
HAVING SUM(rinvoices.payment) - SUM(rinvoices.discount) <> 0