I Have two queries Query 1:
SELECT
e.eid, e.item as 'ITEM', SUM(s.qty) as 'TOTAL SOLD'
from
tbl_sales s,
tbl_matentry e
WHERE
e.eid = s.item
GROUP By e.eid ;
Result 1:
+---------+-------------+---------------+
| eid | ITEM | TOTAL SOLD |
+---------+-------------+---------------+
| 1 | rupa | 5 |
| 2 | pan america | 3 |
| 3 | John Player | 10 |
| 4 | classmate | 11 |
| 5 | lepakshi | 55 |
| 6 | lee | 14 |
| 7 | puma | 9 |
+---------+-------------+---------------+
Query 2:
SELECT
e.eid, e.item as 'ITEM', SUM(s.qty) as 'TOTAL STOCK'
from
tbl_purchases p,
tbl_matentry e
WHERE
e.eid = p.item
GROUP By e.eid ;
Result 2:
+---------+-------------+--------------------+
| eid | ITEM | TOTAL STOCK |
+---------+-------------+--------------------+
| 1 | rupa | 41 |
| 2 | pan america | 45 |
| 3 | John Player | 32 |
| 4 | classmate | 75 |
| 5 | lepakshi | 56 |
| 6 | lee | 65 |
| 7 | puma | 50 |
+---------+-------------+--------------------+
When I combine both sql statements into single statement I am getting incorrect output
So Far i have tried this
SELECT
e.eid,
e.item as 'ITEM',
SUM(p.qty) as 'TOTAL STOCK',
SUM(s.qty) as ' TOATL SOLD',
(SUM(p.qty)-SUM(s.qty)) as 'BALANCE STOCK'
FROM
tbl_matentry e
LEFT OUTER JOIN
tbl_purchases p ON p.item = e.eid
LEFT OUTER JOIN
tbl_sales s ON s.item=e.eid
GROUP BY e.eid ORDER BY e.eid
+-----+-------------+-------+------+---------+
| eid | ITEM | STOCK | SOLD | BALANCE |
+-----+-------------+-------+------+---------+
| 1 | rupa | 123 | 15 | 108 |
| 2 | pan america | 90 | 6 | 84 |
| 3 | John Player | 64 | 20 | 44 |
| 4 | classmate | 150 | 22 | 128 |
| 5 | lepakshi | 112 | 110 | 2 |
| 6 | lee | 130 | 28 | 102 |
| 7 | puma | 100 | 18 | 82 |
+-----+-------------+-------+------+---------+
How to do i get correct result? Where is mistake/error in my query.Please review my Query and help me to get correct data.
My Expected Output is
+---------+-------------+------------------+-----------------+------------------+
| eid | ITEM | TOTAL STOCK | TOTAL SOLD | BALANCE STOCK |
+---------+-------------+------------------+-----------------+------------------+
| 1 | rupa | 41 | 5 | 36 |
| 2 | pan america | 45 | 3 | 42 |
| 3 | John Player | 32 | 10 | 22 |
| 4 | classmate | 75 | 11 | 64 |
| 5 | lepakshi | 56 | 55 | 1 |
| 6 | lee | 65 | 14 | 51 |
| 7 | puma | 50 | 9 | 41 |
+---------+-------------+------------------+-----------------+------------------+