Monday, March 26, 2012

Query with a calculation

Hello Friends

I have 3 tables

1) Product
Id, ShortName

2) IncomingStock
Id, ProductId, Quantity, InDate

3) OutGoingStock
Id, OutDate, ProductId, Quantity

I need to get the results like this
Product name, quantity in stock today

the "quantity in stock today" = sum (quantity recieved) -sum (quantity sent)

Thank you for your time
Sara

Thanks to a dear friend of mine... i have got the query up and running

select p.ProductId,p.ShortName,SUM(t.qty) as QuantityOnStock
from Product p
left join
(select ProductId,sum(Quantity) as qty
from IncommingStock where InDate <=getdate()
group by ProductId
union all
select ProductId,sum(-Quantity) as qty
from OutGoingStock where OutDate<=getdate()
group by ProductId) t
on p.ProductId=t.ProductId
group by p.ProductId,p.ShortName

No comments:

Post a Comment