I have a problem with an sql query that will not work if I try to use alias (scount, samount). The query runs ok without alias and it also work fine using only the first (scount) but as soon as I add the second one (samount) the query fails to execute. I assume this is related to that I do a minus query for some reason.
Can anyone help me resolve this so that I can return the values with the aliases.
select count(t.invoiceID) scount, sum(amount) samount -
IFNULL(
(
select sum(p.amount) as pamount
from invoice t, invoiceFactoring ift, InvoiceType it, Payment p
where t.issuerID = 38
AND ift.invoiceID = t.invoiceID
AND t.invoiceID = p.invoiceID
AND it.invoiceTypeID = t.invoiceTypeID
AND now() > date_add(t.invoiceExpiryDate, INTERVAL 45 DAY)
)
,0)
from invoice t, invoiceFactoring ift, InvoiceType it
where issuerID = 38
AND ift.invoiceID = t.invoiceID
AND it.invoiceTypeID = t.invoiceTypeID
AND now() > date_add(t.invoiceExpiryDate, INTERVAL 45 DAY)
AND t.disabled = 0
AND it.typeCategory = 1
alias goes after calculation.
P.S. clarification
your query is essentially this:
just replace it with
and accept the answer if it helps you because you will hardly get another until you start accepting.