Download presentation
Presentation is loading. Please wait.
Published byTyrone May Modified over 9 years ago
1
SQL Transformation Oracle and ANSI Standard SQL Lecture 10
2
Copyright 2006Page 2 SQL Transformation Transformation Defined Transformation Defined DECODE() Function DECODE() Function CASE Statement CASE Statement Case Study Case Study
3
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.
4
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.
5
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;
6
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;
7
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;
8
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;
9
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;
10
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;
11
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;
12
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.
13
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;
14
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;
15
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;
16
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;
17
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
18
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.
19
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;
20
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;
21
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;
22
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;
23
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.
24
Copyright 2006Page 24 Summary Transformation Defined Transformation Defined DECODE() Function DECODE() Function CASE Statement CASE Statement Case Study Case Study
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.