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 2000 Forums
 Transact-SQL (2000)
 Calculating sums

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-09-03 : 20:40:15
Gregor J. Rothfuss writes "i have a table with the following fields:

stock
-----
id
product_id
location_id
stocktype_id
quantity

i want to calculate the quantity of stock for each product at each location. stock type indicates if it is a stock addition or a stock reduction.

the formula would look somewhat like this:

quantity on stock for a product =
sum(stock entries of this product with type = addition)
- sum(stock entries of this product with type reduction)

i want to display the results like this:


product|location|#

a loc1 23
a loc2 14
b loc1 9

i tried some approaches with grouping and subselects, but that led me nowhere.

i think i could hack something together with vb, but i'd like this to scale, so i'd prefer a t-sql solution."
   

- Advertisement -