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

 
 Home
 E-mail Us
 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 


 

 

 


 

 

 

 

 


Oracle CASE SQL statement


Don Burleson

 

The CASE statement is like a series of IF statements, only using the key word WHEN. A CASE statement is evaluated from top to bottom. If a condition is true, then corresponding THEN clause is executed and execution jumps to the END CASE (short circuit evaluation) clause.

Consider this pseudo-code example:

case
when n_numb = 1 then v_status := 'very small'
when n_numb < 4 then v_status := 'small'
when n_numb = 5 then v_status := 'even'
when n_numb > 4 then v_status := 'large'
else v_status := 'very large'
end case;

Oracle SQL allows you to add "Boolean logic" and branching using the decode and CASE clauses.  The case statement is a more flexible extension of the Decode statement. In its simplest form the Oracle CASE function is used to return a value when a match is found:
 

SELECT last_name, commission_pct,
  (CASE commission_pct 
    WHEN 0.1 THEN 'Low' 
    WHEN 0.15 THEN 'Average'
    WHEN 0.2 THEN 'High' 
    ELSE 'N/A' 
  END ) Commission
FROM employees ORDER BY last_name; 

A more complex version is the Searched CASE expression where a comparison expression is used to find a match:
 

SELECT last_name, job_id, salary,
  (CASE
    WHEN job_id LIKE 'SA_MAN' AND salary < 12000 THEN '10%'  or %% added in the end.
    WHEN job_id LIKE 'SA_MAN' AND salary >= 12000 THEN '15%'
    WHEN job_id LIKE 'IT_PROG' AND salary < 9000 THEN '8%'
    WHEN job_id LIKE 'IT_PROG' AND salary >= 9000 THEN '12%'
    ELSE 'NOT APPLICABLE'
  END ) Raise
FROM employees;

 

Read more on CASE:

Use a CASE Statement in Oracle SQL

This assignment will ask you to modify an existing report to use the CASE statement. Your assignment is to use a CASE statement as follows:

* CASE 1: Books with total sales greater than $100,000, display “Best Seller”

* CASE 2: Books with total sales between $10,000 and $99,999 display “Average Seller”

* CASE 3: Books with sales less than $10,000 display “Poor Seller”

ANSWER:

col store_name format a25
col book_title format a25
col total_sales format $999,999
col sales format a15

break on sales skip 2

select
(case
   when sum(quantity)*book_retail_price > 100000 then 'Best Seller'
   when sum(quantity)*book_retail_price < 10000 then 'Poor Seller'
   else 'Average Seller'
   end ) sales,
   store_name,
   book_title,
   sum(quantity)*book_retail_price total_sales
from
   store,
   sales,
   book
where
   store.store_key = sales.store_key
and
   sales.book_key = book.book_key
group by
   store_name,
   book_title,
   book_retail_price
order by
   total_sales desc
;

 
Get the Complete
Oracle SQL Tuning Information 

The landmark book "Advanced Oracle SQL Tuning  The Definitive Reference"  is filled with valuable information on Oracle SQL Tuning. This book includes scripts and tools to hypercharge Oracle 11g performance and you can buy it for 30% off directly from the publisher.

 

 

��  
 
 
 
 

 
 
 

 
 
Oracle performance tuning software 
 
oracle dba poster
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 -  2014

All rights reserved by Burleson

Oracle © is the registered trademark of Oracle Corporation.