+2  A: 

Simple solution: Use two queries.

Otherwise you can do your aggregated calculation in a subquery (on the whole table, not per row) then JOIN the result of the subquery with the addresses table to get your extra data. Try this:

SELECT *
FROM customer T1
LEFT JOIN
(
    SELECT custId,
           SUM(total) AS sum_total,
           COUNT(total) AS count_total
    FROM orders
    -- WHERE ...
    GROUP BY custId
) T2
ON T1.custId = T2.custId
-- WHERE ...
Mark Byers
Thanks for the answer. I tried it now but it didn't work for me. it seems I cannot access the outer tables from within the subselect. See my edits please.
codymanix
@codymanix: You can't access an alias from an inner query in an outer query.
Mark Byers
No I want to access an alias from the outer query in the inner query. I want to use the customer c which is defined in the outer query in the where clause of the inner query.
codymanix
@codymanix: Why do you want to do that? If custId is a PK for the customer table, doesn't it just give the same result in the end?
Mark Byers
@codymanix: Your question is not clear. Perhaps you could state your full requirements and explain why this answer doesn't meet those requirements.
Mark Byers
A: 

Try something like:

select c.custId, max(c.custName) custName, ...
       a.addrId, max(a.addrLine1) addrLine1, ...
       sum(o.total) order_total, count(o.total) order_count
from customer c 
left join orders o on c.custId = o.custId
left join cust_addresses a on c.custId = a.custId
group by c.custId, a.addrId;

assuming you want all customers, whether or not they have any orders or addresses.

Mark Bannister
But then the sum and count is not displayed correctly if the customer has more than one order or address.
codymanix
@codymanix, did you include the cust_address key field (which I have called addrId, since you haven't posted the table structure) in the group by clause of your query? If so, the sum and count should be returned correctly.
Mark Bannister
but then I have multiple rows for one customer returned.
codymanix
@codymanix: yes, where there is more than one address per customer, you will get one line per address per customer. You **explicitly** said in your question that you wanted to display "all their addresses"; this query resolves the aggregate function problem **and** displays all their addresses.
Mark Bannister