Link to home
Start Free TrialLog in
Avatar of DeathbySQL
DeathbySQL

asked on

update with a select

Hello all,
I need to update the cost of the child item (ID 75) with the cost from the parrent Item divided by the parent quantity. in this case it would be 24/6 . this is all in the same table (item).

select ID, ParentItem, ParentQuantity, cost from ITEM
results:
ID      ParentItem   ParentQuantity   cost
75,           357,                6,                 2
357,           0,                  0,                 24

so after the querry the child item would cost $4

good luck and thanks in advance.
Avatar of lcohan
lcohan
Flag of Canada image

You would need to do something like:

update ITEM set cost = ITEM.ParentQuantity / PARENT.cost
from PARENT
where PARENT.id = ITEM.ParentItem
sorry...other way around for division:

update ITEM set cost = PARENT.cost / ITEM.ParentQuantity
from PARENT
where PARENT.id = ITEM.ParentItem
Avatar of DeathbySQL
DeathbySQL

ASKER

Ico,

There is no PARENT Table.

all the records are in the ITEM table.

thanks,
I was thinking something like
update item set item.cost = (SELECT Item.cost/Item.ParentQuantity
FROM Item
WHERE Item.id = Item.Parentitem)

but I want item.id = item.parentitem to search all the records for a match..there will only be one that matchs.
try

update table1
set cost  = (select B.Cost / parentQuantity
             from table1 B
             where BARCODE.ID=Table1.ParentItem)
where ID= 75
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America 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
Simple like below:


update ITEM with (rowlock) set cost = i.cost / ITEM.ParentQuantity
from ITEM i  with (nolock)
where i.id = ITEM.ParentItem
Oh - not sure how big that table is and if there are any triggers on UPDATE so either way I suggest you batch your update.
@ Icohan

its not a very large table and there is no trigers on update, not sure about batching.

error recieved on you last attempt
The multi-part identifier "ITEM.ParentItem" could not be bound.

thanks
Little Mod needed but got me close enough..awesome

update ITEM
set cost  = (select B.Cost / Item.parentQuantity
             from ITEM B
             where B.ID=ITEM.ParentItem)

where Item.ParentItem<>''