473,772 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multi row update using sub query

1 New Member
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 3733

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

Similar topics

5
2362
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 (table1.id = table2.id) LEFT JOIN table3 on (table1.id2 = table3.id2) LEFT JOIN table4 on (table1.id3 = table4.id3)
2
8312
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 from another table... Easy as pie in .mdb. Of course In the stored procedure it barks at me and tells me that an update query can only have one table.. ouch that hurts... I'm currently reading on the subject but this group has been very
1
1704
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
1660
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 on all key forms. The first thing to do is to successfully split the database. I then rewrite to support multiple users. To allow the save/discard feature I create copies of the key tables and append the word Final to each one. For example I...
5
5767
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 know a good place to start looking for some theory on the subject of multi user applications? I know only bits and pieces, like about transactions, but a compendium of possible approches to multi user programming would be very appreciated!
1
2564
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 following procedure we have a select query query which returns only one row, and after some processing I update the same row.
4
1629
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 can I run into problems and should I do it another way? Thanks, john
12
2182
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 once pointed in the best direction should be able to follow my nose and get things sorted... but I am not quite sure which is the best path to take and would be grateful for advice from networking gurus. I am writing a program to display horse...
9
2603
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 shared between 4 staff memebers. Each staff computer has a copy of the front-end (linked tables, forms, and queries). They basically only use one form. The form works like this
0
9619
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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 we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.