Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Table with adds and subtracts, trying to get the balance

$
0
0

I have a table that lists adds and subtracts from an inventory item

I am trying to figure out how to get the balance from all the rows.

i.e.

row 1 Add 20 (balance is now 20)

row 2 Subtract 10 (balance is now 10)

row 3 Add 5 (balance is now 15)

So I have 3 rows that don't have a running balance so I would need to do something like Sum all the add's and sum all the subtracts and then do a total number of adds - total number of subtracts.

This is the data I currently have AdjustmentTypeID 1 is add and AdjustmentTypeID 2 is subtract, VolumeOrNumber is the amount added or subtracted

AdjustmentTypeID  VolumeOrNumber                         
 ----------------  ---------------------------------------
 1                 20.00                                  
 2                 10.00     

Here is my query w/o any SUMS etc.  as I have not done any aggregate functions so I am not sure how to do this:

SELECT	
	CASE  
		WHEN VolumeOrNumber IS NULL THEN '0' 
		ELSE CAST(VolumeOrNumber AS VARCHAR(10)) 
	END AS InventoryTotal
FROM Item i					
	LEFT JOIN Inventory v ON i.ItemID = v.ItemID	
ORDER BY i.ItemName
Any help is greatly appreciated


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>