troubleshooting Question

loop through items with sql query

Avatar of rmardis
rmardis asked on
Microsoft SQL Server 2008SQL
6 Comments1 Solution304 ViewsLast Modified:
i have the attached quey which returns the correct value for one item from our items database.  how can i change this query to work with all items in our database instead of having to run for one item at a time.

declare @Item nvarchar(15)
declare @DateF datetime
declare @DateT datetime

set @Item = '19168'
set @DateF = '01-01-2009'
set @DateT = getdate()

--READITEM 19168
select t_item as Item,t_stoc --from ttiitm001100 --where t_item = @Item -
--select 4873 -
-

(select sum(t_quan) from ttdinv700100   --Production Issue Always (+)
where t_item = @Item and t_kost = 8 and t_trdt >= @DateF and t_trdt <= @DateT)
--(62729)
+

(select sum(t_quan) from ttdinv700100   --Sales Order Delivery (-)
where t_item = @Item and t_kost = 6 and t_trdt >= @DateF and t_trdt <= @DateT)
--(61513)
+

(select sum(t_quan) from ttdinv700100  --Production Issue (-)
where t_item = @Item and t_kost = 7 and t_trdt >= @DateF and t_trdt <= @DateT)
--(300)
--+ 306
+

(select replace(sum(t_quan),'-','') from ttdinv700100  --Inventory Adjustment Could be either (+/-)
where t_item = @Item and t_kost = 1 and t_trdt >= @DateF and t_trdt <= @DateT and t_quan < 0)
-- (-306)
-

(select sum(t_quan) from ttdinv700100   --Inventory Adjustment Could be either (+/-)
where t_item = @Item and t_kost = 1 and t_trdt >= @DateF and t_trdt <= @DateT and t_quan > 0)
--(1997)

as [Inventory]from ttiitm001100 where t_item = @Item

--Number Should be 2266
ASKER CERTIFIED SOLUTION
Reza Rad
Consultant, Trainer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros