473,487 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Update query to copy data

I'm a bit of a newby to creating update queries so I was hoping those more
experienced could help with what should be a simple query. I do know to
create backups and test on a test database first.

I need to copy an items' cost from location 'CA' to the cost in location
'OH' when the cost in 'CA' is not zero
Table name is ITEMS
Location column is LOC
Cost column is COST

Here's the Select statement from Enterprise Manager's Open Table, Query tool

SELECT loc AS Expr1, cost AS Expr2
FROM ITEMS
WHERE (loc = 'CA') AND (cost <> 0)
I just can't seem to get this into a working Update query.

Thanks in advance...
Jul 20 '05 #1
4 19727
Assuming there is no more than one row where loc = 'CA' in the Items table:

UPDATE Items
SET cost =
(SELECT cost
FROM Items
WHERE loc = 'CA'
AND cost<>0)
WHERE loc = 'OH' AND EXISTS
(SELECT *
FROM Items
WHERE loc = 'CA'
AND cost<>0)

If the Costs come from more than one row then you'll have to explain which
rows you want to populate with what data. Maybe you could post DDL (CREATE
TABLE statement) for the table and gave us some example data to work with.

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
Yikes, I know even less than I thought. The table does have multiple rows
where loc = 'CA'
Here's some sample data:

RECORD ITEM LOC
COST
======= ==== ===
====
1 486 PAR CA
785
2 486 PAR OH
549
3 486 KIT OH
<NULL>
4 486 KIT CA
823
5 CASE OH
0
6 CASE CA
39
7 KIT-DOS CA
0
8 KIT-DOS OH
975
So for example, I want to copy/replace the COST of record 1 to the COST of
record 2, record 4 to record 3, record 6 to record 5, but not record 7 to
record 8 since the cost of 7 is zero.

Thanks again.
Assuming there is no more than one row where loc = 'CA' in the Items table:
UPDATE Items
SET cost =
(SELECT cost
FROM Items
WHERE loc = 'CA'
AND cost<>0)
WHERE loc = 'OH' AND EXISTS
(SELECT *
FROM Items
WHERE loc = 'CA'
AND cost<>0)

If the Costs come from more than one row then you'll have to explain which
rows you want to populate with what data. Maybe you could post DDL (CREATE
TABLE statement) for the table and gave us some example data to work with.

--
David Portas
------------
Please reply only to the newsgroup
--

Jul 20 '05 #3
Can I assume that the combination of (Item, Loc) is always unique, as in
your sample? If so, try this:

UPDATE Items
SET cost =
(SELECT cost
FROM Items AS I
WHERE loc = 'CA' AND item = Items.item
AND cost<>0)
WHERE loc = 'OH' AND EXISTS
(SELECT *
FROM Items
WHERE loc = 'CA' AND item = Items.item
AND cost<>0)

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #4
That did the trick !!
Thank you very much for your help.

Can I assume that the combination of (Item, Loc) is always unique, as in
your sample? If so, try this:

UPDATE Items
SET cost =
(SELECT cost
FROM Items AS I
WHERE loc = 'CA' AND item = Items.item
AND cost<>0)
WHERE loc = 'OH' AND EXISTS
(SELECT *
FROM Items
WHERE loc = 'CA' AND item = Items.item
AND cost<>0)

--
David Portas
------------
Please reply only to the newsgroup
--

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
89288
by: Lauren Quantrell | last post by:
In VBA, I constructed the following to update all records in tblmyTable with each records in tblmyTableTEMP having the same UniqueID: UPDATE tblMyTable RIGHT JOIN tblMyTableTEMP ON...
16
16972
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
2
5764
by: JMCN | last post by:
hi i need some advice on whether if it would be better to use an append query or an update query. here is the situation, i have linked another database table to my current database. then i...
9
4331
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
3
3859
by: dixie | last post by:
I have a form full of subforms which bring summary information onto the form from about 12 different tables. I am trying to get all of that summary information (mainly numbers) into 1 large table...
8
3690
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
4
1641
by: René Kabis | last post by:
People, I am at my wit's end. I am using the exact code from http://aspnet.4guysfromrolla.com/articles/071002-1.aspx And yet, the code does not manage to update the database. When I go to...
9
1688
by: DP | last post by:
hi., i've got 3 tables, customer, film and filmrental. i've got a customer form, with a sub form at the bottom, which is a film rental subform. i've created an update query, which when a...
4
5268
by: urprettyfriend | last post by:
Hi, I have an Excel file with 400 rows of old values and the corresponding new values. My table currently has 10 columns out of which 3 columns use the old value specified in the excel file. I...
16
3460
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7137
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.