the query bellow, unfortunately returns the wrong total of orders. What I want is to know all users whose total orders exceed the sum of their diposit.
SELECT u.id, u.email,u.balance,sum(t.amount - t.fees) as total_disposits ,sum(o.charge) as spent
FROM USERS u
INNER JOIN TRANSACTIONS t ON u.id = t.uid
INNER JOIN ORDERS o ON u.id = o.uid
WHERE total_disposits < spent
GROUP BY u.id;
tables structure :
USERS
-----------------------------------
id | email | balance
-----------------------------------
1 | email1@app.com | 15.50
2 | email2@app.com | 10.00
3 | email3@app.com | 70.00
-----------------------------------
TRANSACTIONS
-----------------------------------
id | user_id | amount | fees
-----------------------------------
1 | 1 | 15.50 | 0.50
2 | 2 | 10.00 | 0.50
3 | 2 | 15.00 | 0.50
4 | 3 | 12.50 | 0.50
5 | 1 | 5.50 | 0.50
-----------------------------------
ORDERS
-----------------------------------
id | user_id | charge
-----------------------------------
1 | 1 | 15.50
2 | 2 | 10.00
3 | 2 | 15.00
4 | 3 | 12.50
5 | 1 | 5.50
-----------------------------------
Thank you