Click here to Skip to main content
15,888,195 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to create an opening-closing qty day-wise here is my table struct

table name Item

ItemID| ItemName| Qty| EnteryDate
1 | bag | 10 | 07-07-2021

Table Name Purchase

ItemID | ItemName | PurchaseQTY | PurchaseDate
1 | bag | 10 | 07-07-2021

Table Name Issue

ItemID | ItemName | IssueQTY | IssueDate
1 | bag | 5 | 07-07-2021


I want to show day-wise Opening closing

ItemID | ItemName | Opening | PurchaseQTY | IssueQTY | closing | IssueDate
1 | bag | 10 | 10 | 5 | 15 | 07-07-2021 |

What I have tried:

select t.ItemName, (Coalesce(Sum(t.QTY)) + IFNULL(sum(t.ReceivedQTY) - sum(t.QTYIssue), 0)) as Opening, 0 as Reciving, 0 as Issue, 0 as Closing, t.EntryDate as StockDate from Stock_view t where t.EntryDate ='2021-06-26' group by t.ItemName, t.EntryDate
Union
select t.ItemName, 0 as Opening, IFNULL(sum(t.ReceivedQTY),0) as Reciving , 0 as Issue, 0 as Closing, t.PUBDate as StockDate from Stock_view t where t.PUBDate = '2021-06-26' group by t.ItemName , t.PUBDate
Union
select t.ItemName, 0 as Opening, 0 as Reciving, IFNULL(sum(t.QTYIssue),0) as Issue, (Coalesce(Sum(t.QTY)) + IFNULL(sum(t.ReceivedQTY) - sum(t.QTYIssue), 0)) as Closing, t.IssueDate as StockDate from Stock_view t where t.IssueDate = '2021-06-26' group by t.ItemName , t.IssueDate
Posted
Updated 7-Jul-21 4:27am

1 solution

Problem number 1: You have an item table but keep repeating the name of the item in both your Purchase and Issue tables. You need to understand Database Normalization[^]

My version of these tables (if I was going to use them - see below) would look more like this (caveat - I'm using table variables in SQL Server because it is all I have to hand at the moment. You may need to tweak the syntax for MySql)
SQL
declare @item table (ItemID int, ItemName varchar(30), Qty int, EntryDate date);
declare @Purchase table (ItemID int, PurchaseQTY int , PurchaseDate date);
declare @Issue table(ItemID int, IssueQTY int, IssueDate date);
Problem number 2: You need to provide more sample data to make this meaningful - the opening stock will be the previous opening stock plus any purchases for the day minus any issues for the day. You are completely ignoring the previous closing stock and it will be easier to spot things like that if you use slightly more data e.g.
SQL
insert into @item (ItemID,ItemName,Qty,EntryDate) values
(1,'bag',10,'2021-07-07'),(2,'box',10,'2021-07-06');

insert into @Purchase(ItemID , ItemName , PurchaseQTY , PurchaseDate) values
(2, 'box' , 10 , '2021-07-06'),(1, 'bag' , 10 , '2021-07-07'),
(2, 'box' , 11 , '2021-07-07'),(1, 'bag' , 20 , '2021-07-07');

insert into @Issue(ItemID , ItemName , IssueQTY , IssueDate) values
(2, 'box' , 3 , '2021-07-06'),(1, 'bag' , 1 , '2021-07-07'),
(2, 'box' , 4 , '2021-07-07'),(1, 'bag' , 2 , '2021-07-07');
Problem number 3: Your "what I have tried" code uses something called Stock_view - but you have not told us what that is (and I am not going to try to guess) which makes me suspect you haven't run it

Problem number 4 You haven't told us what the actual problem is. You should check the posting guidelines for this forum.

Suggestion: Change your schema to simplify it: One table for the items - you would not normally store a Qty on that table either, and one table for the Transactions - both types on the same table. Either use positive quantities for purchases and negative for issues or have a column that indicates whether the stock movement was In or Out E.g.
SQL
declare @item table (ItemID int, ItemName varchar(30), EntryDate date);
declare @transaction table (ItemID int, QTY int , TranDate date, Direction char(1)); -- I = In, O = Out

insert into @item (ItemID,ItemName,Qty,EntryDate) values
(1,'bag','2021-07-07'), (2,'box','2021-07-06');

insert into @transaction(ItemID , QTY , TranDate, Direction) values
(2, 'box' , 10 , '2021-07-06','I'),(1, 'bag' , 10 , '2021-07-07','I'),
(2, 'box' , 11 , '2021-07-07','I'),(1, 'bag' , 20 , '2021-07-07','I'),
(2, 'box' , 3 , '2021-07-06','O'),(1, 'bag' , 1 , '2021-07-07','O'),
(2, 'box' , 4 , '2021-07-07','O'),(1, 'bag' , 2 , '2021-07-07','O');
That way you can calculate the Net stock movements for any single day E.G.
SQL
select a.itemid, b.ItemName, TranDate, SUM(case when a.Direction = 'I' then a.QTY ELSE (-1) * a.QTY end) as netDaily
from @transaction a
inner join @item b on a.ItemID = b.ItemID 
GROUP BY a.itemid, b.ItemName, Trandate
order by TranDate, a.ItemID
As this is homework I will leave the exercise of working out how to include the previous closing stock (which is the opening stock for the next day) in the calculation. You might want to consider keeping a Stock Table just to record the end of day position - but remember you will need to schedule the update to that daily.

Or you could do that as a first step each time - e.g. using the code above and inserting it into a temporary table.

Or you could calculate it "on the fly" each time - in which case have a look at MySQL LAG() Function Explained By Practical Examples[^]
 
Share this answer
 
Comments
Asif 7969814 8-Jul-21 5:02am    
Sir Stock_view is a that i made by combining three table item, purchase_details, IssueDetails using this queriy
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`@`localhost`
SQL SECURITY DEFINER
VIEW `stock_view` AS
SELECT
`pubd`.`ItemName` AS `ItemName`,
IFNULL(`i`.`ItemValue`, 0) AS `ItemValue`,
`i`.`QTY` AS `QTY`,
`i`.`EntryDate` AS `EntryDate`,
`pubd`.`ReceivedQTY` AS `ReceivedQTY`,
IFNULL(`pubd`.`Total`, 0) AS `PValue`,
IFNULL(`issue`.`QTYIssue`, 0) AS `QTYIssue`,
IFNULL(`issue`.`ItemValue`, 0) AS `IssueValue`,
`p`.`PUBDate` AS `PUBDate`,
`iss`.`IssueDate` AS `IssueDate`,
IFNULL(`iss`.`IssueDate`, `p`.`PUBDate`) AS `StockDATE`,
`pubd`.`ItemID` AS `ItemID`,
`i`.`ComId` AS `ComId`
FROM
((((`purchasebilldetails` `pubd`
LEFT JOIN `issuestockdetails` `issue` ON ((`issue`.`ItemID` = `pubd`.`ItemID`)))
LEFT JOIN `purchasebill` `p` ON ((`p`.`ID` = `pubd`.`PuOrderID`)))
LEFT JOIN `issuestock` `iss` ON ((`iss`.`ID` = `issue`.`IssueStockID`)))
LEFT JOIN `item` `i` ON ((`i`.`Item_Id` = `pubd`.`ItemID`)))
WHERE
(`pubd`.`ComID` = `i`.`ComId`)

and by the Issue Stock View I have solved my problem calling this Stock View by this query

SELECT
t1.StockDATE,
mt.Item_Name,
(SELECT QTY FROM Item WHERE Item_Id = t1.ItemID) +
COALESCE((SELECT SUM(t2.ReceivedQTY - COALESCE(t2.QTYIssue)) FROM stock_view t2
WHERE t2.StockDATE < t1.StockDATE AND t1.ItemID = t2.ItemID), 0) AS OpeningStockQTY,
t1.ReceivedQTY,
t1.QTYIssue,
(SELECT QTY FROM Item WHERE Item_Id = t1.ItemID) +
COALESCE((SELECT SUM(t2.ReceivedQTY - COALESCE(t2.QTYIssue)) FROM stock_view t2
WHERE t2.StockDATE <= t1.StockDATE AND t1.ItemID = t2.ItemID), 0) AS ClosingStockQTY,
(SELECT ifnull(ItemValue,0) FROM Item WHERE Item_Id = t1.ItemID) +
COALESCE((SELECT SUM(t2.PValue - COALESCE(t2.IssueValue)) FROM stock_view t2
WHERE t2.StockDATE < t1.StockDATE AND t1.ItemID = t2.ItemID), 0) AS OpeningStockValue,
t1.PValue,
t1.IssueValue,
(SELECT ifnull(ItemValue,0) FROM Item WHERE Item_Id = t1.ItemID) +
COALESCE((SELECT SUM(t2.PValue - COALESCE(t2.IssueValue)) FROM stock_view t2
WHERE t2.StockDATE <= t1.StockDATE AND t1.ItemID = t2.ItemID), 0) AS ClosingStockValue
FROM stock_view t1
INNER JOIN Item mt
ON t1.ItemID = mt.Item_Id where t1.StockDATE between '2021-06-28' and '2021-07-07' and t1.ComId=8
ORDER BY
t1.StockDATE,
mt.Item_Id;

Please Guide me if you have a more easy way for doing this.
CHill60 8-Jul-21 5:10am    
Sorry, you have now introduced 4 more tables without definitions or sample data so I can't help you.
I will say that you have far too many unnecessary sub-queries though. You should look to get that data through the joins
Asif 7969814 8-Jul-21 5:34am    
Sir I have a master details table structure `purchasebill` `p` , `issuestock` `iss` is master and purchasebilldetails` `pubd`, `issuestockdetails` `issue` are details data tables. I am vary thanks full for your help till now . i have solved my problem Thanks A lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900