You are on page 1of 8

Table of Contents

Teradata Advanced SQL


V2.1

Module 1 Windows Aggregate Functions


Window Aggregate Functions .....................................................................................................1-4
Count Window Function ..............................................................................................................1-6
Using WITH for Final Counts .....................................................................................................1-8
Using COUNT Window Function for Final Counts ..................................................................1-10
Using WITH...BY for Group Counts .........................................................................................1-12
Using COUNT Window Function for Group Counts ................................................................1-14
SUM Window Function .............................................................................................................1-16
Using WITH...BY for Group Totals ..........................................................................................1-18
Using SUM Window Function for Group Totals.......................................................................1-20
Using WITH for Final Totals.....................................................................................................1-22
Using SUM Window Function for Final Totals.........................................................................1-24
Cumulative Sums .......................................................................................................................1-26
Cumulative Sums with Reset .....................................................................................................1-28
Moving Sums .............................................................................................................................1-30
Moving Averages .......................................................................................................................1-32
Moving Differences ...................................................................................................................1-34
Summary ....................................................................................................................................1-36
Review Exercise.........................................................................................................................1-38
Lab (1 of 2) ................................................................................................................................1-40
Lab (2 of 2) ................................................................................................................................1-42

Module 2 Ordered Analytic Function Types


Window Aggregate Function Types ............................................................................................2-4
Window Aggregate Functions Permutations ...............................................................................2-6
Group SUM Window Function....................................................................................................2-8
PARTITION with Group SUM Function ..................................................................................2-10
Group SUM and QUALIFY Clause...........................................................................................2-12
Group MINIMUM Window Function .......................................................................................2-14
Group MAXIMUM Window Function......................................................................................2-16
Cumulative SUM Window Function .........................................................................................2-18
Cumulative MINIMUM Window Function ...............................................................................2-20
Cumulative Window MIN and Max Window Function ............................................................2-22
Moving SUM Window Function ...............................................................................................2-24
Moving SUM Window Function with PARTITION BY...........................................................2-26
Moving SUM Window Function with ORDER BY ..................................................................2-28
Moving MINIMUM Window Function .....................................................................................2-30
Moving MAXIMUM Window Function ...................................................................................2-32
Remaining SUM Window Function (1of 2) ..............................................................................2-34

Remaining SUM Window Function (2of 2) .............................................................................. 2-36


Remaining MINIMUM Window Function................................................................................ 2-38
Remaining MAXIMUM Window Function .............................................................................. 2-40
Summary.................................................................................................................................... 2-42
Review Exercise ........................................................................................................................ 2-44
Lab ............................................................................................................................................. 2-46

Module 3 Enhanced Numeric Formatting Options


Standard Column Formats ........................................................................................................... 3-4
Radix and Group Separators........................................................................................................ 3-6
Integers, Fractions and Zero-Suppression ................................................................................... 3-8
Fixed Currency Options............................................................................................................. 3-10
Dual Currency Options .............................................................................................................. 3-12
Floating Currency ...................................................................................................................... 3-14
ISO Currency Symbols .............................................................................................................. 3-16
Currency Names ........................................................................................................................ 3-18
Signed Amounts......................................................................................................................... 3-20
Summary.................................................................................................................................... 3-22
Review Exercise ........................................................................................................................ 3-24
Lab ............................................................................................................................................. 3-26

Module 4 ANSI Time and Dates


Default Date Formatting .............................................................................................................. 4-4
Setting Date Defaults................................................................................................................... 4-6
Interval Data Types...................................................................................................................... 4-8
Year/Month and Day Intervals .................................................................................................. 4-10
Interval Literals in Date Arithmetic........................................................................................... 4-12
Date Literals............................................................................................................................... 4-14
Date Subtraction Yields Days.................................................................................................... 4-16
Date Subtraction Yields Years and Months............................................................................... 4-18
Interval Casting and Calculations.............................................................................................. 4-20
Extracting from Date ................................................................................................................. 4-22
ANSI Time................................................................................................................................. 4-24
Simple Day-Time Intervals........................................................................................................ 4-26
Complex Day/Time Intervals .................................................................................................... 4-28
Combining Day-Time Intervals................................................................................................. 4-30
Time Literals.............................................................................................................................. 4-32
Time Subtraction ....................................................................................................................... 4-34
Day-Time Interval Castings....................................................................................................... 4-36
Interval Casting and Calculations.............................................................................................. 4-38
Extracting from Time ................................................................................................................ 4-40
Review Exercises....................................................................................................................... 4-42
Lab (1 of 2) ................................................................................................................................ 4-44
Lab (2 of 2) ................................................................................................................................ 4-46

Module 5 ANSI Timestamps and Time Zones


ANSI Timestamp .........................................................................................................................5-4
TIMESTAMP Combined with Intervals......................................................................................5-6
Timestamp Subtraction ................................................................................................................5-8
Timestamp Casting ....................................................................................................................5-10
ODBC Date and Time Considerations.......................................................................................5-12
Time Zones ................................................................................................................................5-14
Data Types with Time Zone.......................................................................................................5-16
Setting Time Zones ....................................................................................................................5-18
A Tale of Three Cities................................................................................................................5-20
Los Angeles Adds a Row...........................................................................................................5-22
Normalizing Timestamps ...........................................................................................................5-24
Hong Kong Adds a Row ............................................................................................................5-26
The View from L.A....................................................................................................................5-28
The View from London..............................................................................................................5-30
Ordering Time Zone Output ......................................................................................................5-32
Extracting from Timestamp with Zone ......................................................................................5-34
Extracting from Timestamp (without Zone) ..............................................................................5-36
Overlaps Function ......................................................................................................................5-38
Overlapping Data Type Options ................................................................................................5-40
Review Exercises .......................................................................................................................5-42
Lab (1 of 2) ................................................................................................................................5-44
Lab (2 of 2) ................................................................................................................................5-46

Module 6 Enhanced Date/Time Formatting Options


Year, Month and Day Options .....................................................................................................6-4
Day of Week Format Options ......................................................................................................6-6
Hours, Minutes and Seconds Options ..........................................................................................6-8
TIME Precision Formatting .......................................................................................................6-10
Time Zone Placement ................................................................................................................6-12
Timestamp Formatting (1 of 2) ..................................................................................................6-14
Timestamp Formatting (2 of 2) ..................................................................................................6-16
Summary ....................................................................................................................................6-18
Review Exercise.........................................................................................................................6-20
Lab .............................................................................................................................................6-22

Module 7 Correlated Subqueries and Derived Tables


Traditional Subqueries................................................................................................................. 7-4
Correlated Subqueries.................................................................................................................. 7-6
Another Correlated Subquery ...................................................................................................... 7-8
Correlated Subqueries and Joins................................................................................................ 7-10
EXISTS Operator....................................................................................................................... 7-12
Correlated Subqueries Using EXISTS....................................................................................... 7-14
Null Logic.................................................................................................................................. 7-16
How NOT IN Works ................................................................................................................. 7-18
How NOT EXISTS Works ........................................................................................................ 7-20
NOT IN with Null Inner Rows .................................................................................................. 7-22
NOT EXISTS with Null Inner Rows......................................................................................... 7-24
Summary of Null Exclusion Processing .................................................................................... 7-26
Using Interim Tables ................................................................................................................. 7-28
Derived Tables........................................................................................................................... 7-30
Derived Tables with Groups...................................................................................................... 7-32
Lab ............................................................................................................................................. 7-34
Lab (Optional) ........................................................................................................................... 7-36

Module 8 Advanced Ranking Functions


Ranking Review........................................................................................................................... 8-4
Qualified Ranking........................................................................................................................ 8-6
Ranking with Qualification and PARTITION BY ...................................................................... 8-8
Expanding RANK Scope........................................................................................................... 8-10
Ranking with Aggregations ....................................................................................................... 8-12
Low Order Ranking ................................................................................................................... 8-14
RANK and ORDER BY ............................................................................................................ 8-16
ROW_NUMBER Function (1 of 2)........................................................................................... 8-18
ROW_NUMBER Function (2 of 2)........................................................................................... 8-20
PERCENT_RANK Function (1 of 2) ........................................................................................ 8-22
PERCENT_RANK Function (2 of 2) ........................................................................................ 8-24
Lab ............................................................................................................................................. 8-26

Module 9 Quantiles and Width Buckets


OLAP Aggregation Functions ..................................................................................................... 9-4
Using Quantiles ........................................................................................................................... 9-6
Aggregates and Quantiles ............................................................................................................ 9-8
Quantiles and Derived Tables.................................................................................................... 9-10
Varying Quantiles...................................................................................................................... 9-12
Ordering Quantiles..................................................................................................................... 9-14
WIDTH_BUCKET (1 of 3) ....................................................................................................... 9-16
WIDTH_BUCKET (2 of 3) ....................................................................................................... 9-18
WIDTH_BUCKET (3 of 3) ....................................................................................................... 9-20
Review Exercises....................................................................................................................... 9-22
Lab ............................................................................................................................................. 9-24

Module 10 Advanced Sampling and Random Functions


Sampling ....................................................................................................................................10-4
Sampling Distinct Values...........................................................................................................10-6
Advanced Sampling Features.....................................................................................................10-8
Stratified Sampling Setup ........................................................................................................10-10
Stratified Sampling Example ...................................................................................................10-12
Sampling with Replacement (1 of 2) .......................................................................................10-14
Sampling with Replacement (2 of 2) .......................................................................................10-16
Sample Function with Randomization .....................................................................................10-18
RANDOM Function.................................................................................................................10-20
Duplicate RANDOM Values (1 of 2) ......................................................................................10-22
Duplicate RANDOM Values (2 of 2) ......................................................................................10-24
Using RANDOM for Sampling ...............................................................................................10-26
Using SAMPLE for Sampling .................................................................................................10-28
Using RANDOM for Sample Data ..........................................................................................10-30
Limitations on Use of RANDOM ............................................................................................10-32
Review Questions ....................................................................................................................10-34
Lab ...........................................................................................................................................10-36

Module 11 Adding Table Constraints and Attributes


CREATE TABLE Elements ......................................................................................................11-4
Column Definitions....................................................................................................................11-6
Null Compression ......................................................................................................................11-8
Value Compression ..................................................................................................................11-10
Compression Suggestions ........................................................................................................11-12
Uppercase Compression...........................................................................................................11-14
Compression Limitations .........................................................................................................11-16
Primary Key vs. Primary Index................................................................................................11-18
Creating Indexes.......................................................................................................................11-20
CREATE TABLE Example .....................................................................................................11-22
Column-level Constraints.........................................................................................................11-24
Constraints as Indexes..............................................................................................................11-26
Table-level Constraints ............................................................................................................11-28
CREATE TABLE with PK Constraint ....................................................................................11-30
CREATE TABLE Exercise .....................................................................................................11-32
CREATE TABLE with FK and PK Constraints ......................................................................11-34
HELP CONSTRAINT .............................................................................................................11-36
Altering Table Constraints .......................................................................................................11-38
Parent-Child Relationships ......................................................................................................11-40
Fixing Referential Integrity Problems......................................................................................11-42
Drop Inconsistent References ..................................................................................................11-44
Circular References..................................................................................................................11-46
Using Macros to Preserve Integrity..........................................................................................11-48
Using Constraints to Preserve Integrity ...................................................................................11-50
Review Exercise.......................................................................................................................11-52
Lab (1 of 2) ..............................................................................................................................11-54
Lab (2 of 2) ..............................................................................................................................11-56
Lab (Optional)..........................................................................................................................11-58

Module 12 Volatile and Global Temporary Tables


Why Interim Tables? ................................................................................................................. 12-4
Accessing an Interim Table ....................................................................................................... 12-6
Temp Table Choices .................................................................................................................. 12-8
Derived Tables Revisited......................................................................................................... 12-10
Volatile Temporary Tables ...................................................................................................... 12-12
Volatile Table Restrictions ...................................................................................................... 12-14
Using Volatile Tables (1 of 2) ................................................................................................. 12-16
Using Volatile Tables (2 of 2) ................................................................................................. 12-18
Getting Help on Volatile Tables .............................................................................................. 12-20
Limitations on Volatile Tables ................................................................................................ 12-22
Volatile Table Quiz.................................................................................................................. 12-24
Global Temporary Tables ........................................................................................................ 12-26
Creating Temporary Tables ..................................................................................................... 12-28
Space Allocations .................................................................................................................... 12-30
Getting Help on Temporary Tables ......................................................................................... 12-32
Using Temporary Tables ......................................................................................................... 12-34
Temporary Tables and DDL (1 of 2)....................................................................................... 12-36
Temporary Tables and DDL (2 of 2)....................................................................................... 12-38
Creating Secondary Indexes .................................................................................................... 12-40
Dropping Secondary Indexes................................................................................................... 12-42
Collecting Statistics ................................................................................................................. 12-44
Dropping Statistics................................................................................................................... 12-46
Review Exercises..................................................................................................................... 12-48
Lab Exercises.................................................................................Error! Bookmark not defined.
Optional Lab Exercises..................................................................Error! Bookmark not defined.
More Optional Lab Exercises ........................................................Error! Bookmark not defined.

Module 13 Table Creation from Existing Tables


Creating Empty Tables Using Existing Definitions .................................................................. 13-4
Copied Attributes....................................................................................................................... 13-6
Overriding Copied Attributes .................................................................................................... 13-8
Creating Tables Using Subqueries........................................................................................... 13-10
Renaming Columns ................................................................................................................. 13-12
Changing Column Attributes................................................................................................... 13-14
Creating Populated Tables Using Existing Tables .................................................................. 13-16
Using Joins in a Subquery ....................................................................................................... 13-18
Using Calculations and Expressions........................................................................................ 13-20
Overriding Data Types ............................................................................................................ 13-22
Setting Default Titles............................................................................................................... 13-24
Renaming Columns Using the AS Clause ............................................................................... 13-26
Adding Unique and Primary Key Constraints ......................................................................... 13-28
Volatile and Temporary Tables ............................................................................................... 13-30
Using DEFAULT Values ........................................................................................................ 13-32
Populating NOT NULL Columns............................................................................................ 13-34
Populating DEFAULT Columns ............................................................................................. 13-36
Lab ........................................................................................................................................... 13-38

Module 14 Extended Grouping Functions


Extended Grouping Functions ...................................................................................................14-4
Aggregation Review ..................................................................................................................14-6
The ROLLUP Function: One Level...........................................................................................14-8
ROLLUP Function: Two Levels..............................................................................................14-10
Reversing the ROLLUP Sequence...........................................................................................14-12
ROLLUP with NULL ..............................................................................................................14-14
The GROUPING Function.......................................................................................................14-16
GROUPING, CASE and COALESCE ....................................................................................14-18
CUBE/ROLLUP Comparison..................................................................................................14-20
The CUBE Function.................................................................................................................14-22
Reversing CUBE Parameters ...................................................................................................14-24
Adding GROUPING Function to CUBE .................................................................................14-26
Multi-Dimensional Cubes ........................................................................................................14-28
GROUPING SETS/ROLLUP Comparison .............................................................................14-30
The GROUPING SETS Function ............................................................................................14-32
Adding Grand Totals................................................................................................................14-34
Combining GROUPING SETS (1 of 2)...................................................................................14-36
Combining GROUPING SETS (2 of 2)..................................................................................14-38
The Multiplier Effect ...............................................................................................................14-40
Multiple GROUPING SETS Clauses (1 of 2) .........................................................................14-42
Multiple GROUPING SETS Clauses (2 of 2) .........................................................................14-44
Three Approaches ....................................................................................................................14-46
Grouping Exercise....................................................................................................................14-48
Lab (1 of 2) ..............................................................................................................................14-50
Lab (2 of 2) ..............................................................................................................................14-52

Module 15 MERGE INTO Statement


Transaction Semantics: BTET Mode .........................................................................................15-4
Transaction Semantics: ANSI Mode..........................................................................................15-6
Transaction ROLLBACK: ANSI Mode.....................................................................................15-8
Transaction Semantics: ANSI vs. BTET Mode .......................................................................15-10
DDL and Transactions .............................................................................................................15-12
Traditional Upsert Processing ..................................................................................................15-14
UPDATE Form of UPSERT: Restrictions...............................................................................15-16
MERGE INTO Statement: Example 1.....................................................................................15-18
MERGE INTO Statement: Example 2.....................................................................................15-20
Creating a MERGE INTO Macro ............................................................................................15-22
Executing a MERGE INTO Macro..........................................................................................15-24
Using a Subquery with MERGE INTO ...................................................................................15-26
MERGE Form of UPSERT: Fewer Restrictions......................................................................15-28
MERGE INTO and Partitioned Primary Index........................................................................15-30
Lab (1 of 2) ..............................................................................................................................15-32
Lab (2 of 2) ..............................................................................................................................15-34
Lab Optional (1 of 2)................................................................................................................15-36
Lab Optional (2 of 2)................................................................................................................15-38

Module 16 Recursive Queries


Recursive Query Description..................................................................................................... 16-4
Building Tables For Recursive Processing................................................................................ 16-6
Creating a One Stopover Table............................................................................................... 16-8
Querying Two Levels of Recursion......................................................................................... 16-10
Creating a Two Stopover Table (1 of 2) ............................................................................... 16-12
Creating a Two Stopover Table (2 of 2) ............................................................................... 16-14
Querying Three Levels of Recursion....................................................................................... 16-16
Creating a Recursive Table...................................................................................................... 16-18
Creating Recursive Views ....................................................................................................... 16-20
Accessing Via a Recursive View............................................................................................. 16-22
Recursive INSERT SELECT................................................................................................... 16-24
Recursive CREATE TABLE................................................................................................... 16-26
Recursion Limitations.............................................................................................................. 16-28
Traditional Derived Tables ...................................................................................................... 16-30
Creating 'Derived Tables' using WITH. .................................................................................. 16-32
Summary.................................................................................................................................. 16-34
Review Exercise ...................................................................................................................... 16-36
Lab ........................................................................................................................................... 16-38

Appendix A Customer Service Database Tables & Data


Appendix B Lab Solutions
Appendix C Review Exercise Answers

You might also like