473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

multi row update using sub query

Hi all,

I need to update multiple rows using a sub query. I've pasted sample code below to hopefully help recreate what I'm trying to do.

drop table dummy_test

create table dummy_test
(cmr number
,sub number
,rev number
,cost number)
/
insert into dummy_Test values (1,2,0, 0)
/
insert into dummy_Test values (1,3,0, 0)
/
select *
from dummy_test
/
create table dollar_amounts
(cmr number
,sub number
,rev number
,rev_direction varchar2(1))
/
insert into dollar_amounts values (1, 2, 48, 'I')
/
insert into dollar_amounts values (1, 2, .63, 'I')
/
insert into dollar_amounts values (1, 2, 17, 'O')
/
insert into dollar_amounts values (1, 2, .17, 'O')
/
insert into dollar_amounts values (1, 3, 19, 'I')
/
insert into dollar_amounts values (1, 3, .83, 'I')
/
insert into dollar_amounts values (1, 3, 10, 'O')
/
insert into dollar_amounts values (1, 3, .93, 'O')
/
select *
from dollar_amounts
/

I want to update dummy_test with the sum of the revenue and costs from the dollar_amounts table.

When I do this I can but the row return more than one 1 in this statement.

update dummy_test a
set (rev, cost) =
(select (case when rev_direction = 'I' then sum(rev) end)
,(case when rev_direction = 'O' then sum(rev) end)
from dollar_amounts b
where b.cmr = a.cmr
and b.sub = a.sub
group by reV_direction)

-------------

and when I try this it doesn't recognise the table I want to update, in this instance table a.

update dummy_test a
set (rev, cost) = (
select sum(rev), sum(cost)
from (
(select (case when rev_direction = 'I' then sum(rev) end) rev
,(case when rev_direction = 'O' then sum(rev) end) cost
from dollar_amounts b
where b.cmr = a.cmr
and b.sub = a.sub
group by rev_direction)))

this is what I'm trying to achieve when updating the table:

select cmr, sub, sum(rev), sum(cost)
from (
select cmr, sub
,(case when rev_direction = 'I' then sum(rev) end) rev
,(case when rev_direction = 'O' then sum(rev) end) cost
from dollar_amounts
group by rev_direction, cmr, sub)
group by cmr, sub

trying to find out the best approach for this. I'll be updating roughly 2 million rows in this update.

thanks in advance.
Jun 26 '07 #1
0 3704

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Marek Kotowski | last post by:
In MySQL online documentation there are some examples with multi-tables left joins. But all of them are like this (taken from the documentation): SELECT ... FROM table1 LEFT JOIN table2 on...
2
by: meyvn77 | last post by:
I'm new to adp w/ sql server but I have to use it on a project i'm doing... One of the MUSTS for this project is the ability to update a 00 - 09 text value with the appropriate text description...
1
by: cong ngo | last post by:
Hi all If have table with the records below and these data need to be update to another table by the name field. name phone fax emal ted 619 mary 855
15
by: Deano | last post by:
I've posted about this subject before but haven't really got anywhere yet. I have now come up with a plan of action that takes into account my strong desire to implement save/discard functionality...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
1
by: bjethwan | last post by:
Hi, I have a small doubt in SQL Procedures in DB2. It is the first time that I am using DB2 UDB, so please bear me. Following, is a very small example to put across my doubt. In...
4
by: John | last post by:
I have an update table which I automatically would like to add to the master table which is in use by other users. My idea is to add the records via an update query. Is this a reliable method or...
12
by: bullockbefriending bard | last post by:
I am a complete ignoramus and newbie when it comes to designing and coding networked clients (or servers for that matter). I have a copy of Goerzen (Foundations of Python Network Programming) and...
9
by: zmickle | last post by:
Experts and books all say that you can share an Access back end on a shared drive with the front end running on each host computer. I have a simple database that tracks student data and it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.