Solved

update with a select

Posted on 2011-03-24
10
325 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 40

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 40

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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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:Ephraim Wangoya
ID: 35210204
try

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

Accepted Solution

by:
Ephraim Wangoya 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 40

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 40

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is this datetime? 1 25
Substring works but need to tweak it 14 31
Star schema daily updates 2 33
MySQL-Design Help 12 38
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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…

680 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