473,394 Members | 1,810 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,394 software developers and data experts.

multi row update in one SQL statment

I have the following two tables :

table a
(commit_id,
capital_market_id,
chg_lst_date
)

table b
(b_seq_id,
commit_id,
capital_market_id,
chg_lst_date
)

commid_id is PK in A but not in B. B can have multiple entries per
commit_id.

I want to update all entries in table A - set the capital_market_id
and chg_lst_date - from the corresponding commit_id entry in table B
with the following two rules :

1. since B can have multiple rows per commit_id I want to pick the one
with the latest txn_time and
2. only update in A if the chg_lst_date of a is less than that of B.

Can I do this in one update statement? or do I have to do a
cursor/loop ?

I originally posted this in oracle.misc but then realized it might be
a non-technical forum - hence this repeat post.
Jul 19 '05 #1
2 31103

"Rima" <pa********@yahoo.com> wrote in message
news:8a**************************@posting.google.c om...
I have the following two tables :

table a
(commit_id,
capital_market_id,
chg_lst_date
)

table b
(b_seq_id,
commit_id,
capital_market_id,
chg_lst_date
)

commid_id is PK in A but not in B. B can have multiple entries per
commit_id.

I want to update all entries in table A - set the capital_market_id
and chg_lst_date - from the corresponding commit_id entry in table B
with the following two rules :

1. since B can have multiple rows per commit_id I want to pick the one
with the latest txn_time and
2. only update in A if the chg_lst_date of a is less than that of B.

Can I do this in one update statement? or do I have to do a
cursor/loop ?

I originally posted this in oracle.misc but then realized it might be
a non-technical forum - hence this repeat post.


update A a
set (a.commit_id, a.capital_market_id, a.chg_lst_date) = (
select b.commit_id, b.capital_market_id, b.chg_lst_date
from B b
where b.chg_lst_date = ( select max(b1.chg_lst_date)
from B b1
where b1.commit_id =
b.commit_id
)
)
where a.chg_lst_date < (select max(b2.chg_lst_date)
from B b2
where b2.commit_id = a.commit_id
)



Jul 19 '05 #2
Nic

"Rima" <pa********@yahoo.com> wrote in message
news:8a**************************@posting.google.c om...
"Nicolas Payre" <nicpayre[junk]@sympatico.ca> wrote in message news:<TT********************@news20.bellglobal.com >...
"Rima" <pa********@yahoo.com> wrote in message
news:8a**************************@posting.google.c om...
I have the following two tables :

table a
(commit_id,
capital_market_id,
chg_lst_date
)

table b
(b_seq_id,
commit_id,
capital_market_id,
chg_lst_date
)

commid_id is PK in A but not in B. B can have multiple entries per
commit_id.

I want to update all entries in table A - set the capital_market_id
and chg_lst_date - from the corresponding commit_id entry in table B
with the following two rules :

1. since B can have multiple rows per commit_id I want to pick the one
with the latest txn_time and
2. only update in A if the chg_lst_date of a is less than that of B.

Can I do this in one update statement? or do I have to do a
cursor/loop ?

I originally posted this in oracle.misc but then realized it might be
a non-technical forum - hence this repeat post.


update A a
set (a.commit_id, a.capital_market_id, a.chg_lst_date) = (
select b.commit_id, b.capital_market_id, b.chg_lst_date
from B b
where b.chg_lst_date = ( select max(b1.chg_lst_date)
from B b1
where b1.commit_id = b.commit_id
)
)
where a.chg_lst_date < (select max(b2.chg_lst_date)
from B b2
where b2.commit_id = a.commit_id
)


Thanks Nicolas.

Sorry, ithink a made a mistake the query in my initial post should have
been:

==>> See, i added a very important clause ;-)

update A a
set (a.commit_id, a.capital_market_id, a.chg_lst_date) = (
select b.commit_id, b.capital_market_id, b.chg_lst_date
from B b
where b.chg_lst_date = ( select max(b1.chg_lst_date)
from B b1
where b1.commit_id
=b.commit_id
)
==>> and b.commit_id = a.commit_id
)
where a.chg_lst_date < (select max(b2.chg_lst_date)
from B b2
where b2.commit_id = a.commit_id
)

I'm a little confused however, according to the docs in the case of

update ... set (col,col,col...) = (subquery)

the subquery must return exactly one row.

your subquery :

select b.commit_id, b.capital_market_id, b.chg_lst_date
from B b
where b.chg_lst_date = ( select max(b1.chg_lst_date)
from B b1
where b1.commit_id = b.commit_id
)
would return multiple rows. How does that work?
it depend on your data!

For exemple, if for a same commit_id you can have multiple row with the
exact same chg_lst_date, then the subquery will return multiple rows.... You
have to know what the data is, or will be. If your not sure, it better to go
with some PL/SQL where you can manage differents case with somme IF/ELSE or
even EXCEPTIONS logic.

The use of constraints will help you ensure what your data is.
Thanks,
Rima.

Jul 19 '05 #3

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

Similar topics

3
by: Mark A Framness | last post by:
Greetings, I am working on a project and we need to write a conversion script to initialize a new field on a table. The number of records on this table is on the order of millions so routine...
1
by: jijo kuruvila | last post by:
actually my code looks like this private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.Columns.Visible=true;...
3
by: Bonzol | last post by:
VB.net 2003, 1.1, webapplication hey there connecting to an Access database. There is a field in the database that is yes/no What do I pass in to the datbase to make it check the box, or uncheck...
5
by: parwal.sandeep | last post by:
Hello grp! i'm using INNODB tables which are using frequently . if i fire a SELECT query which fetch major part of table it usually take 10-20 seconds to complete. in mean time if any UPDATE...
2
by: moz2407 | last post by:
Hi, I am fairly new to SQL statements and where as i can perform retriving types of queries i have been stuck on an UPDATE one for weeks now; quite frankly its making me pull what little hair i...
5
by: tonialbrown | last post by:
I have some code that updates a record's fields based on a selection from a list box (lstDelFrom). The user selects the record from the list box & it copies it into the fields , , etc. My...
14
by: thetaamommy | last post by:
CREATE Table CO(CustID int, OfficerSID varchar(10), OfficerRank tinyint) Insert Into CO VALUES (1, 'KURT', 1) Insert Into CO VALUES (1, 'ALEX', 2) Insert Into CO
2
by: ramdil | last post by:
Hi All Please help me .Hope i have described my problem clearly below. I have a condition where i need to update some data in table from the value taken from another table.So basically i need to...
1
by: Leigh | last post by:
This is the query I have to update a column in one table, by comparing 2 of its columns to with the columns of another table. The WIE_Physician table has 1655 records, hence the loop, and the...
1
praclarush
by: praclarush | last post by:
I'm trying to connect to a access database and post an update to a row in a table though code with Visual Basic 2005, but when I do, I'm getting a Syntax error with the update statment. I don't...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.