Solved

update with a select

Posted on 2011-03-24
10
319 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:DeathbySQL
  • 4
  • 4
  • 2
10 Comments
 
LVL 39

Expert Comment

by:lcohan
ID: 35210135
You would need to do something like:

update ITEM set cost = ITEM.ParentQuantity / PARENT.cost
from PARENT
where PARENT.id = ITEM.ParentItem
0
 
LVL 39

Expert Comment

by:lcohan
ID: 35210144
sorry...other way around for division:

update ITEM set cost = PARENT.cost / ITEM.ParentQuantity
from PARENT
where PARENT.id = ITEM.ParentItem
0
 

Author Comment

by:DeathbySQL
ID: 35210170
Ico,

There is no PARENT Table.

all the records are in the ITEM table.

thanks,
0
 

Author Comment

by:DeathbySQL
ID: 35210197
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.
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35210204
try

update table1
set cost  = (select B.Cost / parentQuantity
             from table1 B
             where BARCODE.ID=Table1.ParentItem)
where ID= 75
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 35210212
sorry

try

update table1
set cost  = (select B.Cost / parentQuantity
             from table1 B
             where B.ID=Table1.ParentItem)
where ID= 75
0
 
LVL 39

Expert Comment

by:lcohan
ID: 35210252
Simple like below:


update ITEM with (rowlock) set cost = i.cost / ITEM.ParentQuantity
from ITEM i  with (nolock)
where i.id = ITEM.ParentItem
0
 
LVL 39

Expert Comment

by:lcohan
ID: 35210260
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.
0
 

Author Comment

by:DeathbySQL
ID: 35210316
@ 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
0
 

Author Closing Comment

by:DeathbySQL
ID: 35210379
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<>''
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now