You need left outer join
so you get all customers:
SELECT DR.ACCNO,
COALESCE(SUM(Analytics_SaleInvoice.SalesValueTaxExclusive), 0) as Total
FROM DR_ACCS DR LEFT OUTER JOIN
Analytics_SaleInvoice Analytics_SaleInvoice
ON Analytics_SaleInvoice.AccountNumberAnalysis = DR.ACCNO AND
Analytics_SaleInvoice.AgePeriod = 5
WHERE DR.X_TOPCUSTOMER = 'Y'
GROUP BY DR.ACCNO, DR.ACCNO,Analytics_SaleInvoice.AccountNumberAnalysis
ORDER BY COALESCE(SUM(Analytics_SaleInvoice.SalesValueTaxExclusive), 0) DESC;
Along with the left outer join
, the condition on the second table needs to be moved to the on
clause and the coalesce()
is used to turn the NULL
value into a 0
.
I also added an order by
because you seem to want the best customers first.