I tried by making one test query to get related output Check the below test query for your reference
DECLARE @Inv_Medicine AS TABLE(itemid INT,ItemName VARCHAR(50),EntryDate DATETIME,PurchaseRate DECIMAL(18,2),quantity INT)
DECLARE @Inv_Sales AS TABLE (itemid INT,SaleDate DATETIME,TotalPrice DECIMAL(18,2),Quantity INT)
INSERT INTO @Inv_Medicine VALUES(22,'paracetamol','2017-01-19 23:27:58.757',120,500)
INSERT INTO @Inv_Medicine VALUES(23,'koflet syrup','2017-01-24 02:57:28.163',230,50)
INSERT INTO @Inv_Medicine VALUES(24,'Clotrimazole','2017-01-24 03:01:37.378',360,5)
INSERT INTO @Inv_Medicine VALUES(25,'Ezy Tab','2017-01-24 03:03:24.430',4555,10)
INSERT INTO @Inv_Medicine VALUES(26,'MontekLc','2017-01-24 03:05:16.163',960,50)
INSERT INTO @Inv_Sales VALUES(22,'2017-01-25 03:49:12.330',70.00,5)
INSERT INTO @Inv_Sales VALUES(22,'2017-01-25 03:53:27.017',840.00,6)
SELECT i.itemid
,i.ItemName
,i.EntryDate AS 'Purchase Date'
,i.PurchaseRate AS 'Purchase Amount'
,s.SaleDate
,ISNULL(s.TotalPrice,0) AS 'SalesAmount'
,(
(SELECT ISNULL(SUM(IM.Quantity),0)
FROM @Inv_Medicine im
WHERE IM.itemid = I.itemid)
-
(SELECT ISNULL(SUM(Sm.Quantity),0)
FROM @Inv_Sales SM
WHERE SM.SaleDate < S.SaleDate)
)'Input Stock'
,s.Quantity
,(
(SELECT ISNULL(SUM(IM.Quantity),0)
FROM @Inv_Medicine im
WHERE IM.itemid = I.itemid)
-
(SELECT ISNULL(SUM(Sm.Quantity),0)
FROM @Inv_Sales SM
WHERE SM.SaleDate <= S.SaleDate)
) as 'Remaining Stock'
FROM @Inv_Medicine i LEFT JOIN @Inv_Sales s ON s.itemid=i.itemid
OutPut
itemid |
ItemName |
Purchase Date |
Purchase Amount |
SaleDate |
SalesAmount |
Input Stock |
Quantity |
Remaining Stock |
22 |
paracetamol |
2017-01-19 23:27:58.757 |
120 |
2017-01-25 03:49:12.330 |
70 |
500 |
5 |
495 |
22 |
paracetamol |
2017-01-19 23:27:58.757 |
120 |
2017-01-25 03:53:27.017 |
840 |
495 |
6 |
489 |
23 |
koflet syrup |
2017-01-24 02:57:28.163 |
230 |
NULL |
0 |
50 |
NULL |
50 |
24 |
Clotrimazole |
2017-01-24 03:01:37.377 |
360 |
NULL |
0 |
5 |
NULL |
5 |
25 |
Ezy Tab |
2017-01-24 03:03:24.430 |
4555 |
NULL |
0 |
10 |
NULL |
10 |
26 |
MontekLc |
2017-01-24 03:05:16.163 |
960 |
NULL |
0 |
50 |
NULL |
50 |