Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
blocker
Yak Posting Veteran
89 Posts |
Posted - 2012-01-11 : 20:57:42
|
Good day to all!I just need a little help on how to join 3 tables and get the remaining total quantity of an item selected by the user base on the date filtered and base on item barcode. The process must be done using stored procedures.The following are the orders of tables:tbl_purchases(transdate,memo, itembarcode,itemname,qty)tbl_soldout(transdate,memo, itembarcode,itemname,qty)tbl_adjustment(transdate,itembarcode,itemname,qty,memo)-if memo is "added" is should add to the qty, else if "deducted", it should deduct to qty.example of table contents-tbl_purchasestransdate itembarcode itemname qty 11/02/2011 0012400132 choco milk 2012/05/2011 0245212213 choco milk 30tbl_soldouttransdate itembarcode itemname qty 11/03/2011 0012400132 choco milk 512/05/2011 0245212213 choco milk 4tbl_adjustmenttransdate itembarcode itemname qty memo 11/03/2011 0012400132 choco milk 2 "added"12/05/2011 0245212213 choco milk 1 "deducted" If the user will filter only until 11/30/2011, the result should be:transdate Memo itembarcode itemname stock in stockout remaining_qty 11/02/2011 purchase 0012400132 choco milk 20 2011/03/2011 sales 0012400132 choco milk 5 1511/03/2011 adjst 0012400132 choco milk 2 17If the user will filter until 12/05/2011, the result should be:transdate Memo itembarcode itemname stock in stockout remaining_qty 11/02/2011 purchase 0012400132 choco milk 20 2011/03/2011 sales 0012400132 choco milk 5 1511/03/2011 adjst 0012400132 choco milk 2 1712/05/2011 purchase 0012400132 choco milk 30 4712/05/2011 sales 0012400132 choco milk 4 4312/05/2011 adjst 0012400132 choco milk 1 42 Thank in advanced you for helping!TRANSOFTWARE |
|
Jeffreys
Starting Member
45 Posts |
Posted - 2012-01-11 : 22:19:51
|
I would do something like this - using the combo of unions, derived tables (inline views) and case statements. Joins aren't really appropriate for this kind of query. Of course you'll have to be careful of how you handle the temporal comparisons. disclaimer - I didn't debug or run - meant for example only.DECLARE @transdate datetime ,@barcode VARCHAR(??)SELECT transdate ,memo ,itembarcode ,itemname ,stockin ,stockout ,remaining_qty = ISNULL(stockin,0) - ISNULL(stockout,0)FROM ( SELECT transdate ,memo ,itembarcode ,itemname ,CASE WHEN memo in ('purchase','added') THEN qty ELSE null END AS stockin ,CASE WHEN memo in 'sales','deducted' THEN qty ELSE null END AS stockout ,transtype FROM ( SELECT transdate ,itembarcode ,itemname ,qty ,memo = 'purchase' ,transtype = 1 FROM tbl_Purchases WHERE itembarcode = @barcode AND transdate <= @transdate UNION ALL SELECT transdate ,itembarcode ,itemname ,qty ,memo = 'sales' ,transtype = 2 FROM tbl_Soldout WHERE itembarcode = @barcode AND transdate <= @transdate UNION ALL SELECT transdate ,itembarcode ,itemname ,qty ,memo ,transtype = 3 FROM tbl_adjustment WHERE itembarcode = @barcode AND transdate <= @transdate )dt1)dt2ORDER BY transtype |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-01-11 : 23:01:59
|
[code]declare @transdate date = '2011-11-30';with stk_card as( select row_no = row_number() over (partition by itembarcode order by transdate, stock_in desc, stock_out), transdate, memo, itembarcode, itemname, stock_in, stock_out from ( select transdate, memo = 'purchase', itembarcode, itemname, stock_in = qty, stock_out = NULL from tbl_purchases where transdate <= @transdate union all select transdate, memo = 'sales', itembarcode, itemname, stock_in = NULL, stock_out = qty from tbl_soldout where transdate <= @transdate union all select transdate, memo = 'adjst', itembarcode, itemname, stock_in = case when memo = 'added' then qty end, stock_out = case when memo = 'deducted' then qty end from tbl_adjustment where transdate <= @transdate ) t)select *from stk_card s cross apply ( select remaining_qty = sum(isnull(stock_in, 0) - isnull(stock_out, 0)) from stk_card x where x.itembarcode = s.itembarcode and x.row_no <= s.row_no ) r[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
Jeffreys
Starting Member
45 Posts |
Posted - 2012-01-12 : 06:04:34
|
Yes, you could use a CTE as well. That is a variation on the same theme, in fact nearly identical base query but the CTE makes is more complicated than it has to be. I prefer using derived tables over CTE's personally unless I'm in need of a true recursive solution. |
 |
|
blocker
Yak Posting Veteran
89 Posts |
Posted - 2012-01-17 : 10:27:17
|
quote: Originally posted by khtan
declare @transdate date = '2011-11-30';with stk_card as( select row_no = row_number() over (partition by itembarcode order by transdate, stock_in desc, stock_out), transdate, memo, itembarcode, itemname, stock_in, stock_out from ( select transdate, memo = 'purchase', itembarcode, itemname, stock_in = qty, stock_out = NULL from tbl_purchases where transdate <= @transdate union all select transdate, memo = 'sales', itembarcode, itemname, stock_in = NULL, stock_out = qty from tbl_soldout where transdate <= @transdate union all select transdate, memo = 'adjst', itembarcode, itemname, stock_in = case when memo = 'added' then qty end, stock_out = case when memo = 'deducted' then qty end from tbl_adjustment where transdate <= @transdate ) t)select *from stk_card s cross apply ( select remaining_qty = sum(isnull(stock_in, 0) - isnull(stock_out, 0)) from stk_card x where x.itembarcode = s.itembarcode and x.row_no <= s.row_no ) r KH[spoiler]Time is always against us[/spoiler]
Thank you sir khtan, sorry for late reply but your solution always makes it worked.. I have modified some fields of my tables and it works perfectly. Here is the modified stored procedures that includes date filtering:SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE [dbo].[item_quickrpt] -- Add the parameters for the stored procedure here @itemcode as varchar(max), @fromdate as date, @todate as dateASBEGIN;with stk_card as( select row_no = row_number() over (partition by prodcode order by podate, stock_in, stock_out), podate, memo, porefno,vendorname,prodcode, itemname, stock_in, stock_out from ( select podate, memo = 'purchased', porefno,vendorname= (select suppliername from tbl_supplier y where y.suppliercode=v.supplierid),prodcode, itemname, stock_in = qty, stock_out = 0 from PO_detail v where prodcode=@itemcode and (CONVERT(Date, podate) between @fromdate AND @todate) union all select transdate, memo = 'sales', salesid,customername=(select [Last Name] from tbl_costumers l where l.[ID No.]=(select costumerid from tbl_pos_sales_summary c where c.salesid=b.salesid)),productcode, productdesc, stock_in = 0, stock_out = qty from tbl_pos_sales_detail b where productcode=@itemcode and (CONVERT(Date, transdate) between @fromdate AND @todate) union all select adjustmentdatetime, memo = 'adjustment', [index],userlogin, itemcode, itemname, stock_in = case when memo = 'added' then adjustmenttotal else 0 end, stock_out = case when memo = 'deducted' then adjustmenttotal else 0 end from tbl_item_quantity_adjusments where itemcode=@itemcode and (CONVERT(Date, adjustmentdatetime) between @fromdate AND @todate) ) t)select *from stk_card s cross apply ( select remaining_qty = sum(isnull(stock_in, 0) - isnull(stock_out, 0)) from stk_card x where x.prodcode=@itemcode and x.row_no <= s.row_no ) rEND Code in red color are some of the changes. I humbly ask some clear explanation for me to fully understand on how it works..Thank you very much..TRANSOFTWARE |
 |
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-01-17 : 10:36:02
|
I take it there is NO chance of normalizing the database... http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspxHow to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
blocker
Yak Posting Veteran
89 Posts |
Posted - 2012-01-17 : 10:40:26
|
quote: Originally posted by Jeffreys I would do something like this - using the combo of unions, derived tables (inline views) and case statements. Joins aren't really appropriate for this kind of query. Of course you'll have to be careful of how you handle the temporal comparisons. disclaimer - I didn't debug or run - meant for example only.DECLARE @transdate datetime ,@barcode VARCHAR(??)SELECT transdate ,memo ,itembarcode ,itemname ,stockin ,stockout ,remaining_qty = ISNULL(stockin,0) - ISNULL(stockout,0)FROM ( SELECT transdate ,memo ,itembarcode ,itemname ,CASE WHEN memo in ('purchase','added') THEN qty ELSE null END AS stockin ,CASE WHEN memo in 'sales','deducted' THEN qty ELSE null END AS stockout ,transtype FROM ( SELECT transdate ,itembarcode ,itemname ,qty ,memo = 'purchase' ,transtype = 1 FROM tbl_Purchases WHERE itembarcode = @barcode AND transdate <= @transdate UNION ALL SELECT transdate ,itembarcode ,itemname ,qty ,memo = 'sales' ,transtype = 2 FROM tbl_Soldout WHERE itembarcode = @barcode AND transdate <= @transdate UNION ALL SELECT transdate ,itembarcode ,itemname ,qty ,memo ,transtype = 3 FROM tbl_adjustment WHERE itembarcode = @barcode AND transdate <= @transdate )dt1)dt2ORDER BY transtype
Sir jeffreys, thank you for the solution. I've tested it and it works but the remaining_qty seems not summing up the total. It would be more better sir jeff, if you can modify the solution above base on your query. Thank you..TRANSOFTWARE |
 |
|
|
|
|
|
|