SQL Transformation Oracle and ANSI Standard SQL Lecture 10.

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
Advanced SQL (part 1) CS263 Lecture 7.
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
SQL - Subqueries and Schema Chapter 3.4 V3.0 Napier University Dr Gordon Russell.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Chapter 11 Group Functions
Chapter 11 Group Functions (up to p.402)
MUCH ADO ABOUT NOTHING Walter Schenk SoluTech Consulting Services Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Microsoft Excel Working with Excel Lists, Subtotals and Pivot Tables.
Instructor: Craig Duckett CASE, ORDER BY, GROUP BY, HAVING, Subqueries
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
Introduction to Structured Query Language (SQL)
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Advanced SQL SMSU Computer Services Short Course.
Microsoft Access 2010 Chapter 7 Using SQL.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
SQL Unit 5 Aggregation, GROUP BY, and HAVING Kirk Scott 1.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
SQL Joins Oracle and ANSI Standard SQL Lecture 6.
Chapter 9 Joining Data from Multiple Tables
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Advanced SQL: Triggers & Assertions
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Relational Schema and SQL Queries James Wang.
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Oracle Data Integrator User Functions, Variables and Advanced Mappings
1 Announcements Reading for next week: Chapter 4 Your first homework will be assigned as soon as your database accounts have been set up.  Expect an .
Component 4: Introduction to Information and Computer Science Unit 6: Databases and SQL Lecture 6 This material was developed by Oregon Health & Science.
Query Processing – Implementing Set Operations and Joins Chap. 19.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
 CONACT UC:  Magnific training   
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
Aggregating Data Using Group Functions
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
Using the Set Operators
Using Subqueries to Solve Queries
Writing Correlated Subqueries
Using the Set Operators
Data warehouse Design Using Oracle
Chapter 4 Summary Query.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Using Subqueries to Solve Queries
Contents Preface I Introduction Lesson Objectives I-2
Using the Set Operators
Introduction to SQL Server and the Structure Query Language
Presentation transcript:

SQL Transformation Oracle and ANSI Standard SQL Lecture 10

Copyright 2006Page 2 SQL Transformation Transformation Defined Transformation Defined DECODE() Function DECODE() Function CASE Statement CASE Statement Case Study Case Study

Copyright 2006Page 3 SQL Transformation Defined SQL Transformation aggregates and shifts rows of data into columns of information. SQL Transformation aggregates and shifts rows of data into columns of information. SQL Transformation uses: SQL Transformation uses: An aggregation function with a nested DECODE() function to conditionally evaluate, then accept or discard results. An aggregation function with a nested DECODE() function to conditionally evaluate, then accept or discard results. An aggregation function with a nested CASE statement to conditionally evaluate, then accept or discard results. An aggregation function with a nested CASE statement to conditionally evaluate, then accept or discard results. Typically the results from the aggregation function are assigned a new column name that describes what type of value is returned. Typically the results from the aggregation function are assigned a new column name that describes what type of value is returned.

Copyright 2006Page 4 SQL Transformation DECODE() function The DECODE() function is specific to the Oracle database implementation. The DECODE() function is specific to the Oracle database implementation. The DECODE() function has three prototypes: The DECODE() function has three prototypes: One acts as a simple if-then statement and discards null results. One acts as a simple if-then statement and discards null results. One acts as a simple if-then-else statement and uses the met (if condition is true) or the unmet (else the condition is false) values equally. One acts as a simple if-then-else statement and uses the met (if condition is true) or the unmet (else the condition is false) values equally. One acts as a case statement or if-then, else-if, and else statement; however, the else can be omitted. One acts as a case statement or if-then, else-if, and else statement; however, the else can be omitted. The DECODE() function supports nesting of other functions, including DECODE() function calls. The DECODE() function supports nesting of other functions, including DECODE() function calls.

Copyright 2006Page 5 SQL Transformation DECODE() function: if-then The evaluation expression can be a column variable, function expression, or literal (typically a DATE, NUMBER or VARCHAR2 data type. The evaluation expression can be a column variable, function expression, or literal (typically a DATE, NUMBER or VARCHAR2 data type. The comparison expression can be a column variable, function expression, or literal (typically a DATE, NUMBER or VARCHAR2 data type. The comparison expression can be a column variable, function expression, or literal (typically a DATE, NUMBER or VARCHAR2 data type. The resulting expression can be a column variable, function expression, or literal (typically a DATE, NUMBER or VARCHAR2 data type. The resulting expression can be a column variable, function expression, or literal (typically a DATE, NUMBER or VARCHAR2 data type. SELECT DECODE(evaluation_expression,comparison_expression,comparison_expression,resulting_expression),resulting_expression) FROM dual;

Copyright 2006Page 6 SQL Transformation DECODE() function: if-then RESULT It's true! 1 row selected. SELECT DECODE('One','One','One','It''s true!') result,'It''s true!') result FROM dual;

Copyright 2006Page 7 SQL Transformation DECODE() function: if-then RESULT <Null> 1 row selected. SELECT DECODE('One','Two','Two','It''s true!') result,'It''s true!') result FROM dual;

Copyright 2006Page 8 SQL Transformation DECODE() function: if-then-else RESULT It's false! 1 row selected. SELECT DECODE('One','Two','Two','It''s true!','It''s true!','It''s false!') result,'It''s false!') result FROM dual;

Copyright 2006Page 9 SQL Transformation DECODE() function: case RESULT It's one! 1 row selected. SELECT DECODE('One','Two','It''s two!','Two','It''s two!','One','It''s one!') result,'One','It''s one!') result FROM dual;

Copyright 2006Page 10 SQL Transformation DECODE() function: case RESULT <Null> 1 row selected. SELECT DECODE('One','Two','It''s two!','Two','It''s two!','Three','It'' three!') result,'Three','It'' three!') result FROM dual;

Copyright 2006Page 11 SQL Transformation DECODE() function: case RESULT It ' s not one! 1 row selected. SELECT DECODE('One','Two','It''s two!','Two','It''s two!','Three','It'' three!','Three','It'' three!','It''s not one!') result,'It''s not one!') result FROM dual;

Copyright 2006Page 12 SQL Transformation CASE statement The CASE statement is ANSI defined and not specific to an Oracle database implementation. The CASE statement is ANSI defined and not specific to an Oracle database implementation. The CASE function has one prototype: The CASE function has one prototype: It always uses the WHEN clause to evaluate a comparison expression as TRUE or FALSE ; and TRUE meets the condition while FALSE fails. It always uses the WHEN clause to evaluate a comparison expression as TRUE or FALSE ; and TRUE meets the condition while FALSE fails. It can nest CASE statements in the THEN clause. It can nest CASE statements in the THEN clause. It returns a NULL if none of the WHEN clauses return TRUE and there is no ELSE clause, which can be omitted. It returns a NULL if none of the WHEN clauses return TRUE and there is no ELSE clause, which can be omitted. The CASE statement supports nesting of other functions, in both the WHEN, THEN and ELSE clauses. The CASE statement supports nesting of other functions, in both the WHEN, THEN and ELSE clauses.

Copyright 2006Page 13 SQL Transformation CASE statement: Prototype The comparative expression can be a comparison of two column variables, function expressions, or literals (typically a DATE, NUMBER or VARCHAR2 data types. The comparative expression can be a comparison of two column variables, function expressions, or literals (typically a DATE, NUMBER or VARCHAR2 data types. The resulting expression can be a column variable, function expression, or literal (typically a DATE, NUMBER or VARCHAR2 data type. The resulting expression can be a column variable, function expression, or literal (typically a DATE, NUMBER or VARCHAR2 data type. SELECT CASE WHEN comparative_expression WHEN comparative_expression THEN resulting_expression THEN resulting_expression END result END result FROM dual;

Copyright 2006Page 14 SQL Transformation CASE statement: true comparison RESULT It's one! 1 row selected. SELECT CASE WHEN 'One' = 'One' THEN 'It''s one!' WHEN 'One' = 'One' THEN 'It''s one!' END result END result FROM dual;

Copyright 2006Page 15 SQL Transformation CASE statement: false w/o an else RESULT <Null> 1 row selected. SELECT CASE WHEN 'One' = 'Two' THEN 'It''s two!' WHEN 'One' = 'Two' THEN 'It''s two!' END result END result FROM dual;

Copyright 2006Page 16 SQL Transformation CASE statement: false w/an else RESULT 'It's not one!' 1 row selected. SELECT CASE WHEN 'One' = 'Two' THEN 'It''s two!' WHEN 'One' = 'Two' THEN 'It''s two!' ELSE 'It''s not one!' ELSE 'It''s not one!' END result END result FROM dual;

Copyright 2006Page 17 SQL Transformation Case Study: Problem Definition The TRANSACTION_ID is the primary key. The TRANSACTION_ID is the primary key. The TRANSACTION_ACCOUNT and TRANSACTION_DATE values are basic dimensions or filters. The TRANSACTION_ACCOUNT and TRANSACTION_DATE values are basic dimensions or filters. The TRANSACTION_AMOUNT values are basic facts or data. The TRANSACTION_AMOUNT values are basic facts or data. Problem: The amount needs to be aggregated by month and group by account with months as the columns and accounts as the rows; and a both a sum total for accounts and months. Problem: The amount needs to be aggregated by month and group by account with months as the columns and accounts as the rows; and a both a sum total for accounts and months. Name Null? Type TRANSACTION_ID NUMBER TRANSACTION_GROUP VARCHAR2(20) TRANSACTION_AMOUNT NUMBER TRANSACTION_DATE DATE

Copyright 2006Page 18 SQL Transformation Case Study: Problem Resolution The combination of a SUM() function and a CASE statement enables: The combination of a SUM() function and a CASE statement enables: Adding columns in rows where another column only meets a condition. Adding columns in rows where another column only meets a condition. Creating result set columns by using column aliasing. Creating result set columns by using column aliasing. The conditions in nested CASE statements within SUM() functions can differ between result set columns without impacting the GROUP BY process. The conditions in nested CASE statements within SUM() functions can differ between result set columns without impacting the GROUP BY process. The subtotaling of columns can be done by: The subtotaling of columns can be done by: Using the Oracle implementation specific GROUP BY CUBE() to derive the column total and the ORDER BY to sort the total to the bottom. Using the Oracle implementation specific GROUP BY CUBE() to derive the column total and the ORDER BY to sort the total to the bottom. Using a generic ANSI approach with set operations derives the column total at various levels, and then uses the ORDER BY to sort them appropriately. Using a generic ANSI approach with set operations derives the column total at various levels, and then uses the ORDER BY to sort them appropriately.

Copyright 2006Page 19 SQL Transformation Case Study: Step #1: Pivoting A sum of data, or a fact, is now identified by both the month, and year- to-date columns and account number rows. A sum of data, or a fact, is now identified by both the month, and year- to-date columns and account number rows. SELECT transaction_account account, SUM(CASE WHEN EXTRACT(MONTH FROM transaction_date) = 1 WHEN EXTRACT(MONTH FROM transaction_date) = 1 THEN transaction_amount THEN transaction_amount END) AS jan END) AS jan, SUM(CASE WHEN EXTRACT(YEAR FROM transaction_date) = 2006 WHEN EXTRACT(YEAR FROM transaction_date) = 2006 THEN transaction_amount THEN transaction_amount END) AS ytd END) AS ytd FROM transactions GROUP BY transaction_account;

Copyright 2006Page 20 SQL Transformation Case Study: Step #2: Cubing result SELECT CASE WHEN GROUPING(transaction_account) = 1 THEN 'Total:' WHEN GROUPING(transaction_account) = 1 THEN 'Total:' ELSE transaction_account ELSE transaction_account END AS account END AS account, SUM(CASE WHEN EXTRACT(MONTH FROM transaction_date) = 1 WHEN EXTRACT(MONTH FROM transaction_date) = 1 THEN transaction_amount THEN transaction_amount END) AS jan END) AS jan, SUM(CASE WHEN EXTRACT(YEAR FROM transaction_date) = 2006 WHEN EXTRACT(YEAR FROM transaction_date) = 2006 THEN transaction_amount THEN transaction_amount END) AS ytd END) AS ytd FROM transactions GROUP BY CUBE (transaction_account) ORDER BY transaction_account;

Copyright 2006Page 21 SQL Transformation Case Study: ANSI Step #1: Detail SELECT t.transaction_account account, SUM(CASE WHEN EXTRACT(MONTH FROM transaction_date) = 1 WHEN EXTRACT(MONTH FROM transaction_date) = 1 THEN t.transaction_amount THEN t.transaction_amount END) AS jan END) AS jan, SUM(CASE WHEN EXTRACT(YEAR FROM transaction_date) = 2006 WHEN EXTRACT(YEAR FROM transaction_date) = 2006 THEN transaction_amount THEN transaction_amount END) AS ytd END) AS ytd FROM transactions t GROUP BY t.transaction_account UNION ALL … total_summary … ORDER BY 1;

Copyright 2006Page 22 SQL Transformation Case Study: ANSI Step #2: Summary … detail_transaction_account_rows … UNION ALL SELECT 'Totals:' account, SUM(CASE WHEN EXTRACT(MONTH FROM transaction_date) = 1 WHEN EXTRACT(MONTH FROM transaction_date) = 1 THEN t.transaction_amount THEN t.transaction_amount END) AS jan END) AS jan, SUM(CASE WHEN EXTRACT(YEAR FROM transaction_date) = 2006 WHEN EXTRACT(YEAR FROM transaction_date) = 2006 THEN transaction_amount THEN transaction_amount END) AS ytd END) AS ytd FROM transactions t ORDER BY 1;

Copyright 2006Page 23 SQL Transformation Case Study: Subtotals Subtotals in these types of problems makes solving them more complex. Subtotals in these types of problems makes solving them more complex. Subtotals can be developed as independent queries joined by set operations, provided: Subtotals can be developed as independent queries joined by set operations, provided: There is a means to order detail rows. There is a means to order detail rows. There is a means to insert subtotal rows below their respective detail rows. There is a means to insert subtotal rows below their respective detail rows. There is a business model that clearly establishes the relationship between detail and subtotals. There is a business model that clearly establishes the relationship between detail and subtotals.

Copyright 2006Page 24 Summary Transformation Defined Transformation Defined DECODE() Function DECODE() Function CASE Statement CASE Statement Case Study Case Study