sql transformation oracle and ansi standard sql

24
SQL Transformation SQL Transformation Oracle and ANSI Standard SQL Oracle and ANSI Standard SQL Lecture 10 Lecture 10

Upload: garrison-powers

Post on 02-Jan-2016

94 views

Category:

Documents


3 download

DESCRIPTION

SQL Transformation Oracle and ANSI Standard SQL. Lecture 10. SQL Transformation. Transformation Defined DECODE() Function CASE Statement Case Study. SQL Transformation Defined. SQL Transformation aggregates and shifts rows of data into columns of information. SQL Transformation uses: - PowerPoint PPT Presentation

TRANSCRIPT

SQL TransformationSQL Transformation Oracle and ANSI Standard SQLOracle and ANSI Standard SQL

Lecture 10Lecture 10

Copyright 2006Copyright 2006 Page Page 22

SQL TransformationSQL Transformation

Transformation DefinedTransformation Defined DECODE()DECODE() Function Function CASECASE Statement Statement Case StudyCase Study

Copyright 2006Copyright 2006 Page Page 33

SQL TransformationSQL TransformationDefinedDefined

SQL Transformation aggregates and shifts SQL Transformation aggregates and shifts rows of data into columns of information.rows of data into columns of information.

SQL Transformation uses:SQL Transformation uses: An aggregation function with a nested An aggregation function with a nested DECODE()DECODE()

function to conditionally evaluate, then accept or function to conditionally evaluate, then accept or discard results.discard results.

An aggregation function with a nested An aggregation function with a nested CASECASE statement to conditionally evaluate, then accept statement to conditionally evaluate, then accept or discard results.or discard results.

Typically the results from the aggregation Typically the results from the aggregation function are assigned a new column name that function are assigned a new column name that describes what type of value is returned. describes what type of value is returned.

Copyright 2006Copyright 2006 Page Page 44

SQL TransformationSQL TransformationDECODE()DECODE() function function

The The DECODE()DECODE() function is specific to the function is specific to the Oracle database implementation.Oracle database implementation.

The The DECODE()DECODE() function has three prototypes: function has three prototypes: One acts as a simple One acts as a simple if-thenif-then statement and statement and

discards null results.discards null results. One acts as a simple One acts as a simple if-then-elseif-then-else statement and statement and

uses the met (if condition is true) or the unmet uses the met (if condition is true) or the unmet (else the condition is false) values equally.(else the condition is false) values equally.

One acts as a One acts as a case statementcase statement or if-then, else-if, or if-then, else-if, and else statement; however, the else can be and else statement; however, the else can be omitted.omitted.

The The DECODE()DECODE() function supports nesting of function supports nesting of other functions, including other functions, including DECODE()DECODE() function function calls. calls.

Copyright 2006Copyright 2006 Page Page 55

SQL TransformationSQL Transformation DECODE()DECODE() function: function: if-thenif-then

The The evaluationevaluation expression can be a column variable, function expression can be a column variable, function expression, or literal (typically a expression, or literal (typically a DATEDATE, , NUMBERNUMBER or or VARCHAR2VARCHAR2 data type. data type.

The The comparisoncomparison expression can be a column variable, function expression can be a column variable, function expression, or literal (typically a expression, or literal (typically a DATEDATE, , NUMBERNUMBER or or VARCHAR2VARCHAR2 data type. data type.

The The resultingresulting expression can be a column variable, function expression can be a column variable, function expression, or literal (typically a expression, or literal (typically a DATEDATE, , NUMBERNUMBER or or VARCHAR2VARCHAR2 data type. data type.

SELECT DECODE(evaluation_expressionSELECT DECODE(evaluation_expression

,comparison_expression,comparison_expression

,resulting_expression),resulting_expression)

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 66

SQL TransformationSQL Transformation DECODE()DECODE() function: function: if-thenif-then

RESULTRESULT

--------------------

It's true!It's true!

1 row selected.1 row selected.

SELECT DECODE('One'SELECT DECODE('One'

,'One','One'

,'It''s true!') result,'It''s true!') result

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 77

SQL TransformationSQL Transformation DECODE()DECODE() function: function: if-thenif-then

RESULTRESULT

--------------------

<Null><Null>

1 row selected.1 row selected.

SELECT DECODE('One'SELECT DECODE('One'

,'Two','Two'

,'It''s true!') result,'It''s true!') result

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 88

SQL TransformationSQL Transformation DECODE()DECODE() function: function: if-then-if-then-

elseelse

RESULTRESULT

----------------------

It's false!It's false!

1 row selected.1 row selected.

SELECT DECODE('One'SELECT DECODE('One'

,'Two','Two'

,'It''s true!','It''s true!'

,'It''s false!') result,'It''s false!') result

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 99

SQL TransformationSQL Transformation DECODE()DECODE() function: function: casecase

RESULTRESULT

----------------------

It's one!It's one!

1 row selected.1 row selected.

SELECT DECODE('One'SELECT DECODE('One'

,'Two','It''s two!','Two','It''s two!'

,'One','It''s one!') result,'One','It''s one!') result

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 1010

SQL TransformationSQL Transformation DECODE()DECODE() function: function: casecase

RESULTRESULT

--------------------------

<Null><Null>

1 row selected.1 row selected.

SELECT DECODE('One'SELECT DECODE('One'

,'Two','It''s two!','Two','It''s two!'

,'Three','It'' three!') result,'Three','It'' three!') result

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 1111

SQL TransformationSQL Transformation DECODE()DECODE() function: function: casecase

RESULTRESULT

--------------------------

ItIt''s not one!s not one!

1 row selected.1 row selected.

SELECT DECODE('One'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;FROM dual;

Copyright 2006Copyright 2006 Page Page 1212

SQL TransformationSQL TransformationCASECASE statement statement

The The CASECASE statement is ANSI defined and not statement is ANSI defined and not specific to an Oracle database specific to an Oracle database implementation.implementation.

The The CASECASE function has one prototype: function has one prototype: It always uses the It always uses the WHENWHEN clause to evaluate a clause to evaluate a

comparison expression as comparison expression as TRUETRUE or or FALSEFALSE; and ; and TRUETRUE meets the condition while meets the condition while FALSEFALSE fails. fails.

It can nest It can nest CASECASE statements in the statements in the THENTHEN clause. clause. It returns a It returns a NULLNULL if none of the if none of the WHENWHEN clauses clauses

return return TRUETRUE and there is no and there is no ELSEELSE clause, which clause, which can be omitted.can be omitted.

The The CASECASE statement supports nesting of statement supports nesting of other functions, in both the other functions, in both the WHENWHEN, , THENTHEN and and ELSEELSE clauses. clauses.

Copyright 2006Copyright 2006 Page Page 1313

SQL TransformationSQL Transformation CASECASE statement: Prototype statement: Prototype

The The comparativecomparative expression can be a comparison of expression can be a comparison of two column variables, function expressions, or literals two column variables, function expressions, or literals (typically a (typically a DATEDATE, , NUMBERNUMBER or or VARCHAR2VARCHAR2 data types. data types.

The The resultingresulting expression can be a column variable, expression can be a column variable, function expression, or literal (typically a function expression, or literal (typically a DATEDATE, , NUMBERNUMBER or or VARCHAR2VARCHAR2 data type. data type.

SELECT CASESELECT CASE

WHEN comparative_expressionWHEN comparative_expression

THEN resulting_expressionTHEN resulting_expression

END resultEND result

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 1414

SQL TransformationSQL Transformation CASECASE statement: statement: true true

comparisoncomparison

RESULTRESULT

----------------------

It's one!It's one!

1 row selected.1 row selected.

SELECT CASESELECT CASE

WHEN 'One' = 'One' THEN 'It''s one!'WHEN 'One' = 'One' THEN 'It''s one!'

END resultEND result

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 1515

SQL TransformationSQL Transformation CASECASE statement: statement: false w/o an false w/o an

elseelse

RESULTRESULT

--------------------

<Null><Null>

1 row selected.1 row selected.

SELECT CASESELECT CASE

WHEN 'One' = 'Two' THEN 'It''s two!'WHEN 'One' = 'Two' THEN 'It''s two!'

END resultEND result

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 1616

SQL TransformationSQL Transformation CASECASE statement: false statement: false w/an w/an

elseelse

RESULTRESULT

--------------------

'It's not one!''It's not one!'

1 row selected.1 row selected.

SELECT CASESELECT 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 resultEND result

FROM dual;FROM dual;

Copyright 2006Copyright 2006 Page Page 1717

SQL TransformationSQL Transformation Case Study: Problem Case Study: Problem

DefinitionDefinition

The The TRANSACTION_IDTRANSACTION_ID is the primary key. is the primary key. The The TRANSACTION_ACCOUNTTRANSACTION_ACCOUNT and and TRANSACTION_DATETRANSACTION_DATE values are values are

basic dimensions or filters.basic dimensions or filters. The The TRANSACTION_AMOUNTTRANSACTION_AMOUNT values are basic facts or data. values are basic facts or data. Problem: The amount needs to be aggregated by month and Problem: The amount needs to be aggregated by month and

group by account with months as the columns and accounts as group by account with months as the columns and accounts as the rows; and a both a sum total for accounts and months.the rows; and a both a sum total for accounts and months.

Name Null? TypeName Null? Type

------------------ ----- ------------------------------ ----- ------------

TRANSACTION_ID NUMBERTRANSACTION_ID NUMBER

TRANSACTION_GROUP VARCHAR2(20)TRANSACTION_GROUP VARCHAR2(20)

TRANSACTION_AMOUNT NUMBERTRANSACTION_AMOUNT NUMBER

TRANSACTION_DATE DATETRANSACTION_DATE DATE

Copyright 2006Copyright 2006 Page Page 1818

SQL TransformationSQL Transformation Case Study: Problem Case Study: Problem

ResolutionResolution The combination of a The combination of a SUM()SUM() function and a function and a CASECASE statement enables: statement enables: Adding columns in rows where another column only Adding columns in rows where another column only

meets a condition.meets a condition. Creating result set columns by using column Creating result set columns by using column

aliasing.aliasing. The conditions in nested The conditions in nested CASECASE statements statements

within within SUM()SUM() functions can differ between functions can differ between result set columns without impacting the result set columns without impacting the GROUP GROUP BYBY process. process.

The subtotaling of columns can be done by:The subtotaling of columns can be done by: Using the Oracle implementation specific Using the Oracle implementation specific GROUP BY GROUP BY CUBE()CUBE() to derive the column total and the to derive the column total and the ORDER BYORDER BY to sort the total to the bottom.to sort the total to the bottom.

Using a generic ANSI approach with set operations Using a generic ANSI approach with set operations derives the column total at various levels, and then derives the column total at various levels, and then uses the uses the ORDER BYORDER BY to sort them appropriately. to sort them appropriately.

Copyright 2006Copyright 2006 Page Page 1919

SQL TransformationSQL Transformation Case Study: Step #1: PivotingCase Study: Step #1: Pivoting

A sum of data, or a fact, is now identified by both the month, A sum of data, or a fact, is now identified by both the month, and year-to-date columns and account number rows.and year-to-date columns and account number rows.

SELECT transaction_account accountSELECT transaction_account account

, SUM(CASE, SUM(CASE

WHEN EXTRACT(MONTH FROM transaction_date) = 1WHEN EXTRACT(MONTH FROM transaction_date) = 1

THEN transaction_amountTHEN transaction_amount

END) AS janEND) AS jan

, SUM(CASE, SUM(CASE

WHEN EXTRACT(YEAR FROM transaction_date) = 2006WHEN EXTRACT(YEAR FROM transaction_date) = 2006

THEN transaction_amountTHEN transaction_amount

END) AS ytdEND) AS ytd

FROM transactionsFROM transactions

GROUP BY transaction_account;GROUP BY transaction_account;

Copyright 2006Copyright 2006 Page Page 2020

SQL TransformationSQL Transformation Case Study: Step #2: Cubing Case Study: Step #2: Cubing

resultresultSELECT SELECT CASECASE WHEN GROUPING(transaction_account) = 1 THEN 'Total:'WHEN GROUPING(transaction_account) = 1 THEN 'Total:' ELSE transaction_accountELSE transaction_account END AS accountEND AS account, SUM(CASE, SUM(CASE WHEN EXTRACT(MONTH FROM transaction_date) = 1WHEN EXTRACT(MONTH FROM transaction_date) = 1 THEN transaction_amountTHEN transaction_amount END) AS janEND) AS jan, SUM(CASE, SUM(CASE WHEN EXTRACT(YEAR FROM transaction_date) = 2006WHEN EXTRACT(YEAR FROM transaction_date) = 2006 THEN transaction_amountTHEN transaction_amount END) AS ytdEND) AS ytdFROM transactionsFROM transactionsGROUP BYGROUP BYCUBE (transaction_account)CUBE (transaction_account)ORDER BY transaction_accountORDER BY transaction_account;;

Copyright 2006Copyright 2006 Page Page 2121

SQL TransformationSQL Transformation Case Study: ANSI Step #1: DetailCase Study: ANSI Step #1: Detail

SELECT t.transaction_account accountSELECT t.transaction_account account, SUM(CASE, SUM(CASE WHEN EXTRACT(MONTH FROM transaction_date) = 1WHEN EXTRACT(MONTH FROM transaction_date) = 1 THEN t.transaction_amountTHEN t.transaction_amount END) AS janEND) AS jan, SUM(CASE, SUM(CASE WHEN EXTRACT(YEAR FROM transaction_date) = 2006WHEN EXTRACT(YEAR FROM transaction_date) = 2006 THEN transaction_amountTHEN transaction_amount END) AS ytdEND) AS ytdFROM transactions tFROM transactions tGROUP BY t.transaction_accountGROUP BY t.transaction_accountUNION ALLUNION ALL… … total_summary …total_summary …ORDER BY 1;ORDER BY 1;

Copyright 2006Copyright 2006 Page Page 2222

SQL TransformationSQL Transformation Case Study: ANSI Step #2: Case Study: ANSI Step #2:

SummarySummary… … detail_transaction_account_rows …detail_transaction_account_rows …UNION ALLUNION ALLSELECT 'Totals:' accountSELECT 'Totals:' account, SUM(CASE, SUM(CASE WHEN EXTRACT(MONTH FROM transaction_date) = 1WHEN EXTRACT(MONTH FROM transaction_date) = 1 THEN t.transaction_amountTHEN t.transaction_amount END) AS janEND) AS jan, SUM(CASE, SUM(CASE WHEN EXTRACT(YEAR FROM transaction_date) = 2006WHEN EXTRACT(YEAR FROM transaction_date) = 2006 THEN transaction_amountTHEN transaction_amount END) AS ytdEND) AS ytdFROM transactions tFROM transactions tORDER BY 1;ORDER BY 1;

Copyright 2006Copyright 2006 Page Page 2323

SQL TransformationSQL Transformation Case Study: SubtotalsCase Study: Subtotals

Subtotals in these types of problems Subtotals in these types of problems makes solving them more complex.makes solving them more complex.

Subtotals can be developed as Subtotals can be developed as independent queries joined by set independent queries joined by set operations, provided: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 There is a means to insert subtotal rows

below their respective detail rows.below their respective detail rows. There is a business model that clearly There is a business model that clearly

establishes the relationship between establishes the relationship between detail and subtotals.detail and subtotals.

Copyright 2006Copyright 2006 Page Page 2424

SummarySummary

Transformation DefinedTransformation Defined DECODE()DECODE() Function Function CASECASE Statement Statement Case StudyCase Study