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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 joining 3 tables together and get total quantity..

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_purchases

transdate itembarcode itemname qty
11/02/2011 0012400132 choco milk 20
12/05/2011 0245212213 choco milk 30

tbl_soldout

transdate itembarcode itemname qty
11/03/2011 0012400132 choco milk 5
12/05/2011 0245212213 choco milk 4

tbl_adjustment

transdate 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 20
11/03/2011 sales 0012400132 choco milk 5 15
11/03/2011 adjst 0012400132 choco milk 2 17

If 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 20
11/03/2011 sales 0012400132 choco milk 5 15
11/03/2011 adjst 0012400132 choco milk 2 17
12/05/2011 purchase 0012400132 choco milk 30 47
12/05/2011 sales 0012400132 choco milk 4 43
12/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
)dt2
ORDER BY transtype
Go to Top of Page

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]

Go to Top of Page

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.
Go to Top of Page

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 ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[item_quickrpt]
-- Add the parameters for the stored procedure here
@itemcode as varchar(max),
@fromdate as date,
@todate as date
AS
BEGIN

;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
) r
END



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
Go to Top of Page

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.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

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
)dt2
ORDER 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
Go to Top of Page
   

- Advertisement -