Re: SQL Insert with Value & MAX?

From: Saad Ahmad <sahmad_at_mfa.com>
Date: 8 Mar 1995 15:44:59 GMT
Message-ID: <3jkjdr$i1q_at_homer.alpha.net>


William Pollack (TJFD63A_at_prodigy.com) wrote:
> Given a table with columns A and B, the combination {A,B} being the
> primary key. Given rows of {X,1}, {X,2}, and {X,3}, I'd like to INSERT
> the row {X,<Max(B)+1}. My question: is this possible in a single SQL
> statement, or must the MAX(B) WHERE A=X be saved off in a variable first,
> and INSERTed into the table (plus one)?
Try something like this
insert into table_a (A,B)
select X, nvl(max(B),0)+1
from table_a
where A=X
group by X

--
**************************************************************
*                          Saad Ahmad                        *
*                          E-Mail: sahmad_at_mfa.com            *
**************************************************************
Received on Wed Mar 08 1995 - 16:44:59 CET

Original text of this message