Solved

Add values from Col 5 to values in col 6 and put them into col 7 in the SAME oracle Table

Posted on 2016-08-22
7
54 Views
Last Modified: 2016-08-23
Hi experts,

I have a table Alpha, it has col_1, Col_2, Col_3, Col_4, Col_5, Col_6, Col_7. The datatype in each column is number (10).

I want to add values in Col_5 with values in Col_6 and store the result in col_7 for each row in the table, taking into account NULL values in either column (Col_5, Col_6).  We are adding numbers only. No data conversion. It could be a an update statement or Merge statement.

I tried this, but  did not work.

Update Alpha set Col_7 = ((Select Case when col_5 = -1 or col_6 = -1 then -1
                                                               else  col_5 + col_6
                                                       End)
                                             From Alpha);

Please help.

Thanks
0
Comment
Question by:KamalAgnihotri
7 Comments
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 41765501
update Alpha set .... From Alpha

I suppose you're getting an error?!
0
 

Author Comment

by:KamalAgnihotri
ID: 41765538
Alex, Yes I am getting an error. What is the solution.
0
 
LVL 34

Expert Comment

by:johnsone
ID: 41765590
I would guess this is what you are looking for:
UPDATE alpha 
SET    col_7 = CASE 
                 WHEN col_5 = -1 
                       OR col_6 = -1 THEN -1 
                 ELSE col_5 + col_6 
               END; 

Open in new window

Not sure why you have a sub-query.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 31

Accepted Solution

by:
awking00 earned 500 total points
ID: 41765760
Since you indicated col_5 or col_6 might contain nulls, you may want to modify johnsone's update to this
UPDATE alpha
SET    col_7 = CASE
                 WHEN col_5 = -1
                       OR col_6 = -1 THEN -1
                 ELSE nvl(col_5,0 + nvl(col_6,0)
               END;
 You might also use decode in this instance
update alpha set col_7 = decode(col_5,-1,-1,col_6,-1,-1, nvl(col_5,0) + nvl(col_6,0))
0
 
LVL 34

Expert Comment

by:johnsone
ID: 41765814
I was assuming that the original was correct, just needed the syntax fixed.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
ID: 41765853
what ? really ?

update alpha
set col7 = nvl(col5, 0) + nvl(col6, 0)

i guess I spoiled your homework now
1
 

Author Closing Comment

by:KamalAgnihotri
ID: 41766674
Thanks. That worked.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now