Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

update products.inwarehouse1 orderitems by the sum total of orderitems.is1q+orderitems.is2q+orderitems.is3q (int)

select * from orderitems i
left join products p on p.productid=i.productid
where i.itemid>400000 order by i.itemid desc


I want to update products.ourinwarehouse1  (int)
by the sum total of
orderitems.is1q+orderitems.is2q+orderitems.is3q   (int)


but what if orderitems.is3q is null
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

this will do:
update p
  set ourinwarehouse1  = ( select sum(is1q + is2q + is3q) from orderitems oi where oi.productid = p.productid and oi.itemid > 400000 )
  from products p

Open in new window

Avatar of rgb192

ASKER

and if any of those values are null

do i need to do
isnull(is1q,0)
Avatar of mquiroz
mquiroz

something like:

update products.inwarehouse1 set qtycolumn = isnull(t2.qty, 0) + isnull(t3.qty, 0)
from products.inwarehouse1 t1
left outer join products.inwarehouse2 t2  on  t1.id = t2.id
left outer join products.inwarehouse3 t3  on  t2.id = t3.id
Avatar of rgb192

ASKER

mquiroz
I dont understand


angelIII

I only want to update one oi.itemid to test
i tried

update p
  set ourinwarehouse1  = ( select sum(oi.is1q + oi.is1n+p.ourinwarehouse1) from orderitems oi where oi.productid = p.productid and oi.itemid = 406438 )
  from products p

Msg 8124, Level 16, State 1, Line 1
Multiple columns are specified in an aggregated expression containing an outer reference. If an expression being aggregated contains an outer reference, then that outer reference must be the only column referenced in the expression.
you can use a join to update the table
ASKER CERTIFIED SOLUTION
Avatar of jagssidurala
jagssidurala
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

thanks