Order costumers by the amount of invoices and sum of items : I'm trying to order my costumers by the amount of invoices and the sum of the values of invoices. To get the value of a invoice, I need to sum the items, that can (or not) be in 2 tables: products and support. The structure is something like this: 

[![][1]][1]

I tried this query, but doesn't works: 

    SELECT 
        c.id, 
        c.name, 
        COUNT(i.id) as total_amount, 
        SUM(ip.value * ip.quantity) as total_products,
        SUM(is.value) as total_support
    FROM Invoice i
    INNER JOIN Costumer c ON i.id_costumer = c.id
    LEFT JOIN InVoice_Product ip ON ip.id_invoice = i.id
    LEFT JOIN InVoice_Support sp ON sp.id_invoice = i.id
    GROUP BY c.id, c.name
    ORDER BY total_amount, total_products DESC

If I exec the query without the LEFT JOINS, only looking for the amount of invoices, works great.. But when I try to add the JOINS, the results doesn't match with reality..

Anyone can help? I'm stucked on this..



  [1]: https://i.stack.imgur.com/mInwX.png