[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

update with a select

Posted on 2011-03-24
10
Medium Priority
?
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 

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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 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