The query that you tried seems to show that you have the intuition that group by will help you, but misuse it in the details.
Grouping will effectively help you to generate one output rows from multiple rows fetched from the joins.
Before grouping you should ask yourself what reality each row represents, and what columns are duplicates regarding your problem: they are the ones you will group on.
For example, with the following data (intermediate results of an hypothetical join on the database of an hypothetical clothes seller):
| customer_id | first_name | last_name | order_id | quantity | what | 
|---|---|---|---|---|---|
| 1 | John | Smith | 1000 | 1 | trousers | 
| 1 | John | Smith | 1001 | 1 | sock | 
| 1 | John | Smith | 1002 | 1 | sock | 
| 2 | Jane | Smith | 1003 | 2 | sock | 
| 3 | John | Smith | 1004 | 1 | trousers | 
You can choose to group by against two different axes, depending on what you're interested in:
customer_id, first_name, last_name).customer_id being your primary key, you would avoid a lot of problems if you only worked with customer_id for the 1..n relation (1 customer has multiple orders), group by on it, then only enrich your data for display with the customer's names:
GROUP BY, because all those 3 columns represent the physical reality of a unique customer)joins.customer join orders join withdrawals would return 6 rows, one for each combination of withdrawal and order, so you would apparently end up with 2 trousers.select per group by may be a strategy to think of, to reduce your relations to 1..1 (each customer that has n orders, has only 1 count of orders) and then do your grand-join for final display.\
This is a general warning that I thought of while seing the number of joins you have, with PedidoRecarga which role is unclear to me.group by what and only that because this is what represents a distinct product. The quantity does not distinguish one product from another, they are an attribute of the order, not of the product, and it doesn't make the product nor the order unique: as you see two orders can have the same quantity, exactly as two customers can have the same name, but it doesn't make them the same "entity" in reality. So if you group by quantity too, your 2 orders (customer bought 1 sock, then remembered that by chance he had got two feet, and came back to the shop and bought the other sock) will appear as 1, and your inventory will tell that you've sold only 1 sock while in fact you've sold 1 + 1.group by only on what uniquely defines the reality you want to represent (such as a primary key).sum(), because you sold 1 + 1 sock to customer 1).group by only on what, which is the PK of each product, not on quantity which is an attribute of the order but not even on order_id which is the PK of the order, not the product, and would only be useful for a per-order listing.The problem I see in your query is that you group by (nearly) every field you encounter so probably you first have to question yourself about what reality do I want a unique row for in my end result?