2. DECODE Function, CASE Expression



Similar documents
Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Appendix A Practices and Solutions

Aggregating Data Using Group Functions

Oracle SQL. Course Summary. Duration. Objectives

Writing Control Structures

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

SQL Basics. Introduction to Standard Query Language

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database: SQL and PL/SQL Fundamentals NEW

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Displaying Data from Multiple Tables

14 Triggers / Embedded SQL

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

Oracle For Beginners Page : 1

Chapter 1. Writing Basic. SQL Statements

Subqueries Chapter 6

Instant SQL Programming

An Oracle White Paper June RESTful Web Services for the Oracle Database Cloud - Multitenant Edition

Conversion Functions

Oracle For Beginners Page : 1

SES Project v 9.0 SES/CAESAR QUERY TOOL. Running and Editing Queries. PS Query

5. CHANGING STRUCTURE AND DATA

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

3.GETTING STARTED WITH ORACLE8i

9.1 SAS. SQL Query Window. User s Guide

Netezza SQL Class Outline

Displaying Data from Multiple Tables. Chapter 4

Producing Readable Output with SQL*Plus

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Oracle Database 10g Express

Oracle Database: SQL and PL/SQL Fundamentals NEW

Microsoft Access Lesson 5: Structured Query Language (SQL)

Oracle Database: Introduction to SQL

SQL SELECT Query: Intermediate

Database Programming with PL/SQL: Learning Objectives

Using AND in a Query: Step 1: Open Query Design

Introduction to Microsoft Jet SQL

Where? Originating Table Employees Departments

Oracle Database: Introduction to SQL

Excel: Introduction to Formulas

Pivot and Un-Pivot data in SQL. White Paper

Oracle Database: Introduction to SQL

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Relational Databases

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Achieving Database Interoperability Across Data Access APIs through SQL Up-leveling

Programming with SQL

Oracle Database 10g: Introduction to SQL

SQL Introduction Chapter 7, sections 1 & 4. Introduction to SQL. Introduction to SQL. Introduction to SQL

MOC 20461C: Querying Microsoft SQL Server. Course Overview

2. Which of the following declarations is invalid? Mark for Review (1) Points

A basic create statement for a simple student table would look like the following.

Advanced PostgreSQL SQL Injection and Filter Bypass Techniques

Guide to Performance and Tuning: Query Performance and Sampled Selectivity

Oracle(PL/SQL) Training

Handling PL/SQL Errors

Oracle/SQL Tutorial 1

ICAB4136B Use structured query language to create database structures and manipulate data

Introduction to PL/SQL Programming

Displaying Data from Multiple Tables

Database programming 20/08/2015. DBprog news. Outline. Motivation for DB programming. Using SQL queries in a program. Using SQL queries in a program

Oracle Database: Develop PL/SQL Program Units

What is the value of SQL%ISOPEN immediately after the SELECT statement is executed? Error. That attribute does not apply for implicit cursors.

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

Cal Answers Analysis Training Part I. Creating Analyses in OBIEE

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

Using the SQL Procedure

ORACLE 10g Lab Guide

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Financial Data Access with SQL, Excel & VBA

Foundations & Fundamentals. A PROC SQL Primer. Matt Taylor, Carolina Analytical Consulting, LLC, Charlotte, NC

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

Mini User's Guide for SQL*Plus T. J. Teorey

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

David Dye. Extract, Transform, Load

VARRAY AND NESTED TABLE

Using SQL in RPG Programs: An Introduction

Intro to Embedded SQL Programming for ILE RPG Developers

SQL, PL/SQL FALL Semester 2013

5.1 Database Schema Schema Generation in SQL

AN INTRODUCTION TO THE SQL PROCEDURE Chris Yindra, C. Y. Associates

Relational Database: Additional Operations on Relations; SQL

The Center for Teaching, Learning, & Technology

USING MYWEBSQL FIGURE 1: FIRST AUTHENTICATION LAYER (ENTER YOUR REGULAR SIMMONS USERNAME AND PASSWORD)

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Null and Void? Dealing with Nulls in DB2

SQL. Short introduction

OBJECT ORIENTED EXTENSIONS TO SQL

Teradata Utilities Class Outline

Transcription:

DECODE Function, CASE Expression 2.1 2. DECODE Function, CASE Expression Introduction to DECODE DECODE Examples Contrast with 9i Case Statement CASE Examples CASE Histograms SKILLBUILDERS

DECODE Function, CASE Expression 2.2 Introduction to DECODE Extension to ANSI SQL IF THEN..ELSE function DECODE(value,if1,then1,if2,then2,... [,else] ) Compares value to IF values, 1 by 1 If equal match found, returns corresponding THEN value Returns ELSE value if no match found NULL if ELSE not specified 2.2 DECODE is an Oracle extension that adds an IF/THEN/ELSE function to SQL. The syntax allows the specification a limited number IF/THEN value pairs. Oracle compares the supplied VALUE to the IF values (one by one) and returns the first THEN value if an equality match is found. If a match is not found, the ELSE value is returned, if coded. If ELSE is not coded and a match is not found, NULL is returned. Notable things about DECODE: VALUE, IF, THEN and ELSE can be expressions. For example, SYSDATE- BIRTHDATE. Functions can also be used. (See examples later in this section.) In DECODE, Oracle considers two NULLs to be equivalent. The maximum number items, including VALUE, IF, THEN and ELSE values is 255.

DECODE Function, CASE Expression 2.3 Simple Example Convert state abbreviations to name 2.3 SELECT state, DECODE (state, 'NY', 'NY', 'New 'New York', 'NJ', 'NJ', 'New 'New Jersey', 'RI', 'RI', 'Rhode Island', 'Unknown') AS AS state_name FROM FROM customer; ST ST STATE_NAME STATE_NAME -- -- ------------ ------------ NY NY New New York York...... MO MO Unknown Unknown RI RI Rhode Rhode Island Island NY NY New New York York DECODE Function The example decodes state abbreviations and replaces them with state names. Note that the DECODE function has code/value pairs, followed by an optional default value to be used if the code is not found in the set code/value pairs. NULL is used if there is no default specified. Idea: DECODE is an extremely powerful function. It can be used to perform a host special manipulations such as invoice aging, flipping tables on their side, and calculating columns where the calculation itself varies by row. For example, you could calculate the new salary for all employees where the increase varies based on the employees date hire. These uses are beyond the scope this course. Oracle: The Complete Reference, the Oracle Press book from Osborne, has an entire chapter dedicated to the various ways that you can use DECODE.

DECODE Function, CASE Expression 2.4 Flip Table on Side select select cust_no cust_no,sum(decode,sum(decode (payment_method, 'CA', 'CA', total_order_price) ) cash cash,sum(decode,sum(decode (payment_method, 'VG', 'VG', total_order_price) ) visa_gold visa_gold,sum(decode,sum(decode (payment_method, 'VS', 'VS', total_order_price) ) visa_stnd visa_stnd,sum(decode,sum(decode (payment_method, 'DI', 'DI', total_order_price) ) discover discover,sum(total_order_price) total total from from ord ord group group by by cust_no; cust_no; CUST_NO CUST_NO CASH CASH VISA_GOLD VISA_GOLD VISA_STND VISA_STND DISCOVER DISCOVER TOTAL TOTAL ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 1 50.98 50.98 50.98 50.98 2 19.95 19.95 30 30 35.99 35.99 85.94 85.94 4 19.95 19.95 19.95 19.95 6 10.95 10.95 10.95 10.95 7 43.9 43.9 43.9 43.9 9 133.9 133.9 133.9 133.9 12 12 75.95 75.95 35.95 35.95 111.9 111.9 2.4 The DECODE function is ten used to turn a table on its side. Or, put another way, turn table values into columns. The essence doing this is to: DECODE the column you would like to make into columns. In this example, I have decoded PAYMENT_METHOD. Supply a relevant column for the THEN value. In this case I have coded TOTAL_ORDER_PRICE. Code NULL (or take the default NULL as in this example) for the ELSE value. Supply a meaningful column alias (column name). In this example, I have supplied cash for value CA, visa_gold, VG, etc. In this example I have also use the aggregate function SUM to display the total amount spent by a customer via cash payment method. For example, customer number 2 has spent a total $19.95 in cash, $30 in Visa Gold and $35.99 in Visa Standard. See the supplied script DECODE2.SQL for a working example.

DECODE Function, CASE Expression 2.5 DECODE and Range Comparisons Use SIGN function to build less than, greater than comparisons 2.5 select select lastname, lastname, salary, salary, decode( decode( sign( sign( salary salary - - avg_sal avg_sal ), ), 1, 1, '> '> ' ' to_char(avg_sal, to_char(avg_sal, '99.99') '99.99'),, 0, 0, '= '= ' ' to_char(avg_sal, to_char(avg_sal, '99.99'), '99.99'), -1, -1, ' ' ' ' to_char(avg_sal, to_char(avg_sal, '99.99') '99.99') ) ) sal_desc sal_desc from from employee, employee, avg_sal; avg_sal; LASTNAME LASTNAME SALARY SALARY SAL_DESC SAL_DESC -------------------- -------------------- ---------- ---------- ------------------- ------------------- Anderson Anderson 8 8 Somers Somers 8 8 Washington Washington 12 12 Doright Doright 22 22 > > Wells Wells 25 25 > > Perry Perry 9 9 Roger Roger 10 10 Hall Hall 13 13 = = Barbee Barbee 12 12 We can build a range comparison into the DECODE by incorporating the SIGN function. SIGN returns: 1 if the result is positive -1 if the result is negative 0 if the result is 0 This example assumes the following view has been created: create or replace view avg_sal as select trunc(avg(salary)) avg_sal from employee; To help understand this example, divide employee Anderson s salary by the average and subtract 1: 8-13 = -5. The SIGN function returns 1 (since the result is negative) and DECODE translates that into the string ' to_char(avg_sal, '99.99'). Try the same for Hall: 13-13 = 0. Since the result is 0, the SIGN function returns 0. See the supplied script DECODE3.SQL for a working example.

DECODE Function, CASE Expression 2.6 The CASE Expression 2.6 Range comparisons made easy select select lastname, lastname, salary, salary, CASE CASE WHEN WHEN salary salary > avg_sal avg_sal THEN THEN '> '> ' to_char(avg_sal, '99.99') '99.99') WHEN WHEN salary salary avg_sal avg_sal THEN THEN ' ' ' to_char(avg_sal, '99.99') '99.99') ELSE ELSE '= '= ' to_char(avg_sal, '99.99') '99.99') END END AS AS SAL_DESC from from employee, employee, avg_sal avg_sal LASTNAME LASTNAME SALARY SALARY SAL_DESC SAL_DESC -------------------- ---------- ---------- ------------------- Anderson Anderson 8 Somers Somers 8 Washington Washington 12 12... Oracle provided ANSI CASE support in their SQL language in release 8.1.7. It is not supported in PL/SQL until Oracle9i. (Workaround: Include it with dynamic SQL.) CASE is limited to 128 WHEN/THEN pairs (255 total values). This limit can be overcome by nesting CASE within CASE.

DECODE Function, CASE Expression 2.7 Simple CASE Searched CASE CASE Syntax CASE expression WHEN this1 THEN that1 WHEN this2 THEN that2... [ ELSE that ] END END CASE WHEN condition1 THEN return-value1 WHEN condition2 THEN return-value2... [ ELSE return-value ] END END 2.7 Oracle supports two flavors CASE, simple and searched. The simple case expression tests for an equal condition on the supplied value or expression. The first WHEN value that is equal causes Oracle to return the corresponding THEN value. If none the WHEN values match the supplied expression, the ELSE value is returned. If the ELSE is not coded, NULL is returned. The searched case (as seen in the previous example) allows multiple comparison expressions (, >, =, >=, BETWEEN, LIKE, IN, IS NULL, etc.). The first TRUE expression causes Oracle to return the corresponding THEN value. If none the WHEN values match the supplied expression, the ELSE value is returned. If the ELSE is not coded, NULL is returned.

DECODE Function, CASE Expression 2.8 CASE Nonsense CASE supports many comparisons select case case when when salary between 6 and and 8 then then '6-8' '6-8' when when salary in in (9,10) then then '9-10' when when exists (select null null from from avg_sal where where avg_sal = salary) then then 'EXISTS' when when to_char(salary) like like '2%' '2%' then then 'Like2' when when salary is is null null then then 'Null' when when emp_no in in (select mgr_no from from department) then then 'Dpt 'Dpt Mgr' Mgr' else else 'ELSE' end end AS AS case_test from from employee, avg_sal 2.8 This example illustrates the breadth support for the various relational comparison operators supported by Oracle.

DECODE Function, CASE Expression 2.9 Histograms with CASE Easily define number and width buckets select select sum sum ( case case when when salary salary between between 6 and and 10 10 then then 1 else else 0 end end ) as as sal_6_10, sal_6_10, sum sum ( case case when when salary salary between between 11 11 and and 15 15 then then 1 else else 0 end end ) as as sal_11_15, sal_11_15, sum sum ( case case when when salary salary between between 16 16 and and 20 20 then then 1 else else 0 end end ) as as sal_16_20, sal_16_20, sum sum ( case case when when salary salary between between 21 21 and and 25 25 then then 1 else else 0 end end ) as as sal_21_25 sal_21_25 from from employee; employee; SAL_6_10 SAL_6_10 SAL_11_15 SAL_11_15 SAL_16_20 SAL_16_20 SAL_21_25 SAL_21_25 ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- 4 3 0 2 2.9 Histograms can be easily created with the CASE expression. In this example, to show the distribution salary levels, I have created a histogram containing 4 buckets and each bucket has a width 5.

DECODE Function, CASE Expression 2.10 Histograms with CASE select case case when when salary between 6 and and 10 10 then then 'Salary 06-10' when when salary between 11 11 and and 15 15 then then 'Salary 11-15' when when salary between 16 16 and and 20 20 then then 'Salary 16-20' when when salary between 21 21 and and 25 25 then then 'Salary 21-25' else else 'Salary OOR' OOR' end end AS AS bucket, count(*) num_emps from from employee group group by by case case when when salary between 6 and and 10 10 then then 'Salary 06-10' when when salary between 11 11 and and 15 15 then then 'Salary 11-15' when when salary between 16 16 and and 20 20 then then 'Salary 16-20' when when salary between 21 21 and and 25 25 then then 'Salary 21-25' else else 'Salary OOR' OOR' BUCKET end; BUCKET NUM_EMPS NUM_EMPS end; ------------ ------------ ---------- ---------- Salary Salary 06-10 06-10 4 Salary Salary 11-15 11-15 3 Salary Salary 21-25 21-25 2 2.10 This example shows that the CASE expression can be used in the GROUP BY clause. Like the previous example I am showing the distribution salary levels. However, in this example, each bucket in the histogram is represented by a row in the result (as opposed to a column), and empty buckets are not included in the result.

DECODE Function, CASE Expression 2.11 Decode and Case Workshop 2.11 Using Decode and Case DECODE Workshop 1. Create a report that shows number customers per state. Use the SUM and DECODE functions to format the report similar to: NJ NY RI ---------- ---------- ---------- 2 10 1 2. Add an "OTHER" and "TOTAL" column to the report: NJ NY RI OTHER TOTAL ---------- ---------- ---------- ---------- ---------- 2 10 1 1 14

DECODE Function, CASE Expression 2.12 3. Use DECODE and SIGN functions on the ORD table to create a report similar to: SIZE_ORDER NUM_ORDERS ------------ ---------- Large Order 5 Medium Order 1 Small order 6 Assume that a $30 order (total_order_price) is a "Medium Order". Hint: You will need to repeat the DECODE function in the GROUP BY clause. CASE Workshop 1. Use the CASE expression and SUM function (in the CASE "THEN") to create the following report. The SUM_SALARY column is the sum the salary all employees with the same job title by department. DEPT_NO TITLE SUM_SALARY ---------- -------------------- ---------- 1 Designer 59 2 Sales Clerk 17 2 Sales Manager 8 3 Accountant 10 3 Sales Representative 25 Hint: GROUP BY dept_no, title

DECODE Function, CASE Expression 2.13 2. Use the CASE expression and SUM function to create an order price report similar to : PAY_METHOD SMALL_ORDER MEDIUM_ORDER LARGE_ORDER ------------- ----------- ------------ ----------- CASH 3 0 2 DISCOVER 0 0 1 VISA GOLD 1 1 0 VISA STANDARD 2 0 2 Hints: SUM the result the CASE expressions. GROUP BY the payment_method column.