I have three tables I am working with:
- users table contains users id, name and status
- supply_history table contains id, users_id, amount and status
- rejected_deal table contains id, users_id, rejected_amount and also status.
What I am trying to find is to sum up each users supply_amount and rejected amount where users status, supply_history status and rejected_deal status is 1.
Here is what I have tried so far.
SELECT users.id, users.name,
sum(supply_history.amount) as supply_amount,
sum(rejected_deal.rejected_amount) as rejected_amount
from users
inner join supply_history on supply_history.users_id=users.id
inner join rejected_deal on rejected_deal.users_id=users.id
where users.status='1' and rejected_deal.status='1' and supply_history.status='1'
GROUP by users.id;
My current query result is bellow.
| id | name | supply_amount | rejected_amount |
|---|---|---|---|
| 1 | Skipper | 50 | 20 |
| 2 | Private | 200 | 20 |
The result I am looking for
| id | name | supply_amount | rejected_amount |
|---|---|---|---|
| 1 | Skipper | 50 | 10 |
| 2 | Private | 100 | 20 |
Bellow is my three tables
- users table.
| id | name | status |
|---|---|---|
| 1 | Skipper | 1 |
| 2 | Private | 1 |
- supply_history table.
| id | users_id | amount | status |
|---|---|---|---|
| 1 | 1 | 20 | 1 |
| 2 | 1 | 30 | 1 |
| 3 | 2 | 100 | 1 |
- rejected_deal table.
| id | users_id | rejected_amount | status |
|---|---|---|---|
| 1 | 2 | 15 | 1 |
| 2 | 2 | 5 | 1 |
| 3 | 1 | 10 | 1 |
How do I solve this query please help. Thank you.