I am trying to figure out the correct syntax to exclude all invoices when the net amount is $0 due to returns.
I was able to extract the original invoice on the return invoices so that I have the original invoice record for, say $12 then a return invoice now populated in the invoice number field of the original invoice the return is for with a negative $12. So when these are grouped, since it's on the same invoice number now, the net amount of $12 - $12 = $0
Or whichever way to excludes these invoices since they cancel each other out.
They do not want to see these invoices that cancel each other out.
How about for those invoices that are actually $0?
Can there be something like IF there's more than one of the same invoice and that equals $0, then exclude it? Otherwise it needs to show the other invoices.
SELECT it.*
FROM dbo.invoice_table it
INNER JOIN (
SELECT invoice_number
FROM dbo.invoice_table
GROUP BY invoice_number
HAVING SUM(invoice_amount) <> 0 AND COUNT(*) > 1
) AS it_nonzero ON it_nonzero.invoice_number = it.invoice_number
You'd need to cast it as decimal(19,3), or some other numeric format, before summing it then. Of course with varchar you always run the risk that there will be non-numeric data in the column that causes an error when you cast/convert it.