Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles
New Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 
 

SQL coalesce function tips

Oracle Database Tips by Donald BurlesonJune 17, 2015


Question:
 I am trying to take a list of orders and create a summary that pre-calculates the total of all orders to date for each customer by month. I have data that looks like this

CUSTOMER_ID ORDER_ID ORDER_DT  ORDER_AMT
----------- -------- --------- ---------
1                  1   01-FEB-09   100
1                  2   01-MAR-09   200
1                  3   30-MAY-09   100

And I want to create output that looks like :
CUSTOMER_ID 201506 201505 201504 201503 201502 201501
------------ ------ ------ ------ ------ ------ ------
1               400    400    300     300  100 (null)

I'm not on 11g, so I cannot use the pivot operator, but I've heard that the SQL coalesce clause can be used for this purpose.

Answer:  Yes, coalesce may work, and you can move rows to columns in many ways:

Answer (By Oracle author Laurent Schneider):  (For more advanced SQL tips, see Laurent's book Advanced Oracle SQL Programming.).  When embedded inside SQL, the coalesce function returns the first non-null expression in the list.  The coalesce can be used to substitute a NULL value, much like the NVL clause:

select coalesce(mycol,'value if null') from mytab;
select nvl(mycol,'value if null')      from mytab;

However, the NVL function is Oracle specific, whereas coalesce in generic SQL.  Also,  coalesce runs faster than NVL, and coalesce is a SQL92 standard.

Laurent shows how this can be solved using the SQL coalesce clause:

select 
  coalesce(a,b,c,d,e,f) a,
  coalesce(b,c,d,e,f)   b,
  coalesce(c,d,e,f)     c,
  coalesce(d,e,f)       d,
  coalesce(e,f)         e,
  f
from
  t;

For a practical example of using the coalesce function in Oracle SQL, consider when you send a book to my delivery address, if none then you send it to my correspondence address, if none then in my main address:

select
  coalesce(delivery_addr, correspondence_addr, main_addr)
from
   author;



 

 

��  
 
 
Oracle Training at Sea
 
 
 
 
oracle dba poster
 

 
Follow us on Twitter 
 
Oracle performance tuning software 
 
Oracle Linux poster
 
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  

and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright © 1996 -  2020

All rights reserved by Burleson

Oracle ® is the registered trademark of Oracle Corporation.