| Author |
Topic |
|
studyy
Starting Member
16 Posts |
Posted - 2010-05-12 : 07:59:01
|
| Hi Is it possible to show last record value to 2nd row ? currently i use table to store theses values but i want to make use of view so we get result from direct db without any fear of database corruption in inventory table.Current Stock Table Is :( Which i want to simulate with "view")Date MaterialName OpeningStock ClosingStock15-05-2010 Soap 20 15 16-05-2010 Soap 15 10Source Table /name : TranactionsPurchaseId Date MaterialName Qty202 15-05-2010 Saop 5Source Table /name : TranactionsSalesId Date MaterialName Qty 220 15-05-2010 Soap 10sorry if i m not able to explain the situation correctly but i m only trying to ask how to manage stock balance with views ?Thanks For Help |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-05-12 : 08:09:25
|
quote: Date MaterialName OpeningStock ClosingStock15-05-2010 Soap 20 1516-05-2010 Soap 15 10
Where & how is the date 16-05-2010 coming from ? It is not in the sample data that you provided. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
studyy
Starting Member
16 Posts |
Posted - 2010-05-12 : 13:36:26
|
| HiSorry i missed that line for table "TranactionsSales"Source Table /name : TranactionsSalesId Date MaterialName Qty220 15-05-2010 Soap 10221 16-05-2010 Soap 5Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-12 : 13:45:58
|
do you mean?SELECT s.Date,s.MaterialName,s.OpeningStock,s.OpeningStock - COALESCE(sl.SalQty,0) + COALESCE(p.PurQty,0) AS ClosingStockFROM Stock sOUTER APPLY (SELECT SUM(Qty) AS PurQty FROM TranactionsPurchase WHERE Date = s.Date AND MaterialName = s.MaterialName )pOUTER APPLY (SELECT SUM(Qty) AS SalQty FROM TranactionsSales WHERE Date = s.Date AND MaterialName = s.MaterialName )sl ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
studyy
Starting Member
16 Posts |
Posted - 2010-05-14 : 09:15:53
|
| Hi VisakhThe View I m Getting is thisDate,MaterialName,OpeningStock,ClosingStock13/May/10 12:00:00 AM Soap NULL NULL14/May/10 12:00:00 AM Soap NULL NULLTranactionsSales RecordsSalesID,Date,MaterialName,Qty1 13/May/10 12:00:00 AM Soap 22 14/May/10 12:00:00 AM Soap 6TranactionsPurchase1 13/May/10 12:00:00 AM Soap 302 14/May/10 12:00:00 AM Soap 10The Query is farmatted like this by sqlManagement :SELECT s.Date, s.MaterialName, s.OpeningStock, s.OpeningStock - COALESCE (sl.SalQty, 0) + COALESCE (p.PurQty, 0) AS ClosingStockFROM Stock s OUTER APPLY (SELECT SUM(Qty) AS PurQty FROM tranactionspurchase WHERE Date = s.Date AND MaterialName = s.MaterialName) p OUTER APPLY (SELECT SUM(Qty) AS SalQty FROM tranactionssales WHERE Date = s.Date AND MaterialName = s.MaterialName) slIs it possible to eliminate the stock table & get Balances using view only ?I tried it to mysql too but it dont support outer apply :(Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-14 : 13:16:43
|
| which view? if view has date and materialname you can use it instead of stock table------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
studyy
Starting Member
16 Posts |
Posted - 2010-05-15 : 01:12:30
|
| HiMaintain Stock detail in table is not good & data may be inaccurate.so what i want that to create a view which can simulate stock table from getting data from purchase & sales table & Calulate opening stock & closing stock on base of previous record. |
 |
|
|
studyy
Starting Member
16 Posts |
Posted - 2010-05-15 : 01:22:05
|
| HiI changed sql & added Salqty &Purqty to list.SELECT s.Date, s.MaterialName, s.OpeningStock, sl.SalQty,p.PurQty, s.OpeningStock - COALESCE (sl.SalQty, 0) + COALESCE (p.PurQty, 0) AS ClosingStockFROM Stock s OUTER APPLY (SELECT SUM(Qty) AS PurQty FROM tranactionspurchase WHERE Date = s.Date AND MaterialName = s.MaterialName) p OUTER APPLY (SELECT SUM(Qty) AS SalQty FROM tranactionssales WHERE Date = s.Date AND MaterialName = s.MaterialName) slNow Data which i get is thisDate, MaterialName,OpeningStock,SalQty,PurQty,ClosingStock2010-05-13 00:00:00.000 Soap NULL 2 30 NULL2010-05-14 00:00:00.000 Soap NULL 6 10 NULLI think its near about to result that i want.one thing need to be calculated is that opening stock & the closing stock.I want that for 1st record (because it is first then our opening stock starts from "0" & closing will show "28"then 2nd one show opening "28" & closing shows "32" & so on..Hope i mentioned what i need correctlyThanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-15 : 03:56:52
|
| [code]; WIth CTE (Date,MaterialName,SalQty,PurQty)AS(SELECT s.Date, s.MaterialName, sl.SalQty,p.PurQtyFROM Stock s OUTER APPLY(SELECT SUM(Qty) AS PurQtyFROM tranactionspurchaseWHERE Date = s.Date AND MaterialName = s.MaterialName) p OUTER APPLY(SELECT SUM(Qty) AS SalQtyFROM tranactionssalesWHERE Date = s.Date AND MaterialName = s.MaterialName) sl)SELECT c1.Date,c1.MaterialName,c1.SalQty,c1.PurQty,ISNULL(c2.TotalPrev,0) AS OpeningStock,ISNULL(c2.TotalPrev,0) + ISNULL(c1.PurQty,0)-ISNULL(c1.SalQty,0) AS ClosingStockFROM CTE c1OUTER APPLY(SELECT SUM(PurQty-SalQty) AS TotalPrev FROM CTE WHERE Date< c1.Date AND MaterialName = c1.MaterialName )c2[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
studyy
Starting Member
16 Posts |
Posted - 2010-05-15 : 12:41:29
|
| Hi visakhThanks Its working now as requiredThanks Thanks Thanks Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-16 : 07:50:26
|
welcome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|