I want to extend above example , now there is a new table that I
name it (purchase) and finally I need to calculate difference
of sum of purchase amount and sum of sale amount till every available
date in those 2 tables . Also I want to see all purchases and sales
in final result . This is purchase table content :
Code:
+----+---------------------+-------------+----------------+
| ID | date | productName | purchaseAmount |
+----+---------------------+-------------+----------------+
| 1 | 2010-04-03 16:42:56 | screw | 20 |
| 2 | 2010-04-03 16:43:43 | nut | 10 |
| 3 | 2010-04-03 16:44:44 | screw | 35 |
| 4 | 2010-04-04 16:45:53 | nut | 40 |
| 5 | 2010-04-07 16:46:29 | screw | 15 |
| 6 | 2010-04-08 16:47:52 | screw | 5 |
| 7 | 2010-04-08 16:50:37 | nut | 5 |
+----+---------------------+-------------+----------------+
I tried to write a query with below structure but I`m confused for
writing upper query :
Code:
SELECT f.date,
f.productName,
(HERE WHAT COULD I WRITE
TO CALCULATE ACCUMULATIVE
VALUE OF (SUM OF PURCHASES
- SUM OF SALES) TILL AVAILABLE
DATE IN EACH ROW) AS INVENTORY,
FROM(
SELECT date,
productName,
Null AS saleAmount,
purchaseAmount,
(SELECT sum(purchaseAmount)
FROM purchase AS q
WHERE q.productName = purchase.productName
AND q.ID <= purchase.ID) AS accumulativePurchase
FROM purchase
UNION ALL
SELECT date,
productName,
saleAmount,
Null AS purchaseAmount,
(SELECT SUM(saleAmount)
FROM sold AS q
WHERE q.productName = sold.productName
AND q.id <= sold.id) AS accumulativeSale
FROM sold
) AS f
Thanks in advance
Bookmarks