Workaround with 'Column Name is not valid'

时间秒杀一切 提交于 2019-12-11 05:10:14

问题


Hi is there any workaround with this one?

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

The following returns an error that '[Remaining Balance]' is not a valid column whereas i already declared it. i can't use it in both the WHERE and the CASE clauses. Is there any work around since i really need to get only the invoices that have still remaining balances.

Thanks for the help!


回答1:


You have to use HAVING clause like this

HAVING SUM(rinvoices.payment) - SUM(rinvoices.discount) <> 0

instead of WHERE

Ie:

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


来源:https://stackoverflow.com/questions/8815240/workaround-with-column-name-is-not-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!