teradata division training - online tutorials ( books...

784
Teradata Division Training Introduction Advanced Teradata Structured Query Language Student Guide Course #25966 Version 1.2.2 Advanced Teradata Structured Query Language

Upload: lytu

Post on 20-Jul-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Introduction

Advanced TeradataStructured Query Language

Student Guide

Course #25966Version 1.2.2

Advanced Teradata Structured Query Language

Page 2: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CICS ,CICS/ESA, CICS/VS, Data Base 2, DB2, IBM, MVS/ESA, MVS/XA, QMS, RACF, SQL/DS, VM/XA and VTAM are trademarks or registered trademarks of International Business Machines Corporation in the U.S. and other countries. MICROSOFT, MS-DOS, DOS/V, and WINDOWS are registered trademarks of Microsoft Corporation. NCR is the name and mark of NCR Corporation. Sabre is the trademark of Seagate Technology, Inc. SAS and SAS/C are registered trademarks of the SAS Institute Inc. Sun and SunOS are trademarks of Sun Microsystems, Incorporated. The Teradata logo and Ynet are trademarks, and DBC/1012 and Teradata are registered trademarks of NCR Corporation.UNIX is a registered trademark in the U.S. and other countries, licensed exclusively through X/Open Company Limited. X/Open and the X device are trademarks of X/Open Company Limited. The materials included in this book are a licensed product of NCR Corporation.

Copyright © 2003-2007 By NCR Corporation Rancho Bernardo, CA U.S.A. All Rights Reserved

Printed in U.S.A.

Advanced Teradata Structured Query Language

Page 3: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Objectives and Prerequisites

Recommended Prerequisite Knowledge:• Teradata SQL Intro - Course #25965 or,• Teradata SQL Intro WBT • Familiarity with one of the following editors:

vi - UNIX systemsXEDIT/XVS - IBM systemsWordPad/Notepad - Windows based systems

• Familiarity with one of the following SQL front-ends:BTEQTeradata SQL Assistant

Course Objectives:• Solve query requirements using advanced features of Teradata SQL.• Distinguish Teradata extensions and ANSI features of SQL.

The accelerated version of this course, known as ‘Turbo SQL’, is a one-day overview of the topics covered in this course, with particular attention to those features which are Teradata-specific. It is intended for students with a strong background in SQL, usually experienced with the features and syntax of another vendor. Labs and in-class exercises are not done as part of this offering.Since all topics covered in the Advanced SQL course are, by definition, advanced topics, no icon will be used to designate ‘Turbo’ pages.

Accelerated (Turbo) Advanced SQL

Advanced Teradata Structured Query Language

Page 4: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Advanced TeradataStructured Query Language

Table of ContentsModule 1 Window Aggregate FunctionsModule 2 Ordered Analytic Function Types Module 3 Enhanced Numeric Formatting OptionsModule 4 ANSI Time and DatesModule 5 ANSI Timestamps and Time ZonesModule 6 Enhanced Date/Time Formatting OptionsModule 7 Correlated Subqueries and Derived TablesModule 8 Advanced Ranking FunctionsModule 9 Quantiles and Width BucketsModule 10 Advanced Sampling and Random FunctionsModule 11 Adding Table Constraints and AttributesModule 12 Volatile and Global Temporary TablesModule 13 Table Creation from Existing TablesModule 14 Extended Grouping FunctionsModule 15 MERGE INTO StatementModule 16 Recursive Queries

This presentation is a licensed product of Teradata, a Division of NCR Corporation.Copyright ©2003-2007By NCR CorporationDayton, OH U.S.A.All Rights Reserved

Advanced Teradata Structured Query Language

Page 5: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table Of Contents 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 Advanced Teradata Structured Query Language

Page 6: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

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

Advanced Teradata Structured Query Language

Page 7: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

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

Advanced Teradata Structured Query Language

Page 8: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

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

Advanced Teradata Structured Query Language

Page 9: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

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

Advanced Teradata Structured Query Language

Page 10: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

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 ........................................................................................................................................... 12-50 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

Advanced Teradata Structured Query Language

Page 11: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

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

Advanced Teradata Structured Query Language

Page 12: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

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

Advanced Teradata Structured Query Language

Page 13: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 1

Module 1Window Aggregate Functions

After completing this module, you should be able to:

• Produce reports requiring group-based counts using the COUNT Window function.

• Produce reports requiring group-based counts using the SUM Window function.

• Produce reports showing cumulative sums, moving sums, moving averages and moving differences.

Window Aggregate Functions Page 1-1

Page 14: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Window Aggregate Functions Page 1-2

Page 15: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Window Aggregate Functions Page 1-3

Page 16: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Window Aggregate Functions An aggregate function on which a window specification is applied is called a window aggregate function. Without a window specification, aggregate functions return one value for all qualified rows examined. Window aggregate functions return a new value for each of the qualifying rows participating in the query. Window aggregate functions are used to perform on-line analytical processing, known as OLAP, which permits analysis and mining of data in the data warehouse or data mart. Several standard functions are available to perform this analysis. In this module, the following functions will be discussed: • Count Window - Generation of total counts and sub-counts. • Sum Window - Generation of totals and subtotals. • Cumulative Sum -- Permits cumulative summation of a value over time or any dimension. • Moving Average -- Permits computation of a moving average based on a specified window. • Moving Sum -- Permits calculation of a moving sum over time based on a specified window. • Moving Difference -- Permits calculation of a moving difference over time based on a

specified window. Like traditional aggregate functions, window aggregate functions operate on groups of rows and permit qualification and filtering of the group result. Unlike aggregations, OLAP functions return individual detail rows of information, not just aggregations. A window specification can be applied to the following ANSI SQL-99-compliant aggregate functions: • AVG • COUNT • MAX • MIN • SUM OLAP functions may be performed on all tables and views and may be used to populate tables in conjunction with INSERT SELECT.

Window Aggregate Functions Page 1-4

Page 17: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Window Aggregate Functions

Similarity to traditional aggregate functions:

Operate on groups of rows (like a GROUP BY clause).Produce totals and subtotals.

Operate on groups of rows (like a GROUP BY clause).Produce totals and subtotals.

Differences from traditional aggregate functions:

Return data value for each qualifying row - not group.May not be performed within subqueries.May not be combined with traditional aggregations (i.e. SUM)

Return data value for each qualifying row - not group.May not be performed within subqueries.May not be combined with traditional aggregations (i.e. SUM)

Tables (Perm, Temp, Derived)ViewsINSERT/SELECT populations

Tables (Perm, Temp, Derived)ViewsINSERT/SELECT populations

Window functions may be performed on:

Window aggregate functions in Teradata produce these types of OLAP results:

Window functions are identifiable by the OVER( ) clause.Window functions are identifiable by the OVER( ) clause.

• Sub-counts• Final counts

• Cumulative Sums• Moving Sums

• Moving Averages• Moving Differences

• Sub-totals• Final totals

Window Aggregate Functions Page 1-5

Page 18: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Count Window Function The COUNT Window function may be used to perform the following operations: • Count the number of rows in a defined group • Count the number of rows in all groups A similar result can be produced using the WITH and WITH...BY extensions to Teradata SQL. The WITH...BY function is used to produce sub-totals (and sub-counts) over the detail data for a group, as defined by the BY clause. Similarly, the WITH function is used to provide grand totals (and grand counts) over the detail data for all rows. The COUNT Window function is an ANSI-standard function, whereas the WITH and WITH...BY functions are not. The COUNT Window function also produces true relational output (uniform columns and rows) whereas WITH and WITH...BY do not. Note: It is because of the non-relational format of output returned by the WITH and WITH…BY extensions that prohibits their use in an ODBC environment. The window feature is ANSI SQL-99 compliant and provides a way to dynamically define a subset of data, or window, in an ordered relational database table. A window is specified by the OVER() phrase, which can include the following clauses inside the parentheses: • PARTITION BY • ORDER BY • ROWS The window feature can be applied to the ANSI SQL-99-compliant window functions: • AVG • COUNT • MAX • MIN • PERCENT_RANK • RANK • ROW_NUMBER • SUM

Window Aggregate Functions Page 1-6

Page 19: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

COUNT Window Function

COUNT Window function may be used to perform these operations:• Count the number of rows in a defined group • Count the number of rows in all groups

Similar Functionality to WITH and WITH … BYSimilar Functionality to WITH and WITH … BY

ANSI Count Window Syntax Teradata Extensions

COUNT OVER WITH

COUNT OVER WITH BY

BTEQ and QUERYMAN Supported BTEQ Supported

Window Aggregate Functions Page 1-7

Page 20: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using WITH for Final Counts Using the WITH clause allows generation of final counts. It is a non-ANSI Teradata SQL extension. Consider the following example using the WITH clause. Example

Produce a report showing the employees in departments 410 and 403 by last name, their salaries and the total number of people in both departments. Use the WITH feature of Teradata SQL. Solution and results are seen on the facing page. The WITH clause generates a total count of all rows in the sample. The COUNT Window function may also be used to generate final counts, comparably to the WITH clause capability.

Window Aggregate Functions Page 1-8

Page 21: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using WITH for Final Counts

Name Salary DeptBrown 43100.00 401Hoover 25525.00 401Johnson 36300.00 401Machado 32300.00 401Phillips 24500.00 401Rogers 46000.00 401Trader 37850.00 401Brown 43700.00 403Charles 39500.00 403Hopkins 37900.00 403Lombardo 31000.00 403Ryan 31200.00 403Villegas 49700.00 403

Total_Count 13

Name Salary DeptBrown 43100.00 401Hoover 25525.00 401Johnson 36300.00 401Machado 32300.00 401Phillips 24500.00 401Rogers 46000.00 401Trader 37850.00 401Brown 43700.00 403Charles 39500.00 403Hopkins 37900.00 403Lombardo 31000.00 403Ryan 31200.00 403Villegas 49700.00 403

Total_Count 13

SELECT last_name AS Name,salary_amount AS Salary,department_numberAS Dept

FROM employeeWHERE Dept IN (401,403)WITH COUNT(Dept)(TITLE 'Total_Count')ORDER BY 3,1;

Show employee details and total number of employees:

Note that the output from this query is a non-relational output.

The ‘total row’ is of a different format than the data rows.

Note that the output from this query is a non-relational output.

The ‘total row’ is of a different format than the data rows.

Window Aggregate Functions Page 1-9

Page 22: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using COUNT Window Function for Final Counts Consider the same problem using the COUNT Window function. Example: Produce a report showing the employees in departments 401 and 403 by last name, their salaries and the total number of people in both departments. Use the COUNT Window feature. Solution and results are seen on facing page. Rather than generate a separate row for the result, the total is included on each row of output. The format of the rows is uniform. The following is an equivalent query to the query on the f acing page. It returns the same results. SELECT last_name AS Name ,salary_amount AS Salary ,department_number AS Dept ,COUNT(salary) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS Total_Count FROM employee WHERE Dept IN (401,403) ORDER BY 3,1; The clause ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING Is the default for all window aggregate functions.

Window Aggregate Functions Page 1-10

Page 23: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using COUNT Window Function for Final Counts

Name Salary Dept Total_CountBrown 43100.00 401 13Hoover 25525.00 401 13Johnson 36300.00 401 13Machado 32300.00 401 13Phillips 24500.00 401 13Rogers 46000.00 401 13Trader 37850.00 401 13Brown 43700.00 403 13Charles 39500.00 403 13Hopkins 37900.00 403 13Lombardo 31000.00 403 13Ryan 31200.00 403 13Villegas 49700.00 403 13

Name Salary Dept Total_CountBrown 43100.00 401 13Hoover 25525.00 401 13Johnson 36300.00 401 13Machado 32300.00 401 13Phillips 24500.00 401 13Rogers 46000.00 401 13Trader 37850.00 401 13Brown 43700.00 403 13Charles 39500.00 403 13Hopkins 37900.00 403 13Lombardo 31000.00 403 13Ryan 31200.00 403 13Villegas 49700.00 403 13

Show employee details and total number of employees:

SELECT last_name AS Name,salary_amount AS Salary,department_numberAS Dept,COUNT(salary) OVER ( ) AS Total_Count

FROM employeeWHERE Dept IN (401,403)ORDER BY 3,1;

Note that the generated output is relational in form.

All rows and columns are uniform.

Note that the generated output is relational in form.

All rows and columns are uniform.

Window Aggregate Functions Page 1-11

Page 24: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using WITH...BY for Group Counts Using the WITH...BY clause allows generation of sub-total counts based on groupings identified in the BY clause. It is a non-ANSI Teradata SQL extension. Consider the following example which demonstrates a report showing a count of employees by department. Produce a report showing the people in departments 401 and 403, their salaries, and how many people are in each department. Use the WITH...BY feature of Teradata SQL. Solution and results are seen on the facing page.

Window Aggregate Functions Page 1-12

Page 25: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using WITH...BY for Group Counts

Name Salary DeptTrader 37850.00 401Hoover 25525.00 401Johnson 36300.00 401Phillips 24500.00 401Machado 32300.00 401Brown 43100.00 401Rogers 46000.00 401

Dept_Count 7

Lombardo 31000.00 403Brown 43700.00 403Villegas 49700.00 403Hopkins 37900.00 403Charles 39500.00 403Ryan 31200.00 403

Dept_Count 6

Name Salary DeptTrader 37850.00 401Hoover 25525.00 401Johnson 36300.00 401Phillips 24500.00 401Machado 32300.00 401Brown 43100.00 401Rogers 46000.00 401

Dept_Count 7

Lombardo 31000.00 403Brown 43700.00 403Villegas 49700.00 403Hopkins 37900.00 403Charles 39500.00 403Ryan 31200.00 403

Dept_Count 6

SELECT last_name AS Name,salary_amount AS Salary,department_number AS Dept

FROM employeeWHERE Dept IN (401,403)WITH COUNT(Dept)(TITLE 'Dept_Count') BY Dept;

Note that the output from this query is a non-relational output.

The sub-total rows are of a different format than the data rows.

Note that the output from this query is a non-relational output.

The sub-total rows are of a different format than the data rows.

Show employee details and the number of employees indepartments 401 and 403 using the WITH…BY facility:

Window Aggregate Functions Page 1-13

Page 26: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using COUNT Window Function for Group Counts Consider the same problem done using the COUNT Window function. Example Produce a report showing the people in departments 401 and 403, their salaries, and how many people are in each department. Use the COUNT Window function. Solution and results are seen on the facing page. Note that the generated output is relational in form. All rows and columns are uniform. The PARTITION BY clause defines the group whose rows will be counted by the COUNT Window Function. This count is displayed as part of each row within the group, thus it may appear multiple times. The COUNT Window function permits viewing the detail data of each group, whereas the standard COUNTaggregate function does not show detail data, but only the aggregation at the group level. The COUNT Window function and standard aggregate functions (i.e., SUM, AVG, COUNT, MIN, MAX) may not be combined in a single SELECT statement. The following is an equivalent query to the query on the f acing page. It returns the same results. SELECT last_name AS Name ,salary_amount AS Salary ,department_number AS Dept ,COUNT(Salary) OVER (PARTITION BY Dept ROWS BETWEEN UNBOUNDED PRECEDING AND

UNBOUNDED FOLLOWING) AS Dept_Count FROM employee WHERE Dept IN (401,403); The clause: ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING Is the default for all window aggregate functions.

Window Aggregate Functions Page 1-14

Page 27: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using COUNT Window Function for Group Counts

Name Salary Dept Dept_CountTrader 37850.00 401 7Johnson 36300.00 401 7Phillips 24500.00 401 7Rogers 46000.00 401 7Machado 32300.00 401 7Brown 43100.00 401 7Hoover 25525.00 401 7Charles 39500.00 403 6Brown 43700.00 403 6Hopkins 37900.00 403 6Ryan 31200.00 403 6Lombardo 31000.00 403 6Villegas 49700.00 403 6

Note that the generated output is relational in form.

All rows and columns are uniform.

Note that the generated output is relational in form.

All rows and columns are uniform.

SELECT last_name AS Name,salary_amount AS Salary,department_numberAS Dept,COUNT(Salary) OVER (PARTITION BY Dept)

AS Dept_Count FROM employeeWHERE Dept IN (401,403);

Show employee details and the number of employees indepartments 401 and 403using the COUNT Window function.

Window Aggregate Functions Page 1-15

Page 28: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

SUM Window Function The SUM Window function may be used to perform the following operations: • Sum a column from rows in a defined group. • Sum a column from all rows in all groups. A similar result can be produced using the WITH and WITH...BY extensions to SQL. The WITH...BY function is used to produce sub-totals (and sub-counts) over the detail data for a group, as defined by the BY clause. Similarly, the WITH function is used to provide grand totals (and grand counts) over the detail data for all rows. The SUM Window function is an ANSI-standard function, whereas the WITH and WITH...BY functions are not.

Window Aggregate Functions Page 1-16

Page 29: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

SUM Window Function

SUM Window function may be used to perform these operations:• Sum a column from rows in a defined group• Sum a column from all rows in all groups

Similar functionality to WITH and WITH…BYSimilar functionality to WITH and WITH…BY

ANSI Compliant Teradata Extensions

SUM OVER WITH

SUM OVER WITH BY

BTEQ and QUERYMAN Supported BTEQ Supported

Window Aggregate Functions Page 1-17

Page 30: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using WITH...BY for Group Totals Using the WITH BY clause allows generation of detail rows with sub-totals based on groupings identified in the BY clause. Contents of Salestbl: storeid prodid sales ----------- ---------- ------------- 1001 A 100000.00 1001 C 60000.00 1001 D 35000.00 1001 F 150000.00 1002 A 40000.00 1002 C 35000.00 1002 D 25000.00 1003 A 30000.00 1003 B 65000.00 1003 C 20000.00 1003 D 50000.00

Window Aggregate Functions Page 1-18

Page 31: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using WITH...BY for Group Totals

storeid prodid sales1001 A 100000.001001 C 60000.001001 D 35000.001001 F 150000.00

Sum(sales) 345000.00

1002 A 40000.001002 C 35000.001002 D 25000.00

Sum(sales) 100000.00

1003 A 30000.001003 B 65000.001003 C 20000.001003 D 50000.00

Sum(sales) 165000.00

storeid prodid sales1001 A 100000.001001 C 60000.001001 D 35000.001001 F 150000.00

Sum(sales) 345000.00

1002 A 40000.001002 C 35000.001002 D 25000.00

Sum(sales) 100000.00

1003 A 30000.001003 B 65000.001003 C 20000.001003 D 50000.00

Sum(sales) 165000.00

SELECT storeid, prodid, sales

FROM salestblWITH SUM(sales) BY storeidORDER BY 1,2;

Note that the output from this query is a non-relational output.

The sub-total rows are of a different format than the data rows.

Note that the output from this query is a non-relational output.

The sub-total rows are of a different format than the data rows.

Show sales details and sub-totals by store using the WITH BY facility:

Window Aggregate Functions Page 1-19

Page 32: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using SUM Window Function for Group Totals Consider the same problem solved using the SUM Window function. Produce a report showing the sales for each product by store and the total sales for each store. Use the SUM Window feature of Teradata SQL. Solution and result seen on facing page. The PARTITION BY clause defines the group whose rows will be totalled by the SUM Window Function. This count is displayed as part of each row within the group, thus it may appear multiple times. The SUM Window function permits viewing the detail data of each group, whereas the standard SUM function does not show detail data, but only the aggregation at the group level. The SUM Window function and standard aggregate functions (such as SUM) may not be combined in a single SELECT statement. The following is an equivalent query to the query on the f acing page. It returns the same results.

SELECT storeid , prodid , sales , SUM(sales) OVER (PARTITION BY storeid ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM salestbl ORDER BY 1,2;

Window Aggregate Functions Page 1-20

Page 33: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using SUM Window Function for Group Totals

Note that the generated output is relational in form.

All rows and columns are uniform.

Note that the generated output is relational in form.

All rows and columns are uniform.

SELECT storeid, prodid, sales, SUM(sales) OVER (PARTITION BY storeid)

FROM salestblORDER BY 1,2;

storeid prodid sales Group Sum(sales)1001 A 100000.00 345000.001001 C 60000.00 345000.001001 D 35000.00 345000.001001 F 150000.00 345000.001002 A 40000.00 100000.001002 C 35000.00 100000.001002 D 25000.00 100000.001003 A 30000.00 165000.001003 B 65000.00 165000.001003 C 20000.00 165000.001003 D 50000.00 165000.00

storeid prodid sales Group Sum(sales)1001 A 100000.00 345000.001001 C 60000.00 345000.001001 D 35000.00 345000.001001 F 150000.00 345000.001002 A 40000.00 100000.001002 C 35000.00 100000.001002 D 25000.00 100000.001003 A 30000.00 165000.001003 B 65000.00 165000.001003 C 20000.00 165000.001003 D 50000.00 165000.00

Show sales details and sub-totals by store using the SUM Window facility.

Window Aggregate Functions Page 1-21

Page 34: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using WITH for Final Totals Using the WITH clause allows generation of final totals in addition to detail rows. The WITH clause generates a grand total of all rows in the sample. This result requires the generation of a separate line of output. The SUM Window function may also be used to generate grand totals, comparably to the WITH clause capability.

Window Aggregate Functions Page 1-22

Page 35: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using WITH for Final Totals

storeid prodid sales1001 A 100000.001001 C 60000.001001 D 35000.001001 F 150000.001002 A 40000.001002 C 35000.001002 D 25000.001003 A 30000.001003 B 65000.001003 C 20000.001003 D 50000.00

Sum(sales) 610000.00

storeid prodid sales1001 A 100000.001001 C 60000.001001 D 35000.001001 F 150000.001002 A 40000.001002 C 35000.001002 D 25000.001003 A 30000.001003 B 65000.001003 C 20000.001003 D 50000.00

Sum(sales) 610000.00

SELECT storeid, prodid, sales

FROM salestblWITH SUM(sales)ORDER BY 1,2;

Show store details and total sales across all stores using the WITH facility.

Note that the output from this query is a non-relational output.

The total row is of a different format than the data rows.

Note that the output from this query is a non-relational output.

The total row is of a different format than the data rows.

Window Aggregate Functions Page 1-23

Page 36: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using SUM Window Function for Final Totals Consider the same problem using the SUM Window function. Rather than generate a separate row for the total, it is included on each row of output. Absence of the PARTITION BY clause causes the SUM Window function to sum all rows. The following is an equivalent query to the query on the facing page. It returns the same results.

SELECT storeid , prodid , sales , SUM(sales) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM salestbl ORDER BY 1,2;

Window Aggregate Functions Page 1-24

Page 37: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using SUM Window Function for Final Totals

SELECT storeid, prodid, sales, SUM(sales) OVER( )

FROM salestblORDER BY 1,2;

SELECT storeid, prodid, sales

FROM salestblORDER BY 1,2;

Note that the generated output is relational in form.

All rows and columns are uniform.

, SUM(sales) OVER( )

Note that the generated output is relational in form.

All rows and columns are uniform.

Show store details and total sales across all stores:

storeid prodid sales Group Sum(sales)1001 A 100000.00 610000.001001 C 60000.00 610000.001001 D 35000.00 610000.001001 F 150000.00 610000.001002 A 40000.00 610000.001002 C 35000.00 610000.001002 D 25000.00 610000.001003 A 30000.00 610000.001003 B 65000.00 610000.001003 C 20000.00 610000.001003 D 50000.00 610000.00

storeid prodid sales Group Sum(sales)1001 A 100000.00 610000.001001 C 60000.00 610000.001001 D 35000.00 610000.001001 F 150000.00 610000.001002 A 40000.00 610000.001002 C 35000.00 610000.001002 D 25000.00 610000.001003 A 30000.00 610000.001003 B 65000.00 610000.001003 C 20000.00 610000.001003 D 50000.00 610000.00

Window Aggregate Functions Page 1-25

Page 38: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Cumulative Sums The cumulative sum function provides the ability to perform cumulative sums over a column based on a defined sequence. It computes a running or cumulative total of all column values. In the facing-page example, each row represents a single day of activity for item 10. (Note that not all days have sales of item 10.) The rightmost column shows the cumulative sales figures for this item over the two month period. Example: Produce a listing showing a cumulative sum of sales revenue by day over the first two months of 2004. Use the Teradata CSUM function to accomplish this. Solution and results are seen on the facing page. A cumulative sum by definition implies an ordering on a particular column or expression. The ORDER BY clause defines the sequencing for the cumulation. The ROWS UNBOUNDED PRECEDING clause indicates that all preceding rows in the listing are to be included in the cumulation. Teradata Extension Alternative An alternative Teradata ‘shortcut’ extension is the CSUM function which can produce the same results, however does not comply with ANSI standards. CSUM(colname, sort_item1, sort_item2,…) Results are sorted in ascending order based on the specified sort list columns. Descending sorts of any of the sort list columns may be specified by using the DESC qualifier. Example: CSUM(colname, sort_item1, sort_item2 DESC) The equivalent coding of the example on the facing page follows:

SELECT salesdate, sales, CSUM(sales, salesdate) As "Csum" FROM daily_sales_2004 WHERE salesdate BETWEEN DATE '2004-01-01' AND DATE '2004-03-01';

Window Aggregate Functions Page 1-26

Page 39: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE daily_sales (itemid INTEGER,salesdate DATE FORMAT 'YY/MM/DD‘, sales DECIMAL(9,2))

PRIMARY INDEX ( itemid );

Cumulative Sums

Show a cumulative sum of sales by day over the first two months of 2004.

SELECT salesdate, sales, SUM(sales) OVER (ORDER BY salesdate

ROWS UNBOUNDED PRECEDING) AS "Csum"

DATE '2004-01-01' AND DATE '2004-03-01'

FROM daily_sales_2004WHERE salesdate BETWEEN

;

salesdate sales Csum04/01/01 150.00 150.0004/01/02 200.00 350.0004/01/03 250.00 600.0004/01/05 350.00 950.0004/01/10 550.00 1500.0004/01/21 150.00 1650.0004/01/25 200.00 1850.0004/01/31 100.00 1950.0004/02/01 150.00 2100.0004/02/03 250.00 2350.0004/02/06 350.00 2700.0004/02/17 550.00 3250.0004/02/20 450.00 3700.0004/02/27 350.00 4050.00

salesdate sales Csum04/01/01 150.00 150.0004/01/02 200.00 350.0004/01/03 250.00 600.0004/01/05 350.00 950.0004/01/10 550.00 1500.0004/01/21 150.00 1650.0004/01/25 200.00 1850.0004/01/31 100.00 1950.0004/02/01 150.00 2100.0004/02/03 250.00 2350.0004/02/06 350.00 2700.0004/02/17 550.00 3250.0004/02/20 450.00 3700.0004/02/27 350.00 4050.00

This function computes a running or cumulative total of a column’s value.

The daily_sales_2004 table is used in many of the following examples.

Window Aggregate Functions Page 1-27

Page 40: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Cumulative Sums with Reset A cumulative sum may be reset to zero at specified breakpoints by using the PARTITION BY clause. Note that OLAP functions such as cumulative sums and standard aggregations (SUM, COUNT,AVG, MIN, MAX) are not compatible within the same query. The following is another possible solution:

SELECT salesdate ,sales ,EXTRACT(MONTH FROM salesdate) AS Mon ,CSUM(sales, salesdate) AS "Csum" FROM daily_sales_2004 WHERE itemid = 10

AND EXTRACT(YEAR FROM salesdate) = 2004 AND EXTRACT(MONTH FROM salesdate) in (1,2)

GROUP BY Mon;

Window Aggregate Functions Page 1-28

Page 41: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Cumulative Sums with Reset

SELECT salesdate, sales, EXTRACT(MONTH FROM salesdate) AS Mon,SUM(sales) OVER (PARTITION BY Mon

ORDER BY salesdateROWS UNBOUNDED PRECEDING) AS "Csum"

ResetCumulation

FROM daily_sales_2004 WHERE itemid = 10AND EXTRACT(YEAR FROM salesdate) = 2004AND Mon in (1,2);

Create a running daily sales total for item 10 for Jan and Feb 2004 and reset the cumulation for a change in month:

salesdate sales Mon Csum04/01/01 150.00 1 150.0004/01/02 200.00 1 350.0004/01/03 250.00 1 600.0004/01/05 350.00 1 950.0004/01/10 550.00 1 1500.0004/01/21 150.00 1 1650.0004/01/25 200.00 1 1850.0004/01/31 100.00 1 1950.0004/02/01 150.00 2 150.0004/02/03 250.00 2 400.0004/02/06 350.00 2 750.0004/02/17 550.00 2 1300.0004/02/20 450.00 2 1750.0004/02/27 350.00 2 2100.00

salesdate sales Mon Csum04/01/01 150.00 1 150.0004/01/02 200.00 1 350.0004/01/03 250.00 1 600.0004/01/05 350.00 1 950.0004/01/10 550.00 1 1500.0004/01/21 150.00 1 1650.0004/01/25 200.00 1 1850.0004/01/31 100.00 1 1950.0004/02/01 150.00 2 150.0004/02/03 250.00 2 400.0004/02/06 350.00 2 750.0004/02/17 550.00 2 1300.0004/02/20 450.00 2 1750.0004/02/27 350.00 2 2100.00

Window Aggregate Functions Page 1-29

Page 42: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving Sums The Moving Sum function permits calculation of a moving sum of a specified column, based on a defined ‘query width’ (n). The width determines how many preceding rows will be incorporated into the sum. If there are less than n preceding rows, only those which are available will be used. Teradata Extension AlternativeThe Teradata MSUM function may also be used to produce a moving sum. It is defined as follows: MSUM(colname, n, sort_item1, sort_item2, …) Where: colname = the column used to compute the moving sum n = the defined width of the query, i.e., how many preceding rows are used to compute sum sortlist = the list of columns which will determine the sort order of the result Example: Produce a listing showing a moving sum of sales revenue for the first two months of 1998. Use a width of 3. Use the Teradata MSUM function.

SELECT salesdate, itemid, sales , MSUM(sales, 3, salesdate) AS "Msum"

FROM daily_sales_2004 WHERE itemid = 10

AND EXTRACT(YEAR FROM salesdate) = 2004 AND EXTRACT(MONTH FROM salesdate) IN (1,2);

(Same result as facing-page query.) In the ANSI formation, the ROWS 2 PRECEDING clause means the SUM will include the current row and the two preceding. Note that this number is one less that the width specified in the Teradata CSUM function i.e., that the width of 3 implies that the MSUM function will include the current row and the two preceding rows.

Window Aggregate Functions Page 1-30

Page 43: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving Sums

SELECT salesdate , itemid , sales, SUM(sales) OVER (ORDER BY salesdate

ROWS 2 PRECEDING) AS "Msum"FROM daily_sales_2004WHERE itemid = 10AND EXTRACT(YEAR FROM salesdate) = 2004AND EXTRACT(MONTH FROM salesdate) IN (1,2);

Show a moving sum of sales revenue for the first two months of 2004:

salesdate itemid sales Msum04/01/01 10 150.00 150.0004/01/02 10 200.00 350.0004/01/03 10 250.00 600.0004/01/05 10 350.00 800.0004/01/10 10 550.00 1150.0004/01/21 10 150.00 1050.0004/01/25 10 200.00 900.0004/01/31 10 100.00 450.0004/02/01 10 150.00 450.0004/02/03 10 250.00 500.0004/02/06 10 350.00 750.0004/02/17 10 550.00 1150.0004/02/20 10 450.00 1350.0004/02/27 10 350.00 1350.00

salesdate itemid sales Msum04/01/01 10 150.00 150.0004/01/02 10 200.00 350.0004/01/03 10 250.00 600.0004/01/05 10 350.00 800.0004/01/10 10 550.00 1150.0004/01/21 10 150.00 1050.0004/01/25 10 200.00 900.0004/01/31 10 100.00 450.0004/02/01 10 150.00 450.0004/02/03 10 250.00 500.0004/02/06 10 350.00 750.0004/02/17 10 550.00 1150.0004/02/20 10 450.00 1350.0004/02/27 10 350.00 1350.00

Sum of 3 rows

Sum of 2 rows

Window Aggregate Functions Page 1-31

Page 44: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving Averages The Moving Average (MAVG) function permits the calculation of a moving average on a specified column, based on a defined number of rows known as the query width. The sortlist column specifies the column(s) for the ascending sort of the final result If the number of rows preceding the current row is less than the width, the average is computed based on the existing preceding rows Teradata Extension Alternative (Non-ANSI)The Teradata MSUM function may also be used to produce a moving average. It is defined as follows: MAVG(colname, n, sort_item1, sort_item2, …) Where: colname = the column used to compute the moving average n = the defined width of the query, I.e. how many preceding rows are used to compute avg sortlist = the list of columns which will determine the sort order of the result Example: Produce a listing showing a weekly moving average of sales revenue for the first two months of 1998. Use a width of 7. Use the Teradata MAVG function. SELECT salesdate, itemid, sales, MAVG(sales, 7, salesdate) AS "MAvg" FROM daily_sales_2004 WHERE itemid = 10

AND EXTRACT(YEAR FROM salesdate) = 2004 AND EXTRACT(MONTH FROM salesdate) IN (1,2);

(Same result as facing-page query.) In the ANSI formation, the ROWS 6 PRECEDING clause means the AVG will include the current row and the six preceding. Note that this number is one less that the width specified in the Teradata MAVG function.

Window Aggregate Functions Page 1-32

Page 45: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving Averages

Show a moving average on a 7 day window for sales of item 10:

• Uses current row and preceding 6 rows.

• Uses all preceding rows if less than 6.

• Sort ascending by ORDER BY column(s) is default.

• Uses current row and preceding 6 rows.

• Uses all preceding rows if less than 6.

• Sort ascending by ORDER BY column(s) is default.

salesdate itemid sales MAvg04/01/01 10 150.00 150.00 04/01/02 10 200.00 175.00 04/01/03 10 250.00 200.00 04/01/05 10 350.00 237.50 04/01/10 10 550.00 300.00 04/01/21 10 150.00 275.00 04/01/25 10 200.00 264.29 04/01/31 10 100.00 257.14 04/02/01 10 150.00 250.00 04/02/03 10 250.00 250.00 04/02/06 10 350.00 250.00 04/02/17 10 550.00 250.00 04/02/20 10 450.00 292.8604/02/27 10 350.00 314.29

salesdate itemid sales MAvg04/01/01 10 150.00 150.00 04/01/02 10 200.00 175.00 04/01/03 10 250.00 200.00 04/01/05 10 350.00 237.50 04/01/10 10 550.00 300.00 04/01/21 10 150.00 275.00 04/01/25 10 200.00 264.29 04/01/31 10 100.00 257.14 04/02/01 10 150.00 250.00 04/02/03 10 250.00 250.00 04/02/06 10 350.00 250.00 04/02/17 10 550.00 250.00 04/02/20 10 450.00 292.8604/02/27 10 350.00 314.29

Computes moving AVG of a column where n (width) <= 4096

Avg of 7 rows

Avg of 2 rows

SELECT salesdate, itemid, sales

FROM daily_sales_2004WHERE itemid = 10AND EXTRACT(YEAR FROM salesdate) = 2004AND EXTRACT(MONTH FROM salesdate) IN (1,2);

,AVG(sales) OVER (ORDER BY salesdateROWS 6 PRECEDING) AS "MAvg"

Window Aggregate Functions Page 1-33

Page 46: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving Differences The Moving Difference (MDIFF) function permits a calculation of a moving difference of a specified column, based on a defined query width (n). The width determines how many rows back to count for the subtrahend. If there are less than n preceding rows, a null will be generated to represent the difference. The Moving Difference function is defined as follows: MDIFF(colname, n, sort_item1, sort_item2, …) Where: colname = the column used to compute the moving difference n = the defined width of the query, I.e. how many rows back for the subtrahend sortlist = the list of columns which will determine the sort order of the result This is a Teradata extension and has no ANSI-equivalent function.

Window Aggregate Functions Page 1-34

Page 47: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving Differences

Show a moving difference for a 3 day width on sales of item 10 for the year 1998:

• Uses current row and preceding nth row.

• Result is null if there is no preceding nth row.

• Sort ascending by sortlist column(s) is default.

• There is no ANSI-equivalent function.

• Uses current row and preceding nth row.

• Result is null if there is no preceding nth row.

• Sort ascending by sortlist column(s) is default.

• There is no ANSI-equivalent function.

SELECT salesdate, itemid, sales, MDIFF(sales, 3, salesdate)FROM daily_sales_2004 WHERE itemid = 10AND EXTRACT(YEAR FROM salesdate) = 2004AND EXTRACT(MONTH FROM salesdate) IN (1,2);

Computes moving difference of a column where n (width) <= 4096.MDIFF(colname, n, sortlist)MDIFF(colname, n, sortlist)

salesdate itemid sales MDiff04/01/01 10 150.00 ? 04/01/02 10 200.00 ? 04/01/03 10 250.00 ? 04/01/05 10 350.00 200.00 04/01/10 10 550.00 350.00 04/01/21 10 150.00 -100.00 04/01/25 10 200.00 -150.00 04/01/31 10 100.00 -450.00 04/02/01 10 150.00 .00 04/02/03 10 250.00 50.00: : : :

salesdate itemid sales MDiff04/01/01 10 150.00 ? 04/01/02 10 200.00 ? 04/01/03 10 250.00 ? 04/01/05 10 350.00 200.00 04/01/10 10 550.00 350.00 04/01/21 10 150.00 -100.00 04/01/25 10 200.00 -150.00 04/01/31 10 100.00 -450.00 04/02/01 10 150.00 .00 04/02/03 10 250.00 50.00: : : :

Difference of 2 rows

Difference of 2 rows

Window Aggregate Functions Page 1-35

Page 48: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary The COUNT OVER function:

• Can produce a group count represented with each detail row. The SUM OVER function:

• Can produces a group sum represented with each detail row. • Can produce cumulative sums over a group. • Can produce moving sums over a group. • Can produce moving averages over a group

Both COUNT OVER and SUM OVER Functions:

• Are ANSI standard functions. • May not be combined in a SELECT with standard aggregate functions (i.e. SUM,

COUNT, AVG, MIN, MAX) • May not be combined in a SELECT with WITH and/or WITH...BY clauses

The MDIFF function:

• Can produce moving differences over a group. • Is a non-ANSI Teradata extension.

Window Aggregate Functions Page 1-36

Page 49: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary

The COUNT OVER function: • Can produce a group count represented with each detail row.

Both COUNT OVER and SUM OVER Functions:• Are called ‘windowed aggregate functions’.• Are ANSI standard functions.• May not be combined in a SELECT with standard aggregate

functions (i.e. SUM, COUNT, AVG, MIN, MAX)• May not be combined in a SELECT with WITH or WITH...BY clauses

The SUM OVER function: • Can produces a group sum represented with each detail row.• Can produce cumulative sums over a group.• Can produce moving sums over a group.• Can produce moving averages over a group

The MDIFF function: • Can produce moving differences over a group.• Is a non-ANSI Teradata extension.

Window Aggregate Functions Page 1-37

Page 50: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise For each function listed on the facing page, pick two appropriate coding choices, one each from List 1 and List 2.

Window Aggregate Functions Page 1-38

Page 51: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise

List 1 List 2

Cumulative Sum ________ ________

Moving Sum ________ ________

Moving Average ________ ________

Moving Difference ________ ________

Final Totals ________ ________

Sub Totals ________ ________

List 1

A.) UNBOUNDED PRECEDING

B.) UNBOUNDED FOLLOWING

C.) BOTH

D.) NEITHER

List 2

A.) PARTITION BY

B.) ORDER BY

C.) NEITHER

For each function listed below, pick two appropriate coding choices, one each from List 1 and List 2.

Window Aggregate Functions Page 1-39

Page 52: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)

Window Aggregate Functions Page 1-40

Page 53: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)

1. How many people in each department were born prior to 1950 and who are they? Show last name, first name (reduce both to 10 characters), birth date(as Birthdate), department number (as Dept), and the count of people in that department who qualify (as Dept_Cnt). Order the results by birth date within department.

2. The following SQL statement produces a report showing the employees in departments 401 and 403, the subcount of employees in each department, and the subtotals of their salaries by department. Modify this query to produce the same information using the SUM and COUNT Window functions.

SELECT last_name AS Name,salary_amount AS Salary,department_number AS Dept

FROM employeeWHERE Dept IN (401,403)WITH SUM (Salary) (TITLE 'Dept_Sum'), COUNT(Salary) (TITLE 'Dept_Count')BY Dept;

3. The solution for Lab #2 may be modified to generate this report. Produce a report which shows each employee in departments 401 and 403, their department, their salary, the total of salaries for their department, and the percentage their salary represents of the department total. Sort the output by salary within department. The report columns should be named as follows:

Employee last name (AS Name, truncated to CHAR(10))Employee Department (AS Dept)Employee Salary(AS Salary)Department Salary Sum (AS Dept_Sum)Percent of Dept Salaries (AS Dept_Percent)

Window Aggregate Functions Page 1-41

Page 54: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

Window Aggregate Functions Page 1-42

Page 55: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

SELECT salesdate,CAST(itemid AS BYTEINT) (FORMAT '99') AS "itemid",sales,CSUM(sales, salesdate) AS "CSUM",MSUM(sales, 3, salesdate) AS "MSUM",MDIFF(sales, 3, salesdate) AS "MDIFF",MAVG(sales, 3, salesdate) AS "MAVG"

FROM daily_salesWHERE salesdate BETWEEN 980101 AND 980228;

salesdate itemid sales CSUM MSUM MDIFF MAVG98/01/01 10 150.00 150.00 150.00 ? 150.0098/01/02 10 200.00 350.00 350.00 ? 175.0098/01/03 10 250.00 600.00 600.00 ? 200.0098/01/05 10 350.00 950.00 800.00 200.00 266.6798/01/10 10 550.00 1500.00 1150.00 350.00 383.3398/01/21 10 150.00 1650.00 1050.00 -100.00 350.0098/01/25 10 200.00 1850.00 900.00 -150.00 300.0098/01/31 10 100.00 1950.00 450.00 -450.00 150.0098/02/01 10 150.00 2100.00 450.00 .00 150.0098/02/03 10 250.00 2350.00 500.00 50.00 166.6798/02/06 10 350.00 2700.00 750.00 250.00 250.0098/02/17 10 550.00 3250.00 1150.00 400.00 383.3398/02/20 10 450.00 3700.00 1350.00 200.00 450.0098/02/27 10 350.00 4050.00 1350.00 .00 450.00

4.) The following query and output show sales revenue for item 10 for the first two months of 1998. It also shows a cumulative sum, a moving sum over three days, a moving difference over three days, and a moving average over three days. This output is generated using the Teradata CSUM, MSUM, MDIFF, and MAVG functions. Convert this query to ANSI standard syntax to produce the same result.

Window Aggregate Functions Page 1-43

Page 56: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Window Aggregate Functions Page 1-44

Page 57: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 2

After completing this module, you should be able to:

• Use the Windows Aggregate Functions in conjuction with:• Group Windows• Sum Windows• Cumulative Windows• Remaining Windows

Module 2Ordered Analytic Function Types

Ordered Analytic Function Types Page 2-1

Page 58: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ordered Analytic Function Types Page 2-2

Page 59: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Ordered Analytic Function Types Page 2-3

Page 60: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Window Aggregate Function Types Window aggregate functions are also called ordered analytic functions. They are given this name because the result set is dependent on the ‘ordering’ of the data. Each function has an ordered sequence, either by default or by specification. All Windows Aggregate functions fall into one of four types. These are:

• Group Window: Aggregates based on a grouping of rows. • Cumulative Window: Aggregates based on a cumulation of rows. • Moving Window: Aggregates based on a moving window of rows. • Remaining Window: Aggregates based on the rows remaining outside of a defined

window. Like traditional aggregate functions, window aggregate functions operate on groups of rows and permit qualification and filtering of the group result. Unlike aggregations, OLAP functions return individual detail rows of information, not just aggregations. The window feature is ANSI SQL-99 compliant and provides a way to dynamically define a subset of data, or window, in an ordered relational database table. A window is specified by the OVER() phrase, which can include the following clauses inside the parentheses:

• PARTITION BY • ORDER BY • ROWS

The window feature can be applied to the ANSI SQL-99-compliant window functions:

• AVG • COUNT • MAX • MIN • PERCENT_RANK • RANK • ROW_NUMBER • SUM

OLAP functions may be performed on all tables and views and may be used to populate tables in conjunction with INSERT SELECT.

Ordered Analytic Function Types Page 2-4

Page 61: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Window Aggregate Function Types

All Windows Aggregate functions fall into one of four types:• Group Window: Aggregates based on a grouping of rows.

• Cumulative Window: Aggregates based on a cummulation of rows.

• Moving Window: Aggregates based on a moving window of rows.

• Remaining Window: Aggregates based on the rows remaining outside of a defined window.

All Windows Aggregate functions fall into one of four types:• Group Window: Aggregates based on a grouping of rows.

• Cumulative Window: Aggregates based on a cummulation of rows.

• Moving Window: Aggregates based on a moving window of rows.

• Remaining Window: Aggregates based on the rows remaining outside of a defined window.

Each choice is determined by the ROWS clause defined in the query.

Each choice is used with one of the following aggregate functions:• AVG• COUNT• MAX• MIN• SUM• RANK• PERCENT_RANK• ROW_NUMBER

Ordered Analytic Function Types Page 2-5

Page 62: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Window Aggregate Functions Permutations Each of the four categories of Window functions can be applied to each of the supported Window aggregate functions, thus creating a variety of permutations.

Ordered Analytic Function Types Page 2-6

Page 63: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Window Aggregate Functions Permutations

4 CategoriesGroup WindowCumulative WindowMoving WindowRemaining Window

Group WindowCumulative WindowMoving WindowRemaining Window

SUM ( ) OVERCOUNT ( ) OVERAVG ( ) OVERMIN ( ) OVERMAX ( ) OVER

5 Aggregates

SUM ( ) OVERCOUNT ( ) OVERAVG ( ) OVERMIN ( ) OVERMAX ( ) OVER

Group SUM Window FunctionGroup COUNT Window FunctionGroup AVG Window FunctionGroup MIN Window FunctionGroup MAX Window Function

20 Functions=X

Group SUM Window FunctionGroup COUNT Window FunctionGroup AVG Window FunctionGroup MIN Window FunctionGroup MAX Window Function

Cumulative SUM Window FunctionCumulative COUNT Window FunctionCumulative AVG Window FunctionCumulative MIN Window FunctionCumulative MAX Window Function

Cumulative SUM Window FunctionCumulative COUNT Window FunctionCumulative AVG Window FunctionCumulative MIN Window FunctionCumulative MAX Window Function

Moving SUM Window FunctionMoving COUNT Window FunctionMoving AVG Window FunctionMoving MIN Window FunctionMoving MAX Window Function

Moving SUM Window FunctionMoving COUNT Window FunctionMoving AVG Window FunctionMoving MIN Window FunctionMoving MAX Window Function

Remaining SUM Window FunctionRemaining COUNT Window FunctionRemaining AVG Window FunctionRemaining MIN Window FunctionRemaining MAX Window Function

Remaining SUM Window FunctionRemaining COUNT Window FunctionRemaining AVG Window FunctionRemaining MIN Window FunctionRemaining MAX Window Function

ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING

Use of keywords: UNBOUNDED PRECEDING Absence of keywords: UNBOUNDED FOLLOWING

Absence of keyword: UNBOUNDEDUse of keywords: UNBOUNDED FOLLOWINGAbsence of keywords UNBOUNDED PRECEDING

Ordered Analytic Function Types Page 2-7

Page 64: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Group SUM Window Function The SUM Window Group function permits an aggregate to be computed across a defined group. The group may be defined using the PARTITION BY clause. The absence of this clause indicates that the group consists of all rows. Things to Notice:

• The window is defined as all rows - there is no PARTITION specified. • The final column represents the total of all rows. • The default title of the last column indicates that this is a Group function.

The same query could have been written as follows:

SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM salestbl;

Note that the key words ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING are the default for ordered analytical functions. Contents of Salestbl: storeid prodid sales 1001 A 100000.00 1001 C 60000.00 1001 D 35000.00 1001 F 150000.00 1002 A 40000.00 1002 C 35000.00 1002 D 25000.00 1003 A 30000.00 1003 B 65000.00 1003 C 20000.00 1003 D 50000.00

Ordered Analytic Function Types Page 2-8

Page 65: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Group SUM Window Function

• Permits an aggregate to be computed across a defined group. • The group may be defined using the PARTITION BY clause. • The absence of this clause indicates that group consists of all rows.

Show sales across all products in all stores and a grand total for the group.

SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESC)FROM salestbl;

storeid prodid sales Group Sum(sales)1001 F 150000.00 610000.001001 A 100000.00 610000.001003 B 65000.00 610000.001001 C 60000.00 610000.001003 D 50000.00 610000.001002 A 40000.00 610000.001001 D 35000.00 610000.001002 C 35000.00 610000.001003 A 30000.00 610000.001002 D 25000.00 610000.001003 C 20000.00 610000.00

storeid prodid sales Group Sum(sales)1001 F 150000.00 610000.001001 A 100000.00 610000.001003 B 65000.00 610000.001001 C 60000.00 610000.001003 D 50000.00 610000.001002 A 40000.00 610000.001001 D 35000.00 610000.001002 C 35000.00 610000.001003 A 30000.00 610000.001002 D 25000.00 610000.001003 C 20000.00 610000.00

Things to Notice:• The window is defined as all rows - no

PARTITION is specified.• The final column represents the total of

all rows.• The default title of the last column

indicates this is a Group function.

Things to Notice:• The window is defined as all rows - no

PARTITION is specified.• The final column represents the total of

all rows.• The default title of the last column

indicates this is a Group function.

Note that the key words ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWINGare the default for ordered analytical functions.

Ordered Analytic Function Types Page 2-9

Page 66: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

PARTITION with Group SUM Function The PARTITION BY clause permits the aggregation to occur within the defined grouping column. This effectively permits a ‘sub-total’ aggregation to occur within each group defined by the partition.

Ordered Analytic Function Types Page 2-10

Page 67: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

PARTITION with Group SUM Function

Note that the Group Sum reflects the total for each product.Note that the Group Sum reflects the total for each product.

Show the totals for each product across all stores.

SELECT storeid, prodid, sales, SUM(sales) OVER (PARTITION BY prodid

ORDER BY sales DESC) FROM salestbl ;

storeid prodid sales Group Sum(sales)1001 A 100000.00 170000.001002 A 40000.00 170000.001003 A 30000.00 170000.001003 B 65000.00 65000.001001 C 60000.00 115000.001002 C 35000.00 115000.001003 C 20000.00 115000.001003 D 50000.00 110000.001001 D 35000.00 110000.001002 D 25000.00 110000.001001 F 150000.00 150000.00

storeid prodid sales Group Sum(sales)1001 A 100000.00 170000.001002 A 40000.00 170000.001003 A 30000.00 170000.001003 B 65000.00 65000.001001 C 60000.00 115000.001002 C 35000.00 115000.001003 C 20000.00 115000.001003 D 50000.00 110000.001001 D 35000.00 110000.001002 D 25000.00 110000.001001 F 150000.00 150000.00

Ordered Analytic Function Types Page 2-11

Page 68: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Group SUM and QUALIFY Clause The QUALIFY clause operates on the output rows of a Windows Aggregate Function. It qualifies rows for output, much like the HAVING clause does for non-Window aggregates. The QUALIFY clause requires the existence of a Window Aggregate Function in one of the following:

• The SELECT clause • The GROUP BY clause • The QUALIFY clause itself

QUALIFY and HAVING operate on output rows of the function. WHERE, if present, operates on input rows to the function.

Ordered Analytic Function Types Page 2-12

Page 69: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Group SUM and QUALIFY Clause

The QUALIFY clause requires the existence of a Window Aggregate Function in one of the following:

• The SELECT clause• The GROUP BY clause• The QUALIFY clause itself

The QUALIFY clause requires the existence of a Window Aggregate Function in one of the following:

• The SELECT clause• The GROUP BY clause• The QUALIFY clause itself

Show product totals for all stores for products with at least $150,000 in total sales.

SELECT storeid, prodid, sales, SUM(sales) OVER (PARTITION BY prodid

ORDER BY sales DESC) AS ProdSumFROM salestblQUALIFY ProdSum > = 150000;

storeid prodid sales ProdSum1001 A 100000.00 170000.001002 A 40000.00 170000.001003 A 30000.00 170000.001001 F 150000.00 150000.00

storeid prodid sales ProdSum1001 A 100000.00 170000.001002 A 40000.00 170000.001003 A 30000.00 170000.001001 F 150000.00 150000.00

• The QUALIFY clause operates on the output rows of a Windows Aggregate Function.• It qualifies rows for output, much like HAVING does for non-Window aggregates.

Ordered Analytic Function Types Page 2-13

Page 70: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Group MINIMUM Window Function The MINIMUM Window function reports the minimum value for the defined group.

Ordered Analytic Function Types Page 2-14

Page 71: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Group MINIMUM Window Function

This function reports the minimum value for the defined group.

Show the sales figure for the poorest performing product in each store.

SELECT storeid, prodid, sales,MIN(sales) OVER (PARTITION BY storeid

ORDER BY sales DESC) FROM salestbl;

storeid prodid sales Group Min(sales)1001 F 150000.00 35000.001001 A 100000.00 35000.001001 C 60000.00 35000.001001 D 35000.00 35000.001002 A 40000.00 25000.001002 C 35000.00 25000.001002 D 25000.00 25000.001003 B 65000.00 20000.001003 D 50000.00 20000.001003 A 30000.00 20000.001003 C 20000.00 20000.00

storeid prodid sales Group Min(sales)1001 F 150000.00 35000.001001 A 100000.00 35000.001001 C 60000.00 35000.001001 D 35000.00 35000.001002 A 40000.00 25000.001002 C 35000.00 25000.001002 D 25000.00 25000.001003 B 65000.00 20000.001003 D 50000.00 20000.001003 A 30000.00 20000.001003 C 20000.00 20000.00

Ordered Analytic Function Types Page 2-15

Page 72: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Group MAXIMUM Window Function The MAXIMUM Window function reports the maximum value for the defined group.

Ordered Analytic Function Types Page 2-16

Page 73: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Group MAXIMUM Window Function

The MAXIMUM Window function reports the maximum value for the defined group.

Show the sales figure for the best performing product in each store.

SELECT storeid, prodid, sales,MAX(sales) OVER (PARTITION BY storeid

ORDER BY sales DESC) FROM salestbl;

storeid prodid sales Group Max(sales)1001 F 150000.00 150000.001001 A 100000.00 150000.00 1001 C 60000.00 150000.00 1001 D 35000.00 150000.00 1002 A 40000.00 40000.00 1002 C 35000.00 40000.001002 D 25000.00 40000.001003 B 65000.00 65000.001003 D 50000.00 65000.001003 A 30000.00 65000.001003 C 20000.00 65000.00

storeid prodid sales Group Max(sales)1001 F 150000.00 150000.001001 A 100000.00 150000.00 1001 C 60000.00 150000.00 1001 D 35000.00 150000.00 1002 A 40000.00 40000.00 1002 C 35000.00 40000.001002 D 25000.00 40000.001003 B 65000.00 65000.001003 D 50000.00 65000.001003 A 30000.00 65000.001003 C 20000.00 65000.00

Ordered Analytic Function Types Page 2-17

Page 74: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Cumulative SUM Window Function The SUM Window function permits a cumulative aggregate to be computed across a defined window. The aggregate may be cumulative with preceding rows in the window. The cumulative function is activated by the following two conditions:

• Use of the keywords UNBOUNDED PRECEDING. • Absence of the keywords UNBOUNDED FOLLOWING.

Ordered Analytic Function Types Page 2-18

Page 75: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Cumulative SUM Window Function

This function lets a cumulative aggregate be computed across a defined window. The aggregate is cumulative with preceding rows in the window.

The cumulative function is activated by the following two conditions:• Use of the keywords UNBOUNDED PRECEDING.• Absence of the keywords UNBOUNDED FOLLOWING.

Show sales across all products in all stores and a grand total for the group.

SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESC ROWS UNBOUNDED PRECEDING) FROM salestbl ;

storeid prodid sales Cumulative Sum(sales)1001 F 150000.00 150000.001001 A 100000.00 250000.001003 B 65000.00 315000.001001 C 60000.00 375000.001003 D 50000.00 425000.001002 A 40000.00 465000.001001 D 35000.00 500000.001002 C 35000.00 535000.001003 A 30000.00 565000.001002 D 25000.00 590000.001003 C 20000.00 610000.00

storeid prodid sales Cumulative Sum(sales)1001 F 150000.00 150000.001001 A 100000.00 250000.001003 B 65000.00 315000.001001 C 60000.00 375000.001003 D 50000.00 425000.001002 A 40000.00 465000.001001 D 35000.00 500000.001002 C 35000.00 535000.001003 A 30000.00 565000.001002 D 25000.00 590000.001003 C 20000.00 610000.00

The Cululative Sum reflects the sequential aggregation of all rows.

The default title of last column indicates this is a Cumulative function.

The Cululative Sum reflects the sequential aggregation of all rows.

The default title of last column indicates this is a Cumulative function.

Ordered Analytic Function Types Page 2-19

Page 76: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Cumulative MINIMUM Window Function The MINIMUM function is applied to the cumulative window. Each row shows the minimum value at that point in the cumulating.

Ordered Analytic Function Types Page 2-20

Page 77: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Cumulative MINIMUM Window Function

Show sales of all products in all stores in descending order with a cumulative minimum for sales in each store.

SELECT storeid, prodid, sales,MIN(sales)

ROWS UNBOUNDED PRECEDINGOVER (PARTITION BY storeid ORDER BY

sales DESC ) FROM salestbl;

Things to notice:• Each row for a store represents

the miminum sales until a lesser value is encountered.

• Since the rows are in descending sequence, the current row is always the minimum.

• This can be reversed by changing the sequence, as seen in the next example.

Things to notice:• Each row for a store represents

the miminum sales until a lesser value is encountered.

• Since the rows are in descending sequence, the current row is always the minimum.

• This can be reversed by changing the sequence, as seen in the next example.

storeid prodid sales Cumulative Min(sales)1001 F 150000.00 150000.001001 A 100000.00 100000.001001 C 60000.00 60000.001001 D 35000.00 35000.001002 A 40000.00 40000.001002 C 35000.00 35000.001002 D 25000.00 25000.001003 B 65000.00 65000.001003 D 50000.00 50000.001003 A 30000.00 30000.001003 C 20000.00 20000.00

Ordered Analytic Function Types Page 2-21

Page 78: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Cumulative Window MIN and Max Window Function The examples seen here show the use of the MIN function with the Cumulative Window using an ascending sequence, and the use of the MAX function, also with the Cumulative Window.

Ordered Analytic Function Types Page 2-22

Page 79: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Cumulative MIN and MAX Window Functions

SELECT storeid, prodid, sales,MIN(sales) OV

ASC

ER (PARTITION BY storeidORDER BY sales

ROWS UNBOUNDED PRECEDING) FROM salestbl;

SELECT storeid, prodid, sales,OVER (PARTITION BY storeid

ORDER BY sales ROWS UNBOUNDED PRECEDING)

FROM salestbl;

SELECT storeid, prodid, sales,OVER (PARTITION BY storeid

ORDER BY sales ROWS UNBOUNDED PRECEDING)

FROM salestbl;

MIN(sales)

ASC

MAX(sales)

DESC

SELECT storeid, prodid, sales,OVER (PARTITION BY storeid

ORDER BY sales ROWS UNBOUNDED PRECEDING)

FROM salestbl;

Show sales for the best performing product in each store.

MAX(sales)

DESC

storeid prodid sales Cumulative Max(sales)1001 F 150000.00 150000.001001 A 100000.00 150000.001001 C 60000.00 150000.001001 D 35000.00 150000.001002 A 40000.00 40000.001002 C 35000.00 40000.001002 D 25000.00 40000.001003 B 65000.00 65000.001003 D 50000.00 65000.001003 A 30000.00 65000.001003 C 20000.00 65000.00

storeid prodid sales Cumulative Min(sales)1001 D 35000.00 35000.001001 C 60000.00 35000.001001 A 100000.00 35000.001001 F 150000.00 35000.001002 D 25000.00 25000.001002 C 35000.00 25000.001002 A 40000.00 25000.001003 C 20000.00 20000.001003 A 30000.00 20000.001003 D 50000.00 20000.001003 B 65000.00 20000.00

storeid prodid sales Cumulative Min(sales)1001 D 35000.00 35000.001001 C 60000.00 35000.001001 A 100000.00 35000.001001 F 150000.00 35000.001002 D 25000.00 25000.001002 C 35000.00 25000.001002 A 40000.00 25000.001003 C 20000.00 20000.001003 A 30000.00 20000.001003 D 50000.00 20000.001003 B 65000.00 20000.00

Ordered Analytic Function Types Page 2-23

Page 80: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving SUM Window Function The SUM Window function permits a computed aggregate to move across a defined window. A moving function requires a fixed interval of rows less than the total number of rows. The moving function is activated by the following condition: Absence of the keyword UNBOUNDED. Things to Notice

• Each row computes a moving sum based on itself and the two preceding rows. • The first and second rows compute their sums based on one and two rows respectively. • The default title of the last column indicates that this is a Moving function.

Alternate Syntax

SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESC ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) FROM salestbl;

Ordered Analytic Function Types Page 2-24

Page 81: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving SUM Window Function

This function lets a computed aggregate move across a defined window. A moving function requires a fixed interval of rows less than the total number of rows

The moving function is activated by the absence of the keyword UNBOUNDED.

The moving function is activated by the

SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESC ROWS 2 PRECEDING) FROM salestbl;

Show sales of all products in all stores in descending order with a moving sum using the two preceding rows.

absence of the keyword UNBOUNDED.

• Each row computes a moving sum based on itself and 2 preceding rows.

• The 1st and 2nd rows compute their sums based on one and two rows respectively.

• The default title of the last column indicates this is a Moving function.

storeid prodid sales Moving Sum(sales)1001 F 150000.00 150000.001001 A 100000.00 250000.001003 B 65000.00 315000.001001 C 60000.00 225000.001003 D 50000.00 175000.001002 A 40000.00 150000.001001 D 35000.00 125000.001002 C 35000.00 110000.001003 A 30000.00 100000.001002 D 25000.00 90000.001003 C 20000.00 75000.00

storeid prodid sales Moving Sum(sales)1001 F 150000.00 150000.001001 A 100000.00 250000.001003 B 65000.00 315000.001001 C 60000.00 225000.001003 D 50000.00 175000.001002 A 40000.00 150000.001001 D 35000.00 125000.001002 C 35000.00 110000.001003 A 30000.00 100000.001002 D 25000.00 90000.001003 C 20000.00 75000.00

Ordered Analytic Function Types Page 2-25

Page 82: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving SUM Window Function with PARTITION BY Each row computes a moving sum based on itself and the two preceding rows. The moving sum is reset when a new product is encountered.

Ordered Analytic Function Types Page 2-26

Page 83: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving SUM Window Function with PARTITION BY

Show sales of all products in all stores in descending order with a moving sum using the two preceding rows but partitioning by product.

SELECT storeid, prodid, sales, SUM(sales) OVER (PARTITION BY prodid

ORDER BY sales DESC ROWS 2 PRECEDING) FROM salestbl;

storeid prodid sales Moving Sum(sales)1001 A 100000.00 100000.001002 A 40000.00 140000.001003 A 30000.00 170000.001003 B 65000.00 65000.001001 C 60000.00 60000.001002 C 35000.00 95000.001003 C 20000.00 115000.001003 D 50000.00 50000.001001 D 35000.00 85000.001002 D 25000.00 110000.001001 F 150000.00 150000.00

storeid prodid sales Moving Sum(sales)1001 A 100000.00 100000.001002 A 40000.00 140000.001003 A 30000.00 170000.001003 B 65000.00 65000.001001 C 60000.00 60000.001002 C 35000.00 95000.001003 C 20000.00 115000.001003 D 50000.00 50000.001001 D 35000.00 85000.001002 D 25000.00 110000.001001 F 150000.00 150000.00

• Each row computes a moving sum based on itself and the two preceding rows.

• The moving sum is reset when a new product is encountered.

• Each row computes a moving sum based on itself and the two preceding rows.

• The moving sum is reset when a new product is encountered.

Reset

Ordered Analytic Function Types Page 2-27

Page 84: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving SUM Window Function with ORDER BY Adding an ORDER BY clause to an ordered function such as a moving window, renders the report difficult if not impossible to read. The moving sum amounts have not changed, but their positioning in the report has. It is hard to know what rows are participating in the moving average.

Ordered Analytic Function Types Page 2-28

Page 85: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving SUM Window Function with ORDER BY

Adding an ORDER BY clause to an ordered function such as a moving window, renders the report difficult if not impossible to read.

SELECT storeid, prodid, sales, SUM(sales) OVER (PARTITION BY prodid

ORDER BY sales DESC ROWS 2 PRECEDING) FROM salestblORDER BY storeid, sales;

storeid prodid sales Moving Sum(sales)1001 D 35000.00 85000.001001 C 60000.00 60000.001001 A 100000.00 100000.001001 F 150000.00 150000.001002 D 25000.00 110000.001002 C 35000.00 95000.001002 A 40000.00 140000.001003 C 20000.00 115000.001003 A 30000.00 170000.001003 D 50000.00 50000.001003 B 65000.00 65000.00

storeid prodid sales Moving Sum(sales)1001 D 35000.00 85000.001001 C 60000.00 60000.001001 A 100000.00 100000.001001 F 150000.00 150000.001002 D 25000.00 110000.001002 C 35000.00 95000.001002 A 40000.00 140000.001003 C 20000.00 115000.001003 A 30000.00 170000.001003 D 50000.00 50000.001003 B 65000.00 65000.00

• The moving sum amounts have not changed, but their positioning in the report has.

• It is hard to know what rows are participating in the moving average.

• The moving sum amounts have not changed, but their positioning in the report has.

• It is hard to know what rows are participating in the moving average.

Ordered Analytic Function Types Page 2-29

Page 86: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving MINIMUM Window Function The MINIMUM Window function reports the minimum value for the defined group. Each row computes a moving minimum based on itself and the two preceding rows. The first and second rows compute their minimums based on one and two rows respectively. The moving minimum is reset when a new product is encountered.

Ordered Analytic Function Types Page 2-30

Page 87: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving MINIMUM Window Function

SELECT storeid, prodid, sales,MIN(sales) OVER (PARTITION BY prodid ORDER BY sales DESC ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)FROM salestbl;

This function reports the minimum value for the defined group.

Show sales of all products in all stores in descending order with a moving minimum using the two preceding rows but partitioning by product.

storeid prodid sales Moving Min(sales)1001 A 100000.00 100000.001002 A 40000.00 40000.001003 A 30000.00 30000.001003 B 65000.00 65000.001001 C 60000.00 60000.001002 C 35000.00 35000.001003 C 20000.00 20000.001003 D 50000.00 50000.001001 D 35000.00 35000.001002 D 25000.00 25000.001001 F 150000.00 150000.00

storeid prodid sales Moving Min(sales)1001 A 100000.00 100000.001002 A 40000.00 40000.001003 A 30000.00 30000.001003 B 65000.00 65000.001001 C 60000.00 60000.001002 C 35000.00 35000.001003 C 20000.00 20000.001003 D 50000.00 50000.001001 D 35000.00 35000.001002 D 25000.00 25000.001001 F 150000.00 150000.00

• Each row computes a moving minimum based on itself and the two preceding rows.

• The first and second rows compute their minimums based on one and two rows respectively.

• The moving minimum is reset when a new product is encountered.

• Each row computes a moving minimum based on itself and the two preceding rows.

• The first and second rows compute their minimums based on one and two rows respectively.

• The moving minimum is reset when a new product is encountered.

Reset

Ordered Analytic Function Types Page 2-31

Page 88: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving MAXIMUM Window Function The MAXIMUM Window function reports the maximum value for the defined group.

Ordered Analytic Function Types Page 2-32

Page 89: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Moving MAXIMUM Window Function

SELECT storeid, prodid, sales,MAX(sales) OVER (ORDER BY sales DESC

ROWS 2 PRECEDING)FROM salestbl ;

Show sales of all products in all stores in descending sales order with a moving maximum using the two preceding rows.

storeid prodid sales Moving Max(sales)1001 F 150000.00 150000.001001 A 100000.00 150000.001003 B 65000.00 150000.001001 C 60000.00 100000.001003 D 50000.00 65000.001002 A 40000.00 60000.001001 D 35000.00 50000.001002 C 35000.00 40000.001003 A 30000.00 35000.001002 D 25000.00 35000.001003 C 20000.00 30000.00

storeid prodid sales Moving Max(sales)1001 F 150000.00 150000.001001 A 100000.00 150000.001003 B 65000.00 150000.001001 C 60000.00 100000.001003 D 50000.00 65000.001002 A 40000.00 60000.001001 D 35000.00 50000.001002 C 35000.00 40000.001003 A 30000.00 35000.001002 D 25000.00 35000.001003 C 20000.00 30000.00

Ordered Analytic Function Types Page 2-33

Page 90: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Remaining SUM Window Function (1of 2) The SUM Window function permits a computed aggregate based on the remaining rows in a defined window. Remaining rows are defined as relative to the current row. The remaining function is activated by the following two conditions:

• Use of the keywords UNBOUNDED FOLLOWING. • Absence of the keywords UNBOUNDED PRECEDING.

Ordered Analytic Function Types Page 2-34

Page 91: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Remaining SUM Window Function (1 of 2)• Permits computed aggregates based on the remaining rows in a defined window. • Remaining rows are defined as relative to the current row.• The remaining function is activated by the following two conditions:

• Use of the keywords UNBOUNDED FOLLOWING.

• Absence of the keywords UNBOUNDED PRECEDING.

• Use of the keywords UNBOUNDED FOLLOWING.

• Absence of the keywords UNBOUNDED PRECEDING.

SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESCROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)FROM salestbl;

Show sales of all products in all stores in ascending sequence. Also show the sales sum of all products below the current row in the order.

storeid prodid sales Remaining Sum(sales)1003 C 20000.00 20000.001002 D 25000.00 45000.001003 A 30000.00 75000.001001 D 35000.00 110000.001002 C 35000.00 145000.001002 A 40000.00 185000.001003 D 50000.00 235000.001001 C 60000.00 295000.001003 B 65000.00 360000.001001 A 100000.00 460000.001001 F 150000.00 610000.00

storeid prodid sales Remaining Sum(sales)1003 C 20000.00 20000.001002 D 25000.00 45000.001003 A 30000.00 75000.001001 D 35000.00 110000.001002 C 35000.00 145000.001002 A 40000.00 185000.001003 D 50000.00 235000.001001 C 60000.00 295000.001003 B 65000.00 360000.001001 A 100000.00 460000.001001 F 150000.00 610000.00

Ordered Analytic Function Types Page 2-35

Page 92: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Remaining SUM Window Function (2of 2) The SUM Window function permits a computed aggregate based on the remaining rows in a defined window. Remaining rows are defined as relative to the current row. The remaining function is activated by the following two conditions:

• Use of the keywords UNBOUNDED FOLLOWING. • Absence of the keywords UNBOUNDED PRECEDING.

Things to Notice

• Each row computes a remaining sum based on itself and the following rows. • 'Following rows' means rows with a sales amount less than the current row. • It does not necessarily refer to the rows 'below' it on the list. • The first row on the list only includes itself - there are no 'following' rows. • The final row has all rows 'following' it, thus it is the total of all rows. • The sort sequence of the sales column is shown as ascending even though DESC is

specified. • 'Remaining' is the only one of the Windows functions to behave this way. • The default title of the last column indicates that this is a Remaining function.

Ordered Analytic Function Types Page 2-36

Page 93: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Remaining SUM Window Function (2 of 2)

storeid prodid sales Remaining Sum(sales)1003 C 20000.00 20000.001002 D 25000.00 45000.001003 A 30000.00 75000.001001 D 35000.00 110000.001002 C 35000.00 145000.001002 A 40000.00 185000.001003 D 50000.00 235000.001001 C 60000.00 295000.001003 B 65000.00 360000.001001 A 100000.00 460000.001001 F 150000.00 610000.00

storeid prodid sales Remaining Sum(sales)1003 C 20000.00 20000.001002 D 25000.00 45000.001003 A 30000.00 75000.001001 D 35000.00 110000.001002 C 35000.00 145000.001002 A 40000.00 185000.001003 D 50000.00 235000.001001 C 60000.00 295000.001003 B 65000.00 360000.001001 A 100000.00 460000.001001 F 150000.00 610000.00

Things to notice:• Each row computes a remaining sum based on itself and the following rows. • 'Following rows' means rows with a sales amount less than the current row. • It does not necessarily refer to the rows 'below' it on the list. • The first row on the list only includes itself - there are no 'following' rows. • The final row has all rows 'following' it, thus it is the total of all rows. • The sort sequence of the sales column is shown as ascending even though

DESC is specified. • 'Remaining' is the only one of the Windows functions to behave this way. • The default title of the last column indicates that this is a Remaining function.

Things to notice:• Each row computes a remaining sum based on itself and the following rows. • 'Following rows' means rows with a sales amount less than the current row. • It does not necessarily refer to the rows 'below' it on the list. • The first row on the list only includes itself - there are no 'following' rows. • The final row has all rows 'following' it, thus it is the total of all rows. • The sort sequence of the sales column is shown as ascending even though

DESC is specified. • 'Remaining' is the only one of the Windows functions to behave this way. • The default title of the last column indicates that this is a Remaining function.

SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESCROWS BETWEEN CURRENT ROWAND UNBOUNDED FOLLOWING)FROM salestbl;

SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESCROWS BETWEEN CURRENT ROWAND UNBOUNDED FOLLOWING)FROM salestbl;

(Repeated from previous page for convenience)

Ordered Analytic Function Types Page 2-37

Page 94: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Remaining MINIMUM Window Function The MINIMUM Window function reports the minimum value for the defined group.

Ordered Analytic Function Types Page 2-38

Page 95: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Remaining MINIMUM Window Function

Show sales of all products in all stores partitioned by store with a remaining minimum using the current and following rows.

SELECT storeid, prodid, sales,MIN(sales) OVER (PARTITION BY storeid

ORDER BY sales DESC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)

FROM salestbl;

storeid prodid sales Remaining Min(sales)1001 D 35000.00 35000.001001 C 60000.00 35000.001001 A 100000.00 35000.001001 F 150000.00 35000.001002 D 25000.00 25000.001002 C 35000.00 25000.001002 A 40000.00 25000.001003 C 20000.00 20000.001003 A 30000.00 20000.001003 D 50000.00 20000.001003 B 65000.00 20000.00

storeid prodid sales Remaining Min(sales)1001 D 35000.00 35000.001001 C 60000.00 35000.001001 A 100000.00 35000.001001 F 150000.00 35000.001002 D 25000.00 25000.001002 C 35000.00 25000.001002 A 40000.00 25000.001003 C 20000.00 20000.001003 A 30000.00 20000.001003 D 50000.00 20000.001003 B 65000.00 20000.00

Ordered Analytic Function Types Page 2-39

Page 96: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Remaining MAXIMUM Window Function The MAXIMUM Window function reports the maximum value for the defined group.

Ordered Analytic Function Types Page 2-40

Page 97: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Remaining MAXIMUM Window Function

This function reports the maximum value for the defined group.

Show sales of all products in all stores partitioned by store with a remaining maximum using the current and following rows.

SELECT storeid, prodid, sales,MAX(sales) OVER (PARTITION BY storeid

ORDER BY sales DESC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)FROM salestbl;

storeid prodid sales Remaining Max(sales)1001 D 35000.00 35000.001001 C 60000.00 60000.001001 A 100000.00 100000.001001 F 150000.00 150000.001002 D 25000.00 25000.001002 C 35000.00 35000.001002 A 40000.00 40000.001003 C 20000.00 20000.001003 A 30000.00 30000.001003 D 50000.00 50000.001003 B 65000.00 65000.00

storeid prodid sales Remaining Max(sales)1001 D 35000.00 35000.001001 C 60000.00 60000.001001 A 100000.00 100000.001001 F 150000.00 150000.001002 D 25000.00 25000.001002 C 35000.00 35000.001002 A 40000.00 40000.001003 C 20000.00 20000.001003 A 30000.00 30000.001003 D 50000.00 50000.001003 B 65000.00 65000.00

Ordered Analytic Function Types Page 2-41

Page 98: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary SUM Window Group Function

• Defines a group for which an aggregation is to be produced. • A group which is a subset of the answer set is defined by the PARTITION BY clause. • Identified by use of keywords ROWS BETWEEN UNBOUNDED PRECEDING AND

UNBOUNDED FOLLOWING. • Since this is the default, it need not be specified.

SUM Window Cumulative Function

• Permits a cumulative aggregate to be computed across a defined window. • Identified by use of keywords UNBOUNDED PRECEDING and absence of keywords

UNBOUNDED FOLLOWING. SUM Window Moving Function

• Permits a computed aggregate to move across a defined window. • Always uses a subset of the defined rows in the answer set. • Identified by absence of keywords UNBOUNDED.

SUM Window Remaining Function

• Permits a computed aggregate based on the remaining rows in a defined window. • Identified by use of keywords UNBOUNDED FOLLOWING and absence of keywords

UNBOUNDED PRECEDING.

Ordered Analytic Function Types Page 2-42

Page 99: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary

Window Group Function•Defines a group for which an aggregation is to be produced. •A group which is a subset of the answer set is defined by the PARTITION BY clause. • Identified by use of keywords ROWS BETWEEN UNBOUNDED PRECEDING AND

UNBOUNDED FOLLOWING. •Since this is the default, it need not be specified.

Window Cumulative Function•Permits a cumulative aggregate to be computed across a defined window. • Identified by use of keywords UNBOUNDED PRECEDING and absence of keywords

UNBOUNDED FOLLOWING.

Window Moving Function•Permits a computed aggregate to move across a defined window. •Always uses a subset of the defined rows in the answer set. • Identified by absence of keywords UNBOUNDED.

Window Remaining Function•Permits a computed aggregate based on the remaining rows in a defined window. • Identified by use of keywords UNBOUNDED FOLLOWING and absence of keywords

UNBOUNDED PRECEDING.

Ordered Analytic Function Types Page 2-43

Page 100: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise

Ordered Analytic Function Types Page 2-44

Page 101: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise

Present Present Absent

Cumulative Window ________ ________ ________

Moving Window ________ ________ ________

Group Window ________ ________ ________

Remaining Window ________ ________ ________

ChoicesUP - UNBOUNDED PRECEDING

UF - UNBOUNDED FOLLOWING

UB - UNBOUNDED

For each type of function listed below, identify which coding choices, by their absence or presence, will activate the function.

Ordered Analytic Function Types Page 2-45

Page 102: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

Ordered Analytic Function Types Page 2-46

Page 103: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

1. Show the salary amounts for each employee in departments 401 and 402 in ascending order and show the group sum for each department.

2. Modify the previous query to also show the maximum salary amount in each department.

3. Modify the previous query to show the salary amounts for each employee in departments 401 and 402 and show the cumulative sum of the salary amounts by department.

4. Modify the previous query to show the salaries of all employees in departments 401 and 402 in descending order with a moving average by department using the two preceding rows.

5. Modify the previous query to show the salaries of all employees in departments 401 and 402 in ascending sequence. Also show the sum of the remaining salaries of all employees below the currentrow in the hierarchy.

Ordered Analytic Function Types Page 2-47

Page 104: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Ordered Analytic Function Types Page 2-48

Page 105: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 3

Module 3Enhanced Numeric Formatting Options

After completing this module, you should be able to:

• Apply appropriate enhanced formatting options to numeric data types.

Enhanced Numeric Formatting Options Page 3-1

Page 106: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Enhanced Numeric Formatting Options Page 3-2

Page 107: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Enhanced Numeric Formatting Options Page 3-3

Page 108: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Standard Column Formats All columns have a default format defined by the column data type. The default format for 'salary_amount' (a DECIMAL(10,2) data type) may be easily seen by submitting the following query:

SELECT DISTINCT(FORMAT(salary_amount)) FROM employee; Format(salary_amount) ------------------------------ -----------------.99

This result shows that the default format for a DECIMAL data type consists of a 'sign-suppressed' whole number to the left of the decimal point, and two numeric characters to the right, representing the fractional portion. The following query uses this default format.

SELECT salary_amount FROM employee WHERE employee_number = 801; salary_amount ------------------- 100000.00

One can override the default format by specifying an explicit FORMAT clause.

SELECT salary_amount (FORMAT 'Z99,999.99') FROM employee WHERE employee_number = 801; salary_amount -------------------- 100,000.00

Default formats may also be specified as part of a CREATE TABLE statement.

Enhanced Numeric Formatting Options Page 3-4

Page 109: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Standard Column Formats

All columns have a default format defined by the column data type.

Show the default format for 'salary_amount': (a DECIMAL(10,2) data type)

SELECT DISTINCT(FORMAT(salary_amount)) FROM employee;Format(salary_amount)-----------------------------------------------.99

Format(salary_amount)-----------------------------------------------.99

The following query uses this default format.

SELECT salary_amount FROM employeeWHERE employee_number = 801;

salary_amount----------------------

100000.00

salary_amount----------------------

100000.00

Override the default format by specifying an explicit FORMAT clause in the SELECT or in a CREATE TABLE.

SELECT salary_amount (FORMAT 'Z99,999.99') FROM employeeWHERE employee_number = 801;

salary_amount----------------------

100,000.00

Enhanced Numeric Formatting Options Page 3-5

Page 110: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Radix and Group Separators Additional conventions are available which permit the format to be specified using user-defined formatting characters. They are defined in a file called the SDF file (Specification for Data Formatting). This file is maintained using the tdlocaldef utility. Radix and Group Separators The following query uses SDF conventions to define a format for the output. These conventions must be defined in the SDF file using the tdlocaldef utility. SELECT salary_amount (FORMAT 'G-(10)D9(2)') FROM employee WHERE employee_number = 801;salary_amount ------------- 100,000.00 G - Represents the GROUP SEPARATOR as defined in the SDF. In U.S. applications, this is usually a comma (,) character. It is used to separate digits of the whole number into groups of three or less. It must always appear as the first character in a FORMAT string. D - Represents the RADIX symbol as defined in the SDF. Radix separates the whole number from the fractional part. In U.S. applications, this is usually a period (.). Note: SDF symbols may not be mixed in the same FORMAT string with explicit FORMAT designations. For example, you cannot have 'D' and an explicit '.' in the same FORMAT.

Enhanced Numeric Formatting Options Page 3-6

Page 111: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Radix and Group Separators

G:

D:

G D

Represents the GROUP SEPARATOR as defined in the SDF. It is used to separate digits of the whole number into groups of three or less. It must always appear as the first character in a FORMAT string. In U.S. applications, this is usually a comma (,) character.

Represents the RADIX symbol. Radix separates the whole number from the fractional part. In U.S. applications, this is usually a period (.).

SELECT salary_amount (FORMAT ' -(10) 9(2)')FROM employeeWHERE employee_number = 801;

salary_amount---------------------

100,000.00

salary_amount---------------------

100,000.00

Produce salary amount using group and radix format options to produce the equivalent of the format: ‘---,---.99‘

Note: SDF symbols may not be mixed in the same FORMATstring with explicit FORMAT designations. For example, you cannot have 'D' and an explicit '.' in the same FORMAT.

Note: SDF symbols may not be mixed in the same FORMATstring with explicit FORMAT designations. For example, you cannot have 'D' and an explicit '.' in the same FORMAT.

Enhanced Numeric Formatting Options Page 3-7

Page 112: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Integers, Fractions and Zero-Suppression The same output may be achieved using the following formatting example:

SELECT salary_amount (FORMAT 'G-Z(I)D9(F)') FROM employee WHERE employee_number = 801; salary_amount ------------- 100,000.00

• F: Represents the number of characters needed to display the fraction portion of numeric

data. In the case of a DEC(10,2) data type, 9(F) is equivalent to 9(2). • I: Represents the number of characters needed to display the INTEGER portion of

numeric data. In the case of a DEC(10,2) data type, I is equivalent to 9(8). • Z: Represents zero-suppressed decimal digit. Z(I) represents a zero-suppressed integer.

Floating zero-suppression may also be specified using multiple Z's (ZZZZZZ).

Enhanced Numeric Formatting Options Page 3-8

Page 113: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Integers, Fractions and Zero-Suppression

F:

I:

Z:

Represents the number of characters needed to display the fraction portion of numeric data. In the case of a DEC(10,2) data type, 9(F) is equivalent to 9(2).

Represents the number of characters needed to display the INTEGER portion of numeric data. In the case of a DEC(10,2) data type, I is equivalent to 9(8).

Represents zero-suppressed decimal digit. Z(I) represents a zero-suppressed integer. Floating zero-suppression may also be specified using multiple Z's (ZZZZZZ).

The same output as the previous example results from this formatting example.

salary_amount---------------------

100,000.00

salary_amount---------------------

100,000.00

SELECT salary_amount (FORMAT ' - ( )D9(F)')FROM employeeWHERE employee_number = 801;

SELECT salary_amount (FORMAT ' -(10) 9(2)')FROM employeeWHERE employee_number = 801;

G Z I

G DSELECT salary_amount (FORMAT ' -(10) 9(2)')FROM employeeWHERE employee_number = 801;

Previous Example

G D

Enhanced Numeric Formatting Options Page 3-9

Page 114: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Fixed Currency Options The following examples demonstrate the ability to customize formats to accommodate a variety of international conventions for representing currency amounts. Fixed Currency

SELECT salary_amount (FORMAT 'GL9(I)D9(F)') FROM employee WHERE employee_number = 801; salary_amount --------------------- $00,100,000.00

L: Represents the CURRENCY SYMBOL as defined in the SDF. In U.S. applications, this is usually the dollar ($) symbol. A single 'L' indicates a fixed (i.e. non-floating) currency symbol.

SELECT salary_amount (FORMAT 'GZ(I)D9(F)L') FROM employee WHERE employee_number = 801; salary_amount -------------------- 100,000.00$

Note: The currency symbol may appear at either the beginning or the end of the FORMAT string.

Enhanced Numeric Formatting Options Page 3-10

Page 115: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

salary_amount----------------------$00,100,000.00

salary_amount----------------------00,100,000.00

salary_amount---------------------

100,000.00

$

$

salary_amount---------------------

100,000.00

Note: FORMAT The currency symbol may appear at either the beginning or the end of the string.

$

Fixed Currency Options

Represents the CURRENCY SYMBOL. A single 'L' indicates a fixed (i.e. non-floating) currency symbol. In U.S. applications, this is usually the dollar ($) symbol.

The following examples demonstrate the ability to customize formats to accomodatea variety of international conventions for representing currency amounts.

SELECT salary_amount (FORMAT 'G 9(I)D9(F)')FROM employeeWHERE employee_number = 801;

SELECT salary_amount (FORMAT 'GZ(I)D9(F) ')FROM employeeWHERE employee_number = 801;

L:

L

L

Note: FORMAT The currency symbol may appear at either the beginning or the end of the string.

Enhanced Numeric Formatting Options Page 3-11

Page 116: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Dual Currency Options Dual Currency Symbol A close relative to the 'L' symbol is the letter 'O' symbol.

SELECT salary_amount (FORMAT 'O-Z(I)') FROM employee WHERE employee_number = 801; salary_amount ------------- $ 100000 SELECT salary_amount (FORMAT '-Z(I)BO') FROM employee WHERE employee_number = 801; salary_amount ------------- 100000 $

O: Represents the DUAL CURRENCY symbol. For U.S. applications, this is usually the dollar symbol '$'. For countries like France which use dual currencies, this might be the Euro symbol, while the standard CURRENCY SYMBOL might be the Franc symbol. This gives applications the ability to select either currency's symbol. Note: In both examples, the symbol is in a 'fixed' position - i.e. non-floating.

Enhanced Numeric Formatting Options Page 3-12

Page 117: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

salary_amount---------------------

$ 100000

salary_amount---------------------

100000

salary_amount---------------------

100000

$

$

salary_amount---------------------

100000

Note that in both examples, the symbol is in a 'fixed' position - i.e. non-floating.

$

Dual Currency Options

The 'L' symbol is closely related to the ' ymbol.

Represents the DUAL CURRENCY symbol. For U.S. applications, this is usually the dollar symbol '$'.

For countries like France which use dual currencies, this might be the Euro symbol, while the standard CURRENCY SYMBOL might be the Franc symbol. This gives applications the ability to select either currency's symbol.

SELECT salary_amount (FORMAT ' -Z(I)')FROM employeeWHERE employee_number = 801;

SELECT salary_amount (FORMAT '-Z(I)B ')FROM employeeWHERE employee_number = 801;

O' s

O:

O

O

Note that in both examples, the symbol is in a 'fixed' position - i.e. non-floating.

Enhanced Numeric Formatting Options Page 3-13

Page 118: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Floating Currency

SELECT salary_amount (FORMAT 'GLLZ(I)D9(F)') FROM employee WHERE employee_number = 801; salary_amount -------------------- $100,000.00

• LL: Represents a FLOATING CURRENCY SYMBOL as does any number of multiple

L's strung together. • OO: Represents similarly, a DUAL FLOATING CURRENCY SYMBOL.

Enhanced Numeric Formatting Options Page 3-14

Page 119: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

salary_amount--------------------

$100,000.00

salary_amount--------------------

$100,000.00

salary_amount--------------------

$100,000.00

salary_amount--------------------

$100,000.00

Floating Currency

LL:

OO:

LL

OOOO

Represents a FLOATING CURRENCY SYMBOL as does any number of multiple L's strung together.

Represents similarly, a DUAL FLOATING CURRENCY SYMBOL.

SELECT salary_amount (FORMAT 'G Z(I)D9(F)')FROM employeeWHERE employee_number = 801;

SELECT salary_amount (FORMAT 'G Z(I)D9(F)')FROM employeeWHERE employee_number = 801;

Enhanced Numeric Formatting Options Page 3-15

Page 120: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ISO Currency Symbols The ISO Currency Symbol is represented by an abbreviation of the currency name.

SELECT salary_amount (FORMAT '-Z(I)BC') FROM employee WHERE employee_number = 801; salary_amount ------------- 100000 USD

C: Represents the ISO currency symbol. For U.S. applications this is usually the 'USD' designation.

SELECT salary_amount (FORMAT 'C-Z(I)B') FROM employee WHERE employee_number = 801 salary_amount ------------- USD 100000

Note: The ISO currency symbol may be placed at either the beginning or the end of the FORMAT string and represents a fixed position.. Dual ISO currency symbol A close relative to the 'C' symbol is the 'U' symbol.

SELECT salary_amount (FORMAT '-Z(I)BU') FROM employee WHERE employee_number = 801; salary_amount ------------- 100000 USD

U: Represents the Dual ISO currency symbol. This is useful for countries who wish to represent more than one type of currency in their financial reporting.

Enhanced Numeric Formatting Options Page 3-16

Page 121: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

salary_amount---------------------

100000 USD

salary_amount---------------------

100000

salary_amount--------------------

100000

USD

USD

salary_amount--------------------

100000

Both ISO currency symbols may be placed at either the beginning or the end of the FORMAT string and represent a fixed position in the format.

USD

ISO Currency Symbols

Represents the ISO currency symbol. The ISO Currency Symbol represents an abbreviation of the currency name. For U.S. applications this is usually the 'USD' designation.

Represents the Dual ISO currency symbol. This is useful for countries who wish to represent more than one type of currency in their financial reporting.

SELECT salary_amount (FORMAT '-Z(I)B ')FROM employeeWHERE employee_number = 801;

SELECT salary_amount (FORMAT ' -Z(I)B')FROM employeeWHERE employee_number = 801

C:

U:

C

C

Both ISO currency symbols may be placed at either the beginning or the end of the FORMAT string and represent a fixed position in the format.

Enhanced Numeric Formatting Options Page 3-17

Page 122: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Currency Names

SELECT salary_amount (FORMAT '-Z(I)BN') FROM employee WHERE employee_number = 801; salary_amount -------------------------- 100000 US Dollars

N: Represents FULL CURRENCY NAME. For U.S. applications, this is usually U.S. Dollars. B: Represents a space Dual Currency Names

SELECT salary_amount (FORMAT '-Z(I)BA') FROM employee WHERE employee_number = 801; salary_amount -------------------------- 100000 US Dollars

A: Represents FULL DUAL CURRENCY NAME. For countries like the U.S. this may be the same as the FULL CURRENCY NAME. For countries like France, this might be the Euro, while FULL CURRENCY NAME might be the Franc. This gives applications the ability to select either currency.

SELECT salary_amount (FORMAT 'G-Z(I)BN') FROM employee WHERE employee_number = 801; salary_amount -------------------------- 100,000 US Dollars

It is however, allowed with the CURRENCY SYMBOL as seen here.

SELECT salary_amount (FORMAT 'GL-Z(I)D9(F)') FROM employee WHERE employee_number = 801; salary_amount --------------- $ 100,000.00

Enhanced Numeric Formatting Options Page 3-18

Page 123: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Currency NamesN:A:B:

US Dollars

Represents FULL CURRENCY NAME. For U.S. applications, this is usually U.S. Dollars.Represents FULL DUAL CURRENCY NAMERepresents a space

salary_amount---------------------------------

100000

SELECT salary_amount (FORMAT '-Z(I)B ')FROM employeeWHERE employee_number = 801;

N salary_amount---------------------------------

100000 US Dollars

SELECT salary_amount (FORMAT '-Z(I)B ')FROM employeeWHERE employee_number = 801;

A

It can also be used with the Group Separator as seen here.

salary_amount-----------------------------

100,000 US Dollars

salary_amount-----------------------------

100,000 US Dollars

SELECT salary_amount (FORMAT ' -Z(I)B ')FROM employeeWHERE employee_number = 801;

salary_amount---------------------------------

100000

G A

US Dollars

salary_amount---------------------------------

100000 US Dollars

Enhanced Numeric Formatting Options Page 3-19

Page 124: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Signed Amounts Fixed Sign Negative Amounts Signs may be represented either in fixed or floating formats.

SELECT salary_amount * -1 (FORMAT '-Z(I)BN') FROM employee WHERE employee_number = 801; (salary_amount*-1) ------------------------- - 100000 US Dollars

Note: The sign is in a fixed position. Floating Sign Negative Amounts

SELECT salary_amount * -1 (FORMAT '--Z(I)BN') FROM employee WHERE employee_number = 801; (salary_amount*-1) -------------------------- -100000 US Dollars

Note: The sign floats to the nearest digit when two or more leading signs (-) are specified.

Enhanced Numeric Formatting Options Page 3-20

Page 125: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

(salary_amount*-1)--------------------------------- 100000 US Dollars

(salary_amount*-1)--------------------------------

100000 US Dollars

Note: The sign is in a fixed position.

-

Note: The sign is in a fixed position.

(salary_amount*-1)--------------------------

100000 US Dollars-

(salary_amount*-1)--------------------------

100000 US Dollars

Note: The sign floats to the nearest digit if two or more leading signs (-) are specified.

Signed Amounts

Fixed Sign Negative AmountsSigns may be represented either in fixed or floating formats.

SELECT salary_amount * -1 (FORMAT ' Z(I)BN')FROM employeeWHERE employee_number = 801;

Floating Sign Negative Amounts

-

SELECT salary_amount * -1 (FORMAT ' Z(I)BN')FROM employeeWHERE employee_number = 801;

--

-

Note: The sign floats to the nearest digit if two or more leading signs (-) are specified.

Enhanced Numeric Formatting Options Page 3-21

Page 126: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary The following is a summary of SDF Numeric Formatting Options.

• G: Represents the GROUP SEPARATOR as defined in the SDF. In U.S. applications, this is usually a comma (,) character. It is used to separate digits of the whole number into groups of three or less. It always appears as the first character in a FORMAT string.

• D: Represents the RADIX symbol as defined in the SDF. Radix separates the whole number from the fractional part. In U.S. applications, this is usually a period (.).

• F: Represents the number of characters needed to display the fraction portion of numeric data. In the case of a DEC(10,2) data type, 9(F) is equivalent to 9(2).

• I: Represents the number of characters needed to display the INTEGER portion of numeric data. In the case of a DEC(10,2) data type, I is equivalent to 9(8).

• Z: Represents zero-suppressed decimal digit. Z(I) represents a zero-suppressed integer. Floating zero-suppression may also be specified using multiple Z's (ZZZZZZ).

• L: Represents the CURRENCY SYMBOL as defined in the SDF. In U.S. applications, this is usually the dollar ($) symbol. A single 'L' indicates a fixed (i.e. non-floating) currency symbol.

• - Represents the DUAL CURRENCY symbol. This option gives applications the ability to select multiple currency symbols.

• N: Represents FULL CURRENCY NAME. For U.S. applications, this is usually 'U.S. Dollars'.

• A: Represents FULL DUAL CURRENCY NAME. This option gives applications the ability to select multiple currency names.

• C: Represents the ISO currency symbol. For U.S. applications this is usually the 'USD' designation.

• U: Represents the Dual ISO currency symbol. This is useful for countries who wish to represent more than one type of currency in their financial reporting.

Enhanced Numeric Formatting Options Page 3-22

Page 127: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

SummaryThe following is a summary of SDF Numeric Formatting Options.• G: Represents the GROUP SEPARATOR as defined in the SDF. It is used to separate

digits of the whole number into groups of three or less. • D: Represents the RADIX symbol as defined in the SDF. Radix separates the whole

number from the fractional part. • F: Represents the number of characters needed to display the fraction portion of

numeric data. • I: Represents the number of characters needed to display the INTEGER portion of

numeric data. • Z: Represents zero-suppressed decimal digit. Z(I) represents a zero-suppressed

integer. Floating zero-suppression may also be specified using multiple Z's.• L: Represents the CURRENCY SYMBOL as defined in the SDF. A single 'L' indicates

a fixed (i.e. non-floating) currency symbol. • O: Represents the DUAL CURRENCY symbol. This option gives applications the

ability to select multiple currency symbols.• N: Represents FULL CURRENCY NAME. • A: Represents FULL DUAL CURRENCY NAME. This option gives applications the

ability to select multiple currency names.• C: Represents the ISO currency symbol. • U: Represents the Dual ISO currency symbol.

Enhanced Numeric Formatting Options Page 3-23

Page 128: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise For each function listed on the facing page, pick two appropriate coding choices, one each from List 1 and List 2.

Enhanced Numeric Formatting Options Page 3-24

Page 129: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise

Match the appropriate FORMAT character with its definition.

___ GROUP SEPARATOR

___ RADIX

___ Fractional portion of numeric data

___ INTEGER portion of numeric data

___ Zero-suppressed decimal digit

___ Fixed currency symbol

___ DUAL CURRENCY symbol

___ FULL CURRENCY NAME

___ FULL DUAL CURRENCY NAME

___ ISO currency symbol

___ Dual ISO currency symbol

A

C

D

F

G

I

L

O

N

U

Z

Enhanced Numeric Formatting Options Page 3-25

Page 130: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab The facing page describes the tasks for your lab exercise.

Enhanced Numeric Formatting Options Page 3-26

Page 131: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

Apply the appropriate format to the budget_amount column of the SQL below to produce the formatting results specified.

SELECT budget_amountFROM departmentWHERE department_number = 401;

Note: in some cases, there may be more than one possible format choice to produce the desired result.

a.) $ 982,300.00

b.) $982,300.00

c.) 982,300.00 $

d.) 00,982,300.00 US Dollars

e.) USD 982,300.00

f.) 982300 USD

We recommend that these labs be done using BTEQ or BTEQWin. If an ODBC client such as SQL Assistant is used, the projected column must be CAST to

CHARACTER with the correct precision, which may require some trial and error.

Enhanced Numeric Formatting Options Page 3-27

Page 132: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Enhanced Numeric Formatting Options Page 3-28

Page 133: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 4

Module 4ANSI Time and Dates

After completing this module, you should be able to:

• Perform queries using ANSI compliant Time and Date representations.

• Use Interval data types for date/time arithmetic.

• Extract appropriate date/time based information.

• Create date/time based literals.

ANSI Time and Dates Page 4-1

Page 134: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ANSI Time and Dates Page 4-2

Page 135: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

ANSI Time and Dates Page 4-3

Page 136: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Default Date Formatting Traditionally, the Teradata standard date format has been “YY/MM/DD”. The ANSI standard suggests “YYYY-MM-DD” as a more appropriate standard. Teradata makes this a selectable option with the SET SESSION DATEFORM option. This option governs not only how dates will be displayed on reports but also the importing and exporting of date character strings. It does not change the way Teradata stores date data types which are still stored as integers of the form “yyymmdd' “where “yyy” represents an offset of number of years after the year 1900. This SET option is an SQL command and can be executed at any time during a session. It is not to be confused with the BTEQ .SET command.

ANSI Time and Dates Page 4-4

Page 137: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Default Date Formatting

SET SESSION DATEFORM = ANSIDATE; /* ANSI Default format */SET SESSION DATEFORM = ANSIDATE; /* ANSI Default format */

Current Date 2004-01-20SELECT CURRENT_DATE; SELECT DATE; Date

2004-01-20

SET SESSION DATEFORM = INTEGERDATE; /* Teradata default */SET SESSION DATEFORM = INTEGERDATE; /* Teradata default */

Current Date 04/01/20SELECT CURRENT_DATE; SELECT DATE; Date

04/01/20

• Controls default display of selected dates.• Controls import and export of dates as character strings.• Is switchable during session.

SELECT DATE; /* Traditional Teradata current date selection */ Date 04/01/20

SELECT CURRENT_DATE; /* ANSI standard current date selection */ Date04/01/20

ANSI Time and Dates Page 4-5

Page 138: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Setting Date Defaults The DATEFORM option optionally may be set in any of three places. It should be set in the DBS Control record which will become the system default. It also can be set as part of defining a user, which will result in this being the default logon setting. And finally, it may also be set during the session using the SET DATEFORM option seen on the previous slide. An imported character string representing a date will be assumed to have the default DATEFORM setting, unless otherwise indicated. ODBC considerations are covered in the Timestamp module.

ANSI Time and Dates Page 4-6

Page 139: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Setting Date Defaults

MODIFY GENERAL 14 = 0 /* INTEGERDATE (YY/MM/DD) */MODIFY GENERAL 14 = 1 /* ANSIDATE (YYYY-MM-DD) */MODIFY GENERAL 14 = 0 /* INTEGERDATE (YY/MM/DD) */MODIFY GENERAL 14 = 1 /* ANSIDATE (YYYY-MM-DD) */

System Level(DBS Control record)

CREATE USER ped ……..DATEFORM = ANSIDATE

= INTEGERDATE

CREATE USER ped ……..DATEFORM = ANSIDATE

= INTEGERDATEUSER level

SET SESSION DATEFORM = ANSIDATE= INTEGERDATE

SET SESSION DATEFORM = ANSIDATE= INTEGERDATESESSION level

CREATE TABLE tbla(tbldate DATE);

SET SESSION DATEFORM = ANSIDATE;

USING newdate (CHAR(10))INSERT INTO tbla ( :newdate);

Importing an ANSI date to a Teradata date column:

Imports a character string of 'yyyy-mm-dd' and converts to a DATE data type.

Imports a character string of 'yyyy-mm-dd' and converts to a DATE data type.

Result:

ANSI Time and Dates Page 4-7

Page 140: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Interval Data Types

Interval Description Data Type INTERVAL YEAR Number of years SMALLINT INTERVAL MONTH Number of months SMALLINT INTERVAL DAY Number of days SMALLINT INTERVAL YEAR TO MONTH

Number of years and months

SMALLINT SMALLINT

INTERVAL HOUR Number of hours SMALLINT INTERVAL MINUTE Number of minutes SMALLINT INTERVAL SECOND Number of seconds DECIMAL(10,6) INTERVAL DAY TO HOUR

Number of days and hours

SMALLINT SMALLINT

INTERVAL DAY TO MINUTE

Number of days, hours and minutes

SMALLINT SMALLINT SMALLINT

INTERVAL DAY TO SECOND

Number of days, hours, minutes and seconds

SMALLINT SMALLINT SMALLINT DECIMAL(8,6)

INTERVAL HOUR TO MINUTE

Number of hours and minutes

SMALLINT SMALLINT

INTERVAL HOUR TO SECOND

Number of hours, minutes and seconds

SMALLINT SMALLINT DECIMAL(8,6)

INTERVAL MINUTE TO SECOND

Number of minutes and seconds

SMALLINT DECIMAL(8,6)

ANSI Time and Dates Page 4-8

Page 141: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Interval Data Types

• Interval data types represent a displacement between two points in time.• Intervals are stored as one or more numeric fields combined into a single data type.

Interval Description Data TypeINTERVAL YEAR Number of years SMALLINTINTERVAL MONTH Number of months SMALLINTINTERVAL DAY Number of days SMALLINTINTERVAL YEAR TO MONTH Number of years and months SMALLINTINTERVAL HOUR Number of hours SMALLINTINTERVAL MINUTE Number of minutes SMALLINTINTERVAL SECOND Number of seconds DECIMAL(10,6)INTERVAL DAY TO HOURS Number of days and hours SMALLINT(x2)INTERVAL DAY TO MINUTE Number of days, hours and minutes SMALLINT(x3)INTERVAL DAY TO SECOND Number of days, hours,

minutes and secondsSMALLINT(x3)DECIMAL(8,6)

INTERVAL HOUR TO MINUTE Number of hours and minutes SMALLINT(x2)INTERVAL HOUR TO SECOND Number of hours,

minutes and secondsSMALLINT(x2)DECIMAL(8,6)

INTERVAL MINUTE TO SECOND Number of minutes and seconds SMALLINTDECIMAL(8,6)

ANSI Time and Dates Page 4-9

Page 142: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Year/Month and Day Intervals Interval data types represent a displacement between two points in time. As seen in the above list, they are available in many different formats. Generally, intervals fall into two categories, year-month intervals and day-time intervals. As we will see in this module, all year-month intervals may be combined with a date to produce a new date. The only Day-Time interval that may be combined with a date is the DAY interval. Intervals may be represented as literals in SQL scripts. Exact syntax rules must be enforced to insure that the character string conforms to the properties of the specific literal. Intervals may be converted from one type to another using the CAST feature of SQL, however conversions are only possible within a general category. A YEAR interval may be converted to months, but may not be converted to days or hours.

ANSI Time and Dates Page 4-10

Page 143: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Year/Month and Day IntervalsYear-Month Intervals:INTERVAL YEAR(n) -9999 to +9999 years (n=1-4)INTERVAL MONTH(n) -9999 to +9999 monthsINTERVAL YEAR(n) TO MONTH

-9999 to 9999 years and 0 -12 months

Examples of Year-Month Interval Literals:INTERVAL -'32' YEAR (-32 years)INTERVAL '9' MONTH (+ 9 months)INTERVAL '5-08' YEAR TO MONTH

(+5 years, 8 months)

• Sign is outside of quotestring.• Plus (+) is default, represented by a blank.• Explicit + generates an error.• Default value for n is n = 2.• Precision (n) is not specified with literals.

• Sign is outside of quotestring.• Plus (+) is default, represented by a blank.• Explicit + generates an error.• Default value for n is n = 2.• Precision (n) is not specified with literals.

Example of Day Interval Literal:INTERVAL -'365' DAY (-365 days)

Day Interval:INTERVAL DAY(n) -9999 to +9999 days, (n=1-4)

YEAR YEAR TO MONTHMONTHDAYDAY TO HOURDAY TO MINUTEDAY TO SECONDHOURHOUR TO MINUTEHOUR TO SECONDMINUTEMINUTE TO SECONDSECOND

Year/MonthIntervals

Day/TimeIntervals

ANSI Time and Dates Page 4-11

Page 144: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Interval Literals in Date Arithmetic Date data types may be combined with certain literal types to form a different date. Intervals may be combined with certain literal types to form a different literal. Intervals may also have mathematical operations performed on them such as division and multiplication. This will also yield a new interval. Dates may be subtracted from other dates to yield some interval of time between them. Dates may be combined only with the above specific interval types.

ANSI Time and Dates Page 4-12

Page 145: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Interval Literals in Date Arithmetic

SET SESSION DATEFORM = ANSIDATE;

Date + Interval = DateInterval + Interval = IntervalDate - Date = IntervalInterval * / n = Interval

Date + Interval = DateInterval + Interval = IntervalDate - Date = IntervalInterval * / n = Interval

DATE YEAR YEAR TO MONTHMONTHDAY

SELECT CURRENT_DATE; SELECT CURRENT_DATE;

SELECT CURRENT_DATE - INTERVAL '2' DAY;SELECT CURRENT_DATE - INTERVAL '2' DAY;Show the date 2 days ago:

Show today's date:

SELECT CURRENT_DATE + INTERVAL '3' YEAR;SELECT CURRENT_DATE + INTERVAL '3' YEAR;

SELECT CURRENT_DATE + INTERVAL -'3' MONTH;SELECT CURRENT_DATE + INTERVAL -'3' MONTH;

Show the date in 3 years:

Show the date 3 months ago:

SELECT (INTERVAL '5-10' YEAR TO MONTH) + (INTERVAL '2-03' YEAR TO MONTH);

SELECT (INTERVAL '5-10' YEAR TO MONTH) + (INTERVAL '2-03' YEAR TO MONTH);

Add 5 years and 10 months to 2 years and 3 months:

2004-01-202004-01-20

2004-01-182004-01-18

2007-01-202007-01-20

2003-09-202003-09-20

8-018-01

ANSI Time and Dates Page 4-13

Page 146: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Date Literals Date literals always must be represented using the ANSI standard date format, regardless of the DATEFORM being used by the session. Date literals, as well as date data types, may be combined with interval data types. When adding a one month literal to a date, the same day of the new month will be the reported result. Not all days exist in all months. Note: The operation will fail if the same day does not exist in the new month. The appropriate mechanism for handling this situation is the ADD_MONTHS function. This function allows any day of any month to be mapped to a day of the new month. The ADD_MONTH function takes into account that the month of February has no day 30, 31 or even 29 in some years. It will map any of these numbers to the last day of the month of February, whatever it is in a given year. The ADD_MONTH function should be used when each day must have a mapping in the new month.

ANSI Time and Dates Page 4-14

Page 147: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Date Literals

SELECT (DATE '2003-12-29' + INTERVAL '2' MONTH);SELECT (DATE '2003-12-29' + INTERVAL '2' MONTH);

Date literals are always represented by the format: DATE 'YYYY-MM-DD'

SELECT (DATE '2004-12-29' + INTERVAL '2' MONTH); SELECT (DATE '2004-12-29' + INTERVAL '2' MONTH);

SELECT ADD_MONTHS(DATE '2003-12-31',2);SELECT ADD_MONTHS(DATE '2003-12-31',2);

SELECT DATE '2004-01-01' + INTERVAL '2-03' YEAR TO MONTH;

SELECT DATE '2004-01-01' + INTERVAL '2-03' YEAR TO MONTH;

SELECT ADD_MONTHS(DATE '2004-12-29',2)SELECT ADD_MONTHS(DATE '2004-12-29',2)

Add 2 years and 3 months to January 1, 2004:

Add 2 months to December 29, 2003:

Add 2 months to December 29, 2004:

Add 2 months to December 29, 2004:

Add 2 months to December 31, 2003:

2004-02-29 2004-02-29

*** Failure 2665 Invalid date. *** Failure 2665 Invalid date.

2004-02-29 2004-02-29

2006-04-01 2006-04-01

2005-02-28 2005-02-28

ANSI Time and Dates Page 4-15

Page 148: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Date Subtraction Yields Days

YEAR YEAR TO MONTHMONTH DAY INTEGER

DATE - DATE =

Subtraction operations may be performed on two dates yielding either an interval data type or, if none is specified, an integer. Integers, when used in conjunction with date operations, are always treated as day units. When the result of date subtraction yields an interval data type, the amount of precision required by the result must be specified in the interval representation. Thus, if the difference between two dates yields 100 days, the day interval must be specified as DAY(3). Because the maximum number of days supported in a DAY interval is 9999, a solution might be to always use DAY(4) when doing date subtraction. The same is true for months and years as well. The maximum number of digits in all three cases is four. Failure to properly specify an adequate number of digits for a result will produce an error.

ANSI Time and Dates Page 4-16

Page 149: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Date Subtraction Yields Days

Date - Date = Interval or IntegerDate - Date = Interval or Integer

How many days are between Jan. 1 and Mar 1 of 1999? Result is an INTERVAL DAY(2) data type.

May be used in INTERVAL calculation.Result is an INTERVAL DAY(2) data type.May be used in INTERVAL calculation.

SELECT (DATE '1999-03-01' - DATE '1999-01-01') DAY;SELECT (DATE '1999-03-01' - DATE '1999-01-01') DAY; 59 59

How many days are between Jan. 1, 1996 and Mar 1,1999?

SELECT DATE '1999-03-01' - DATE '1996-01-01';SELECT DATE '1999-03-01' - DATE '1996-01-01';

Result is an INTEGER representing # days.Result is an INTEGER representing # days.

1155 1155

How many days are between Jan. 1, 1996 and Mar 1,1999?

SELECT (DATE '1999-03-01' - DATE '1996-01-01') DAY;SELECT (DATE '1999-03-01' - DATE '1996-01-01') DAY;

*** Failure 7453 Interval Field Overflow. *** Failure 7453 Interval Field Overflow.

How many days are between Jan. 1, 1996 and Mar 1,1999?

SELECT (DATE '1999-03-01' - DATE '1996-01-01') DAY(4);SELECT (DATE '1999-03-01' - DATE '1996-01-01') DAY(4);

Result is an INTERVAL DAY(4) data type.Result is an INTERVAL DAY(4) data type.

1155 1155

ANSI Time and Dates Page 4-17

Page 150: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Date Subtraction Yields Years and Months As seen here, date subtraction also may yield years and months. Only whole years and whole months will be in the produced result. As before, the interval defined to hold the result must be defined large enough to hold the result; otherwise an error results. In the additional examples supplied on this page, one can see the hazard of using the CAST feature to produce the desired interval result. While CAST may seem to be an appropriate mechanism to control the interval output, note that the casting is performed after the subtraction has taken place. A number of days represented as an integer is calculated first, and then the integer number is converted to an interval number. Clearly, this does not produce the intended result. The following table is used for the examples on the facing page: CREATE TABLE dates date1 DATE ,date2 DATE ,interval1 YEAR(3) TO MONTH ,interval2 DAY(4) ,interval3 MONTH(4);

Erroneous result is an integer in days cast as months.

SELECT CAST ((DATE '1999-03-01' - DATE '1999-01-01') AS INTERVAL MONTH); (1999-03-01-1999-01-01) ---------------------------------- 59

SELECT CAST ((60 - 1) AS INTERVAL (60-1) --------- 59

SELECT (DATE '1999-03-01' - DATE '1999-01-01') (1999-03-01 - 1999-01-01) MONTH ------------------------------------------------ 2

Similar operation, same

Correct result is an interval of

Additional 'Cautionary'

ANSI Time and Dates Page 4-18

Page 151: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Date Subtraction Yields Years and Months

SELECT * FROM dates;SELECT * FROM dates;

date1 date22004-01-01 1899-01-01date1 date2

2004-01-01 1899-01-01

CREATE TABLE dates(date1 DATE , date2 DATE, interval1 YEAR(3) TO MONTH,interval2 DAY(4), interval3 MONTH(4));

CREATE TABLE dates(date1 DATE , date2 DATE, interval1 YEAR(3) TO MONTH,interval2 DAY(4), interval3 MONTH(4));

How many years/months are between Jan 1,1899 and Jan 1,2004?

SELECT date1 - date2 YEAR TO MONTH FROM dates;SELECT date1 - date2 YEAR TO MONTH FROM dates;*** Failure 7453 Interval Field Overflow.*** Failure 7453 Interval Field Overflow.

How many year/months are between Jan 1,1899 and Jan 1,2004?

SELECT date1 - date2 YEAR(3) TO MONTH FROM dates;

SELECT date1 - date2 YEAR(3) TO MONTH FROM dates; 105-00105-00

How many months are between Jan 1,1899 and Jan 1,2004?

SELECT date1 - date2 MONTH FROM dates;SELECT date1 - date2 MONTH FROM dates;*** Failure 7453 Interval field overflow. *** Failure 7453 Interval field overflow.

How many months are between Jan 1,1899 and Jan 1,2004?

SELECT date1 - date2 MONTH(4) FROM dates;SELECT date1 - date2 MONTH(4) FROM dates; 1260 1260

How many days are between Jan. 1, 1899 and Jan 1,2004?

SELECT date1 - date2 FROM dates;SELECT date1 - date2 FROM dates;

Result is an integer representing # days.Too large to be represented as INTERVAL DAY.Result is an integer representing # days.Too large to be represented as INTERVAL DAY.

38350 38350

ANSI Time and Dates Page 4-19

Page 152: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Interval Casting and Calculations Casting of interval data types is used to convert one interval data type into another. Within the year-month category, an interval may be recast as any other interval. Years may be cast as months and months may be cast as years and months and so on. Mathematical operations such as division and multiplication may be used with interval data types to calculate fractional portions of intervals or to apply a factor of multiple intervals to a date. Note in the examples on this page, in certain circumstances a conversion of the date type may not be necessary. Because years and months are being added together, the optimizer will figure out that the result must be represented in years and months. The second example is much simpler and produces the same result.

SELECT (CAST(INTERVAL '125' MONTH AS INTERVAL YEAR(2) TO MONTH)) + INTERVAL '12' YEAR;

( 125+ 12) --------------- 22-05

125 mos = 10 yrs 5 mos + 12 yrs = 22 yrs 5

Not

SELECT INTERVAL '125' MONTH + INTERVAL '12'

( 125+ 12) --------------- 22-05

Additional

ANSI Time and Dates Page 4-20

Page 153: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Interval Casting and Calculations

YEARYEAR TO MONTHMONTH

YEARYEAR TO MONTHMONTH

CAST ASYEARYEAR TO MONTHMONTH

YEARYEAR TO MONTHMONTH

INTERVAL CASTINGS

Convert 3 years and 4 months to months:

SELECT CAST((INTERVAL '3-04' YEAR TO MONTH) AS INTERVAL MONTH);

SELECT CAST((INTERVAL '3-04' YEAR TO MONTH) AS INTERVAL MONTH); 40 40

SELECT CAST((INTERVAL '3-07' YEAR TO MONTH) AS INTERVAL YEAR);

SELECT CAST((INTERVAL '3-07' YEAR TO MONTH) AS INTERVAL YEAR);

Convert 3 years and 7 months to years:

3 3

INTERVAL CALCULATIONS

Calculate one third of 10 years and 2 months:

SELECT INTERVAL '10-02' YEAR TO MONTH / 3;SELECT INTERVAL '10-02' YEAR TO MONTH / 3;10 yrs 2 mos = 122 mos / 3 = 40.67 mos

round to 40 = 3 yrs 4 mos10 yrs 2 mos = 122 mos / 3 = 40.67 mos

round to 40 = 3 yrs 4 mos

3-043-04

Calculate 12 fifths of 2 years and 2 months:

SELECT INTERVAL '2-02' YEAR TO MONTH *12 / 5;SELECT INTERVAL '2-02' YEAR TO MONTH *12 / 5;2 yrs 2 mos = 26mos, 26*12=312 mos312 /5 = 62.4 mos, 62 /12 = 5 (plus 2 mos remainder)2 yrs 2 mos = 26mos, 26*12=312 mos312 /5 = 62.4 mos, 62 /12 = 5 (plus 2 mos remainder)

5-02 5-02

ANSI Time and Dates Page 4-21

Page 154: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extracting from Date We may combine a date with an interval and extract the year, month or day from the result. Note: Students may choose at this point to do the DATE Lab Exercises at the end of this module. SELECT * FROM

date1 date2 --------------- ----------------

ANSI Time and Dates Page 4-22

Page 155: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extracting from Date

EXTRACTYEARMONTHDAY

FROM (DATE)

SELECT EXTRACT (YEAR FROM date1) FROM dates;SELECT EXTRACT (YEAR FROM date1) FROM dates;Extract the year from Jan. 01, 2004 (date1).

2004 2004

SELECT date1 + INTERVAL '20' DAY FROM dates;SELECT date1 + INTERVAL '20' DAY FROM dates;What is the date 20 days after Jan. 01, 2004?

2004-01-21 2004-01-21

SELECT EXTRACT (YEAR FROM (date1 + INTERVAL '20' DAY)) FROM dates;

SELECT EXTRACT (YEAR FROM (date1 + INTERVAL '20' DAY)) FROM dates;

Extract the year 20 days following Jan. 01, 2004 :

20042004

SELECT EXTRACT (MONTH FROM (date1+ INTERVAL '20' DAY)) FROM dates;

SELECT EXTRACT (MONTH FROM (date1+ INTERVAL '20' DAY)) FROM dates;

Extract the month 20 days following Jan. 01, 2004

1 1

SELECT EXTRACT (DAY FROM (date1 + INTERVAL '20' DAY)) FROM dates;

SELECT EXTRACT (DAY FROM (date1 + INTERVAL '20' DAY)) FROM dates;

Extract the day 20 days following Jan. 01, 2004 :

2121

ANSI Time and Dates Page 4-23

Page 156: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ANSI Time The TIME data type has some similarities to the DATE data type. It is a single column which carries three different fields of information. It also has an intelligent clock in its implementation, just as DATE has calendar intelligence. Additionally, the TIME data type supports precision in seconds up to six places. Time, like date, may be represented as a literal. The character string must conform properly to the TIME data type. The exact degree of precision will be determined by the parser as it scans the literal. For example, TIME '11:37:58.213000' will be determined to be a TIME(6) data type. TIME data types require six total bytes of storage, regardless of the specified precision. Note: Teradata has traditionally supported a TIME keyword which is selectable and returns an integer representing the time of day in form HHMMSS. It does not represent an actual data type and has no real clock intelligence for purposes of calculations. ODBC Considerations: See page Timestamp module for ODBC TIME setting information.

ANSI Time and Dates Page 4-24

Page 157: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ANSI Time

TIME(n) - Where n = 0-6 (default is 6) HH:MM:SS.nnnnnnHH - stored as BYTEINT (1 byte)MM - stored as BYTEINT (1 byte)SS - stored as DEC(8,6) (4 bytes)

(Character Time conversion Representation requirement)TIME (0) - 11:37:58 CHAR(8)TIME (6) - 11:37:58.213000 CHAR(15)

CREATE TABLE tbla (tbltime TIME);INSERT INTO tbla (CURRENT_TIME); SELECT * FROM tbla;

CREATE TABLE tbla (tbltime TIME);INSERT INTO tbla (CURRENT_TIME); SELECT * FROM tbla;

tbltime11:33:13.480000 tbltime11:33:13.480000

ANSI Time and Dates Page 4-25

Page 158: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Simple Day-Time Intervals Day-Time interval data types can be segregated into simple and complex categories. Simple day-time intervals are those with only one unit of measurement. Complex data types have two. All simple day-time intervals support up to 4 digits for representing the unit, and may be either positive or negative. Additionally, the SECOND interval may have up to six places of precision. Interval literals may be specified by conforming to the proper syntax for the appropriate interval data type. The parser may scan the literal to determine the exact specification of the date type.

ANSI Time and Dates Page 4-26

Page 159: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Simple Day-Time Intervals

• Interval data types represent a displacement between two points in time.

• There are two general categories of INTERVAL data types:

Simple Day-Time Intervals:INTERVAL DAY(n) -9999 to +9999 days, (n=1-4)INTERVAL HOUR(n) -9999 to +9999 hoursINTERVAL MINUTE(n) -9999 to +9999 minutesINTERVAL SECOND(n,m) 9999.999999 seconds

Examples of Simple Day-Time Interval Literals:INTERVAL '320' DAY (320 days)INTERVAL -'9' HOUR (- 9 hours)INTERVAL '700' MINUTE (700 minutes)INTERVAL '1375.904508' SECOND (1375.904508 seconds)

Note: Default values n=2, m=6.Note: Default values n=2, m=6.

DAYHOUR MINUTESECOND DAY TO HOURDAY TO MINUTEDAY TO SECONDHOUR TO MINUTEHOUR TO SECONDMINUTE TO SECOND

DAYHOUR MINUTESECOND DAY TO HOURDAY TO MINUTEDAY TO SECONDHOUR TO MINUTEHOUR TO SECONDMINUTE TO SECOND

Day/Time Intervals

YEARYEAR TO MONTHMONTH

YEARYEAR TO MONTHMONTH

Year/Month IntervalsSimple

Complex

ANSI Time and Dates Page 4-27

Page 160: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Complex Day/Time Intervals Complex day-time intervals are combinations of two simple day-time intervals. Their representation as character strings is a bit more complex because of the varieties of syntax possible. The parser will scan a complex day-time literal to see if it conforms to the expected syntax, then assign a working interval data type to it.

ANSI Time and Dates Page 4-28

Page 161: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Complex Day/Time Intervals

INTERVAL '25 09' DAY TO HOUR (25 days, 9 hours)INTERVAL -'9 13:36' DAY TO MINUTE (- 9 days,13 hrs,36min)INTERVAL '25 13:36:15.5' DAY TO SECOND (25 days,13 hrs,36min,15.5 secs)INTERVAL '13:36' HOUR TO MINUTE (13 hrs,36 min)INTERVAL '13:36:15.5' HOUR TO SECOND (13 hrs,36min,15.5 secs)INTERVAL '36:15.5' MINUTE TO SECOND (36 min,15.5 secs)

INTERVAL '25 09' DAY TO HOUR (25 days, 9 hours)INTERVAL -'9 13:36' DAY TO MINUTE (- 9 days,13 hrs,36min)INTERVAL '25 13:36:15.5' DAY TO SECOND (25 days,13 hrs,36min,15.5 secs)INTERVAL '13:36' HOUR TO MINUTE (13 hrs,36 min)INTERVAL '13:36:15.5' HOUR TO SECOND (13 hrs,36min,15.5 secs)INTERVAL '36:15.5' MINUTE TO SECOND (36 min,15.5 secs)

DAYHOUR MINUTESECOND DAY TO HOURDAY TO MINUTEDAY TO SECONDHOUR TO MINUTEHOUR TO SECONDMINUTE TO SECOND

DAYHOUR MINUTESECOND DAY TO HOURDAY TO MINUTEDAY TO SECONDHOUR TO MINUTEHOUR TO SECONDMINUTE TO SECOND

Day/Time Intervals

YEARYEAR TO MONTHMONTH

YEARYEAR TO MONTHMONTH

Year/Month IntervalsSimple

Complex

Complex day-time intervals are combinations of simple intervals.

Note: When an interval literal is specified in an SQL statement, the parser determinesthe exact data type: i.e., INTERVAL '1375.90' SECOND becomes SECOND(4,2)

Note: When an interval literal is specified in an SQL statement, the parser determinesthe exact data type: i.e., INTERVAL '1375.90' SECOND becomes SECOND(4,2)

Examples of Complex Day-Time Interval Literals:

ANSI Time and Dates Page 4-29

Page 162: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Combining Day-Time Intervals

DAY DAY TO HOUR DAY TO MINUTE DAY TO SECOND HOUR HOUR TO MINUTE HOUR TO SECOND MINUTE MINUTE TO SECOND SECOND

Day/Time Intervals

YEAR YEAR TO MONTH MONTH

Year/Month Intervals

Day-time literals may be combined through addition or subtraction. Only compatible interval types may be combined. Year-month intervals may not be combined with day-time intervals. Because the optimizer can figure out if two intervals are combinable, it can assign a temporary interval data type for the result. Combining an HOUR interval with a SECOND interval produces as a result an HOUR TO SECOND interval.

SELECT INTERVAL '03 05:23' DAY TO MINUTE + INTERVAL -'02 03:23' DAY TO MINUTE;

( 3 05:23+-2 03:23) --------------------------- 1 02:00

Additional

ANSI Time and Dates Page 4-30

Page 163: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Combining Day-Time Intervals

SELECT INTERVAL '03 05:23' DAY TO MINUTE + INTERVAL '02 03:23' DAY TO MINUTE;

SELECT INTERVAL '03 05:23' DAY TO MINUTE + INTERVAL '02 03:23' DAY TO MINUTE;

5 08:46 5 08:46

SELECT INTERVAL '05:23' HOUR TO MINUTE + INTERVAL '43' MINUTE;

SELECT INTERVAL '05:23' HOUR TO MINUTE + INTERVAL '43' MINUTE;

6:06 6:06

24:06:44 24:06:44 SELECT INTERVAL '23:35:55' HOUR TO SECOND + INTERVAL '30:49' MINUTE TO SECOND;

SELECT INTERVAL '23:35:55' HOUR TO SECOND + INTERVAL '30:49' MINUTE TO SECOND;

INTERVAL + INTERVAL = INTERVAL_

SELECT INTERVAL '23' HOUR+ INTERVAL '30.49' SECOND;

SELECT INTERVAL '23' HOUR+ INTERVAL '30.49' SECOND;

23:00:30.49 23:00:30.49

Add 3 days, 5 hrs, 23 mins to 2 days, 3 hrs, 23 mins:

Add 43 mins to 5 hrs and 23 mins:

Add 30 mins 49 secs to 23 hrs, 35 mins, 55 secs:

Add 30.49 secs to 23 hrs:

ANSI Time and Dates Page 4-31

Page 164: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Time Literals

TIME

HOUR HOUR TO MINUTE HOUR TO SECOND MINUTE MINUTE TO SECONDSECOND

Time literals may be combined with time-based intervals to produce a new time. The resulting time will be treated as the same time data-type as the starting time, regardless of the interval type. If changing the resulting time to a different time data type (i.e., with different precision) is desired, the result must be cast to the new time data type. An example of this follows: SELECT CAST((TIME '09:30:22' - INTERVAL '01:20:10.45' HOUR TO SECOND) AS TIME(2)); (09:30:22- 1:20:10.45) ------------------------------

Result is cast as

ANSI Time and Dates Page 4-32

Page 165: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Time Literals

SELECT TIME '09:30:22' + INTERVAL '01:20' HOUR TO MINUTE;

SELECT TIME '09:30:22' + INTERVAL '01:20' HOUR TO MINUTE;

SELECT TIME '09:30:22' - INTERVAL '01:20:10' HOUR TO SECOND;

SELECT TIME '09:30:22' - INTERVAL '01:20:10' HOUR TO SECOND;

Time literals are represented by the format: TIME 'HH:MM:SS.nnnnnn'

SELECT TIME '09:30:22.45' - INTERVAL '01:20:10' HOUR TO SECOND;

SELECT TIME '09:30:22.45' - INTERVAL '01:20:10' HOUR TO SECOND;

Time literal is of type TIME(2)Time interval is of type HOUR TO SECOND(0)Result is TIME(2)

Time literal is of type TIME(2)Time interval is of type HOUR TO SECOND(0)Result is TIME(2)

SELECT TIME '09:30:22' - INTERVAL '01:20:10.45' HOUR TO SECOND;

SELECT TIME '09:30:22' - INTERVAL '01:20:10.45' HOUR TO SECOND;

Time literal is of type TIME(0)Time interval is of type HOUR TO SECOND(2)Result is TIME(0) - subtract first, round result

Time literal is of type TIME(0)Time interval is of type HOUR TO SECOND(2)Result is TIME(0) - subtract first, round result

Assuming the current time is 09:30:22:

What time was it 1hour and 20 minutes and 10 seconds ago?

Redo the previous example using TIME(2) for current time:

Redo the same example with an HOUR TO SECOND(2) interval:

10:50:2210:50:22

08:10:1208:10:12

08:10:12.4508:10:12.45

08:10:1108:10:11

What time will it be in 1hour and 20 minutes?

ANSI Time and Dates Page 4-33

Page 166: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Time Subtraction Time subtraction will produce a time-interval result as specified in the SQL statement. If a result is to be represented in hours, any minutes or seconds in the remainder will be omitted, as no attempt at rounding takes place. If no time interval type is specified for the result, an error will be reported. The exact number of digits in the result must be specified in the interval type, when the result will not be compatible with the default number of digits. The following table is used for the examples on the facing page:

CREATE TABLE times time1 TIME ,time2 TIME ,interval1 HOUR TO SECOND(2) ,interval2 MINUTE(3) ,interval3 SECOND(4);

How much time is there before midnight? (Time is approx 5:35 PM)

(23:59:59.99 - Current Time) HOUR ------------------------------------------------- 6

SELECT (TIME '23:59:59.99' - CURRENT_TIME) HOUR;

(23:59:59.99 - Current Time) HOUR TO MINUTE ------------------------------------------------------------------ 6:24

SELECT (TIME '23:59:59.99' - CURRENT_TIME) HOUR TO MINUTE;

(23:59:59.99 - Current Time) HOUR TO SECOND -------------------------------------------------------------------- 6:24:57.80

SELECT (TIME '23:59:59.99' - CURRENT_TIME) HOUR TO SECOND(2);

Additional Examples

ANSI Time and Dates Page 4-34

Page 167: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Time Subtraction

SELECT (time1 - time2) HOUR FROM times;SELECT (time1 - time2) HOUR FROM times;

SELECT (time1 - time2) MINUTE FROM times;SELECT (time1 - time2) MINUTE FROM times;

SELECT (time1 - time2) MINUTE(3) FROM times;SELECT (time1 - time2) MINUTE(3) FROM times;

SELECT (time1 - time2) SECOND(4) FROM times;SELECT (time1 - time2) SECOND(4) FROM times;

How many full hours elapse between 8 AM and 10:35:40 AM?

How many minutes elapse between 8 AM and 10:35:40 AM?

How many minutes elapse between 8 AM and 10:35:40 AM?

How many seconds elapse between 8 AM and 10:35:40 AM?

TIME - TIME =

HOURHOUR TO MINUTEHOUR TO SECONDMINUTEMINUTE TO SECONDSECOND

SELECT * FROM times;SELECT * FROM times; CREATE TABLE timestime1 TIME,time2 TIME,interval1 HOUR TO SECOND(2),interval2 MINUTE(3),interval3 SECOND(4);

CREATE TABLE timestime1 TIME

,time2 TIME,interval1 HOUR TO SECOND(2),interval2 MINUTE(3),interval3 SECOND(4);

22

*** FAILURE 7453 INTERVAL FIELD OVERFLOW.*** FAILURE 7453 INTERVAL FIELD OVERFLOW.

155155

9340.0000009340.000000

time1 time210:35:40 08:00:00

time1 time210:35:40 08:00:00

ANSI Time and Dates Page 4-35

Page 168: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Day-Time Interval Castings Conversion between different types of day-time interval types is easily accomplished using the CAST feature of SQL. Hours may be converted into days, days into minutes and minutes into hours as needed. It is useful to note that conversions which include a remainder not supported by the conversion interval type will be dropped, not rounded. Converting 47 hours into days will yield only one day. Attention must be paid to the appropriate number of digits specified in the conversion interval type. If a conversion results in 100 days, it is necessary to define the conversion interval type as DAY(3).

ANSI Time and Dates Page 4-36

Page 169: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Day-Time Interval Castings

SELECT CAST((INTERVAL '3:45' HOUR TO MINUTE) AS INTERVAL HOUR);

SELECT CAST((INTERVAL '3:45' HOUR TO MINUTE) AS INTERVAL HOUR);

SELECT CAST((INTERVAL '3:45' HOUR TO MINUTE) AS INTERVAL MINUTE(3));

SELECT CAST((INTERVAL '3:45' HOUR TO MINUTE) AS INTERVAL MINUTE(3));

SELECT CAST((INTERVAL '50:45' HOUR TO MINUTE) AS INTERVAL DAY TO MINUTE);

SELECT CAST((INTERVAL '50:45' HOUR TO MINUTE) AS INTERVAL DAY TO MINUTE);

SELECT CAST((INTERVAL '2 02:45' DAY TO MINUTE) AS INTERVAL HOUR TO MINUTE);

SELECT CAST((INTERVAL '2 02:45' DAY TO MINUTE) AS INTERVAL HOUR TO MINUTE);

Interval ConversionsInterval Conversions

Reverse the previous conversion:

Convert 50 hours, 45 minutes to days, hours and minutes:

Convert 3 hours, 45 minutes to minutes:

Convert 3 hours, 45 minutes to hours:

DAYDAY TO HOURDAY TO MINUTEDAY TO SECONDHOURHOUR TO MINUTEMINUTEMINUTE TO SECOND

DAYDAY TO HOURDAY TO MINUTEDAY TO SECONDHOURHOUR TO MINUTEMINUTEMINUTE TO SECOND

CAST AS

DAYDAY TO HOURDAY TO MINUTEDAY TO SECONDHOURHOUR TO MINUTEMINUTEMINUTE TO SECOND

DAYDAY TO HOURDAY TO MINUTEDAY TO SECONDHOURHOUR TO MINUTEMINUTEMINUTE TO SECOND

33

225225

2 02:45 2 02:45

50:4550:45

ANSI Time and Dates Page 4-37

Page 170: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Interval Casting and Calculations Because the default for the seconds interval is SECOND(2,6), all conversions to seconds will yield six precision places, unless the conversion interval type is defined otherwise. Note in the example on the facing-page, a conversion to SECOND(4,0) was necessary, both to contain the appropriate number of seconds and to eliminate the precision. Arithmetic calculations such as multiplication and division also may be performed on day-time interval types. One might wish to combine a given time interval multiple times or divide it into fixed time units. Either is easily accomplished with standard SQL arithmetic operators.

ANSI Time and Dates Page 4-38

Page 171: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Interval Casting and Calculations

SELECT CAST((INTERVAL '2' HOUR) AS INTERVAL SECOND(4));

SELECT CAST((INTERVAL '2' HOUR) AS INTERVAL SECOND(4));

SELECT CAST((INTERVAL '2' HOUR)AS INTERVAL SECOND(4,0));

SELECT CAST((INTERVAL '2' HOUR)AS INTERVAL SECOND(4,0)); 7200 7200

Interval Castings

Convert 2 hours to seconds without precision:

Convert 2 hours to seconds with 6-point precision:

Interval Calculations

SELECT INTERVAL '3 07' DAY TO HOUR/2;SELECT INTERVAL '3 07' DAY TO HOUR/2; 1 151 15

SELECT INTERVAL '03 02:20' DAY TO MINUTE*3;SELECT INTERVAL '03 02:20' DAY TO MINUTE*3; 9 07:00 9 07:00

Compute half of the interval 3 days and 7 hours:

Triple the interval 3 days, 2 hours and 20 minutes:

7200.0000007200.000000

ANSI Time and Dates Page 4-39

Page 172: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extracting from Time The number of hours, minutes or seconds may be extracted from a TIME data type by using the EXTRACT feature of SQL. Units may be extracted either from a time data type or from a calculation resulting in a time data type. As the example on the facing-page indicates, one may extract from time plus or minus some time interval.

time1 time2 ------------ ------------ 10:35:40 08:00:00

SELECT * FROM times;

ANSI Time and Dates Page 4-40

Page 173: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extracting from Time

EXTRACTHOURMINUTESECOND

FROM (TIME)

SELECT time1 + INTERVAL '10:15' MINUTE TO SECONDFROM times;SELECT time1 + INTERVAL '10:15' MINUTE TO SECONDFROM times;

Add 10 minutes and 15 seconds to the time 10:35:40 :

10:45:55 10:45:55

SELECT EXTRACT (HOUR FROM (time1+ INTERVAL '10:15' MINUTE TO SECOND))

FROM times;

SELECT EXTRACT (HOUR FROM (time1+ INTERVAL '10:15' MINUTE TO SECOND))

FROM times;

Extract the hours portion from the new time:

10 10

SELECT EXTRACT (MINUTE FROM (time1 + INTERVAL '10:15' MINUTE TO SECOND))

FROM times;

SELECT EXTRACT (MINUTE FROM (time1 + INTERVAL '10:15' MINUTE TO SECOND))

FROM times;

Extract the minutes portion from the new time:

4545

SELECT EXTRACT (SECOND FROM (time1 + INTERVAL '10:15' MINUTE TO SECOND))

FROM times;

SELECT EXTRACT (SECOND FROM (time1 + INTERVAL '10:15' MINUTE TO SECOND))

FROM times;

Extract the seconds portion from the new time:

55 55

SELECT EXTRACT (HOUR FROM time1) FROM times;SELECT EXTRACT (HOUR FROM time1) FROM times;

Extract the hour portion from the time 10:35:40 (time1).

1010

ANSI Time and Dates Page 4-41

Page 174: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercises

ANSI Time and Dates Page 4-42

Page 175: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercises

1.) What time will it be in 3 hours and 20 minutes? ____

2.) What hour will it be in 3 hours and 20 minutes? ____

3.) What will the date be in 6 months? ____

4.) How much time before midnight? ____

5.) What's the date in 10 days? ____

6.) How many years are there in 500 months? ____

7.) What year will it be in 500 months? ____

8.) Combine the time spent on the two projects. ____

9.) How many days till Christmas? ____

10.) What's the halfway point of the project timeline? ____

Match the technique from the lettered list with the appropriate question from the numbered list.

a.) CAST

b.) ADD_MONTHS

c.) DATE INTERVAL

d.) DATE - DATE

e.) INTERVAL */ n

f.) INTERVAL INTERVAL

g.) EXTRACT

h.) TIME INTERVAL

i.) TIME - TIME

_+

_+

_+

ANSI Time and Dates Page 4-43

Page 176: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)

ANSI Time and Dates Page 4-44

Page 177: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)

Date Lab Exercises1.) Show the total number of years between 2000-01-01 and 2010-12-31.

2.) Show the total number of months between 2000-01-01 and 2010-12-31.

3.) Show the total number of days between 2000-01-01 and 2010-12-31.

4.) Show the total number of years with a month remainder between 2010-12-31 and 2000-01-01.

5.) Change one of the dates to make the difference exactly 11 years.

6.) Show the date halfway between 2000-02-15 and 2000-10-15.

7.) Convert the interval 232 months to year/month and display half of the interval.

8.) Extract the year 9999 days beyond 2000-01-01.

ANSI Time and Dates Page 4-45

Page 178: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

ANSI Time and Dates Page 4-46

Page 179: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

Time Lab Exercises1.) Assume current time is 09:45:10 What time will it be in 900 minutes?

2.) Add 2 hours, 10 minutes and 35.50 seconds to current time (09:45:10) and display the result with 2 point precision.

3.) How much time is there between the following two times; 10:20:30 and 12:30:45 ?In hours?In minutes?In seconds?

4.) How much time is there before midnight tonight? Use actual time.

5.) Convert 76 hours to days with an hour remainder.

6.) Convert 1 day, 6 hours and 22 minutes to minutes.

7.) What represents the halfway point between 11:30:30 and 18:00:00?

8.) Extract the hour 1200 minutes from the current time (09:45:10)

ANSI Time and Dates Page 4-47

Page 180: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

ANSI Time and Dates Page 4-48

Page 181: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 5

Module 5ANSI Timestamps and Time Zones

After completing this module, you should be able to:

• Perform queries using ANSI compliant Timestamp and Time with Zone representations.

• Use Timestamps for time sequenced processing.

• Use Zones for geographic location based processing.

• Extract time or date information from a timestamp.

ANSI Timestamps and Time Zones Page 5-1

Page 182: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ANSI Timestamps and Time Zones Page 5-2

Page 183: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

ANSI Timestamps and Time Zones Page 5-3

Page 184: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ANSI Timestamp TIMESTAMP is a data type which integrates both date and time into a single column. By combining the DATE(four bytes) and the TIME(six bytes) data types, the resulting type requires ten bytes of storage. The default timestamp is TIMESTAMP(6), yielding six places of precision for seconds. Timestamps represented as character strings must be syntactically precise and may take a maximum of twenty six bytes of storage. CURRENT_TIMESTAMP is a reserve word which when selected returns the current date and time. Timestamps may be combined with any interval type, either Year-Month or Day-Time, to yield a new timestamp. ODBC Considerations: See page 2-6 for ODBC TIMESTAMP setting information.

ANSI Timestamps and Time Zones Page 5-4

Page 185: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ANSI TimestampTimestamp combines date and time into a single column.

TIMESTAMP(n) - Where n=(0-6)

Consists of 6 fields of informationYEAR,MONTH,DAY,HOUR,MINUTE,SECONDInternal format is DATE(4 bytes) + TIME(6 bytes) = 10 bytes

Timestamp Formats Character ConversionTIMESTAMP(0) = '2001-12-07 11:37:58' CHAR(19)TIMESTAMP(6) = '2001-12-07 11:37:58.213000' CHAR(26)

TIMESTAMP

YEAR YEAR TO MONTHMONTHDAYDAY TO HOURDAY TO MINUTEDAY TO SECONDHOURHOUR TO MINUTEHOUR TO SECONDMINUTEMINUTE TO SECONDSECOND

Timestamp may be combined with any day-time interval to produce a new timestamp.

Timestamp may be combined with any day-time interval to produce a new timestamp.

CREATE TABLE tblb (tmstampb TIMESTAMP);INSERT INTO tblb (CURRENT_TIMESTAMP);SELECT * FROM tblb;

CREATE TABLE tblb (tmstampb TIMESTAMP);INSERT INTO tblb (CURRENT_TIMESTAMP);SELECT * FROM tblb; tmstampb

2001-11-06 13:48:38.580000tmstampb2001-11-06 13:48:38.580000

ANSI Timestamps and Time Zones Page 5-5

Page 186: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

TIMESTAMP Combined with Intervals

TIMESTAMP

YEAR YEAR TO MONTH MONTH DAY DAY TO HOUR DAY TO MINUTE DAY TO SECOND HOUR HOUR TO MINUTE HOUR TO SECOND MINUTE MINUTE TO SECOND SECOND

Timestamps may be combined with any type of interval data type. Timestamps also may be represented as literals, using the TIMESTAMP “YYYY-MM-DD HH:MM:SS.nnnnnn” format. The timestamp resulting from such an operation will carry the same precision as the originating timestamp, unless the resulting timestamp is CAST to a different precision level. Note: all examples on the facing page are aligned to make addition and subtraction of the appropriate intervals easier to read.

ANSI Timestamps and Time Zones Page 5-6

Page 187: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Timestamp Combined with Intervals

SELECT tmstmp1- INTERVAL '01:20:10' HOUR TO SECOND

FROM timestamps;

SELECT tmstmp1- INTERVAL '01:20:10' HOUR TO SECOND

FROM timestamps;

SELECT tmstmp1 + INTERVAL '08 10:15' DAY TO MINUTE

FROM timestamps;

SELECT tmstmp1 + INTERVAL '08 10:15' DAY TO MINUTE

FROM timestamps;

2004-10-01 08:10:122004-10-01 08:10:12

SELECT tmstmp1- INTERVAL '2-06' YEAR TO MONTH

FROM timestamps;

SELECT tmstmp1- INTERVAL '2-06' YEAR TO MONTH

FROM timestamps;2002-04-01 09:30:222002-04-01 09:30:22

2004-10-09 19:45:222004-10-09 19:45:22

SELECT tmstmp1 - INTERVAL '01:20:10.45' HOUR TO SECOND

FROM timestamps;

SELECT tmstmp1 - INTERVAL '01:20:10.45' HOUR TO SECOND

FROM timestamps;2004-10-01 08:10:11 2004-10-01 08:10:11

• Timestamp is TIMESTAMP(0)• Interval is HOUR TO SECOND(2)• Result is TIMESTAMP(0) and may be re-cast if needed

• Timestamp is TIMESTAMP(0)• Interval is HOUR TO SECOND(2)• Result is TIMESTAMP(0) and may be re-cast if needed

Subtract 2 yrs and 6 mos from tmstmp1:

Subtract 1 hr, 20 mins and 10 secsfrom tmstmp1:

Add 8 days, 10 hrs, 15 mins to tmstmp1:

Subtract 1 hr, 20 min, 10.45 sec from tmstmp1:

SELECT tmstmp1 FROM timestamps;SELECT tmstmp1 FROM timestamps; tmstmp12004-10-01 09:30:22

tmstmp12004-10-01 09:30:22

ANSI Timestamps and Time Zones Page 5-7

Page 188: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Timestamp Subtraction

TIMESTAMP - TIMESTAMP =

YEAR YEAR TO MONTH MONTH DAY DAY TO HOUR DAY TO MINUTE DAY TO SECOND HOUR HOUR TO MINUTE MINUTE MINUTE TO SECOND

A timestamp may be subtracted from another timestamp to yield an interval. The interval to be yielded must be specified as part of the syntax. Any remainders calculated will be discarded if they are not represented in the interval data type. All examples on the facing page are aligned to make the timestamp subtraction more easily readable.

SELECT (TIMESTAMP '2001-10-20 10:25:40' - TIMESTAMP '2000-09-19 08:20:00') DAY(3) TO

(2001-10-20 10:25:40 - 2000-09-19 08:20:00) DAY TO HOUR ----------------------------------------------------------------------------------- 396 02

SELECT (TIMESTAMP '2001-10-20 10:25:40' - TIMESTAMP '2000-09-19 08:20:00') DAY(3) TO

(2001-10-20 10:25:40 - 2000-09-19 08:20:00) DAY TO MINUTE -------------------------------------------------------------------------------------- 396 02:05

Examples of Literal Timestamp

Timestamp literals are represented by the format:TIMESTAMP 'YYYY-MM-DD HH:MM:SS nnnnnn'

ANSI Timestamps and Time Zones Page 5-8

Page 189: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Timestamp Subtraction

SELECT tmstmp1 - tmstmp2 MONTH FROM timestamps; SELECT tmstmp1 - tmstmp2 MONTH FROM timestamps; 1212

SELECT tmstmp1 - tmstmp2 YEAR FROM timestamps;SELECT tmstmp1 - tmstmp2 YEAR FROM timestamps; 11

SELECT tmstmp1 - tmstmp2 DAY(3) FROM timestamps;SELECT tmstmp1 - tmstmp2 DAY(3) FROM timestamps; 378378

SELECT tmstmp1 - tmstmp2DAY(3) TO SECOND

FROM timestamps;

SELECT tmstmp1 - tmstmp2DAY(3) TO SECOND

FROM timestamps;378 01:10:22.000000378 01:10:22.000000

Calculate the difference between the following two timestamps as directed:

In months?

In years?

In days?

In days, hours, minutes and seconds?

SELECT tmstmp1 FROM timestamps;SELECT tmstmp1 FROM timestamps; tmstmp1 tmstmp22004-10-01 09:30:22 2003-09-19 08:20:00

tmstmp1 tmstmp22004-10-01 09:30:22 2003-09-19 08:20:00

ANSI Timestamps and Time Zones Page 5-9

Page 190: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Timestamp Casting Timestamps may be cast as a date, a time, or another timestamp. Casting as another timestamp is reasonable only if a change to the precision of seconds is needed. Note that it is not possible to cast precision to be less than the original timestamp. Doing so results in an overflow error. Timestamp literals are represented by the format: TIMESTAMP 'YYYY-MM-DD HH:MM:SS.nnnnnn'

ANSI Timestamps and Time Zones Page 5-10

Page 191: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Timestamp Casting

TIMESTAMP may be CAST as:DATETIME(n)TIMESTAMP(n)

TIMESTAMP may be CAST as:DATETIME(n)TIMESTAMP(n)

SELECT CAST(tmstmp1 AS DATE) FROM timestamps;SELECT CAST(tmstmp1 AS DATE) FROM timestamps;

04/10/0104/10/01

SELECT CAST(tmstmp1 AS TIME(2)) FROM timestamps; SELECT CAST(tmstmp1 AS TIME(2)) FROM timestamps;

SELECT CAST((tmstmp1 - INTERVAL '01:20:10.45' HOUR TO SECOND) AS TIMESTAMP(2))

FROM timestamps;

SELECT CAST((tmstmp1 - INTERVAL '01:20:10.45' HOUR TO SECOND) AS TIMESTAMP(2))

FROM timestamps; 2004-10-01 08:10:11.55 2004-10-01 08:10:11.55

SELECT CAST((TIMESTAMP '2001-10-01 09:30:22.000000') AS TIMESTAMP(2));

SELECT CAST((TIMESTAMP '2001-10-01 09:30:22.000000') AS TIMESTAMP(2)); *** Failure 7454 DateTime field overflow.*** Failure 7454 DateTime field overflow.

Casting which truncates precision is not permittedCasting which truncates precision is not permitted

Cast the given timestamp as a Date:

Cast the given timestamp as a Time:

Subtract the given interval and cast the result as TIMESTAMP(2):

Reduce timestamp precision totwo places:

SELECT tmstmp1 FROM timestamps;SELECT tmstmp1 FROM timestamps;

tmstmp1 2004-10-01 09:30:22

Timestamp literals are represented by the format:TIMESTAMP 'YYYY-MM-DD HH:MM:SS.nnnnnn'Timestamp literals are represented by the format:TIMESTAMP 'YYYY-MM-DD HH:MM:SS.nnnnnn'

09:30:22.0009:30:22.00

ANSI Timestamps and Time Zones Page 5-11

Page 192: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ODBC Date and Time Considerations Setting Date Time Formats in ODBC: DateTimeFormat = [ [I|A] ] [I|A] [I|A] ] Where A=ANSI (new format), I=Integer (old format) The first letter in the three-letter combination represents the DATE format, the second represents the TIME format, and the third the TIMESTAMP format. The letter 'A' indicates ANSI (new format), and the letter 'I' indicates INTEGER (old format).

DateTimeFormat = IIA (Indicates INTEGER DATE, INTEGER TIME and ANSI TIMESTAMP. When using the driver with Teradata V2R2 or earlier releases, no action is required to use the old-style DATEs, TIMEs or TIMESTAMPs. When using the driver with Teradata V2R3 or later, the default action is to use the old-style DATEs, TIMEs or TIMESTAMPs. If you wish to use ANSI DATEs, TIMEs or TIMESTAMPs instead, specify the appropriate value through the setup.

ANSI Timestamps and Time Zones Page 5-12

Page 193: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ODBC Date and Time Considerations

Setting Date Time Formats in ODBC:

DateTimeFormat = [ [I|A] ] [I|A] [I|A] ]

Where A=ANSI (new format), I=Integer (old format)

• The first of the three letters represents the DATE format.• The second represents the TIME format.• The third represents the TIMESTAMP format.

CREATE TABLE t1 (c1 DATE, c2 TIME, c3 TIMESTAMP);

SHOW TABLE t1;

CREATE SET TABLE SQL01.t1 ,NO FALLBACK ,NO BEFORE JOURNAL, NO AFTER JOURNAL(c1 DATE FORMAT 'YYYY-MM-DD',c2 INTEGER FORMAT '99:99:99',c3 TIMESTAMP(6))

PRIMARY INDEX ( c1 );

CREATE TABLE t1 (c1 DATE, c2 TIME, c3 TIMESTAMP);

SHOW TABLE t1;

CREATE SET TABLE SQL01.t1 ,NO FALLBACK ,NO BEFORE JOURNAL, NO AFTER JOURNAL(c1 DATE FORMAT 'YYYY-MM-DD',

c3 TIMESTAMP(6))PRIMARY INDEX ( c1 );

Created with ODBC settingDateTimeTimestamp 'III'.

CREATE TABLE t1 (c1 DATE, c2 TIME, c3 TIMESTAMP);

SHOW TABLE t1;

CREATE SET TABLE SQL01.t1 ,NO FALLBACK ,NO BEFORE JOURNAL, NO AFTER JOURNAL(c1 DATE FORMAT 'YYYY-MM-DD',

c3 TIMESTAMP(6))PRIMARY INDEX ( c1 );

c2 INTEGER FORMAT '99:99:99', c2 TIME(6),

CREATE TABLE t1 (c1 DATE, c2 TIME, c3 TIMESTAMP);

SHOW TABLE t1;

CREATE SET TABLE SQL01.t1 ,NO FALLBACK ,NO BEFORE JOURNAL, NO AFTER JOURNAL(c1 DATE FORMAT 'YYYY-MM-DD',

c3 TIMESTAMP(6))PRIMARY INDEX ( c1 );

Created with ODBC settingDateTimeTimestamp 'AAA'.

c2 TIME(6),

ANSI Timestamps and Time Zones Page 5-13

Page 194: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Time Zones The selection of reserved words CURRENT_TIME and CURRENT_TIMESTAMP automatically will return the current time zone in addition to the current time or timestamp. Time zones always are relative to some agreed upon base time zone. Typically, this base time zone is assigned value 00:00. Internationally, 00:00 is assigned to Greenwich Mean Time (GMT). All other time zones are relative to GMT. As a database administrator, one may assign 00:00 to the local time zone or any other specified choice. Time zones increase in value as they move East and decrease as they move west. If one moves West past the International Date Line, one can arrive in the previous day. As we look at this topic, we shall see how to set time zones, how to store and retrieve them, and how to use them to normalize date and time information.

ANSI Timestamps and Time Zones Page 5-14

Page 195: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Time Zones

CURRENT_TIME & CURRENT_TIMESTAMP both return time zoneCURRENT_TIME & CURRENT_TIMESTAMP both return time zone

SELECT CURRENT_TIME;SELECT CURRENT_TIME;

Current Time16:29:30+00:00Current Time16:29:30+00:00

SELECT CURRENT_TIMESTAMP;SELECT CURRENT_TIMESTAMP;

Current TimeStamp2001-11-16 16:30:02.070000+00:00 Current TimeStamp2001-11-16 16:30:02.070000+00:00

Time zones may be relative to:• Greenwich Meridian Time (GMT)• Your Local Time Zone• Any Time Zone you choose

LondonLAHongKong

00:00(GMT)

-08:00 +08:00

LondonHongKongLA

00:00 +08:00(GMT)

+16:00

Relative to GMT Relative to LA

ANSI Timestamps and Time Zones Page 5-15

Page 196: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Data Types with Time Zone Greenwich Mean Time also is referred to as Universal Coordinated Time (UTC). The acronym seems incorrect, however its syntax based on a language other than English. If UTC is considered 00:00, one can easily determine that the Pacific Standard Time zone, which is eight hours earlier, will be -08:00. Both TIME and TIMESTAMP data types may be specified with or without time zones. Timestamps recorded with time zone information may be compared to other timestamps in other time zones. For example, if I were studying a global phenomenon as it occurred at 1 PM PST, it might be necessary to relate that time to the exact same moment in Hong Kong, where clearly it would be a different time of day. Both TIME WITH ZONE and TIMESTAMP WITH ZONE may be represented as literals using conventions we have seen previously. The addition of the time zone adds two bytes of storage to both of these data types, one byte for the hour and one byte for the minute representation of the time zone. Some time zones are not set on the hour but on the half hour.

ANSI Timestamps and Time Zones Page 5-16

Page 197: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Data Types with Time Zone

TIME(n) WITH TIME ZONEConsists of TIME(n) + two additional fields (6+2 = 8 bytes)

TIMEZONE_HOUR (BYTEINT, -12 to +13)TIMEZONE_MINUTE (BYTEINT, 00 to 59)

TIME(n) WITH TIME ZONEConsists of TIME(n) + two additional fields (6+2 = 8 bytes)

TIMEZONE_HOUR (BYTEINT, -12 to +13)TIMEZONE_MINUTE (BYTEINT, 00 to 59)

Literal time representations Data TypeTIME '11:37:58' TIME(0)TIME '11:37:58-08:00' TIME(0) WITH TIME ZONETIME '11:37:58.213000+09:30' TIME WITH TIME ZONE

TIMESTAMP(n) WITH TIME ZONEConsists of TIMESTAMP(n) + same two additional fields (10+2 = 12)

TIMESTAMP(n) WITH TIME ZONEConsists of TIMESTAMP(n) + same two additional fields (10+2 = 12)

Literal time representations Data TypeTIMESTAMP '2001-10-01 11:37:58' TIMESTAMP(0)

TIMESTAMP TIMESTAMP(0)'2001-10-01 11:37:58-08:00' WITH TIME ZONE

TIMESTAMP TIMESTAMP'2001-10-01 11:37:58.213000+09:30' WITH TIME ZONE

UTC = 00:00 Universal Coordinated TimeAlso known as GMTDefault if Time Zone unspecified

EST = -05:00 Eastern Standard Time (5 hours earlier)EDT = -04:00 Eastern Daylight TimePST = -08:00 Pacific Standard Time

UTC = 00:00 Universal Coordinated TimeAlso known as GMTDefault if Time Zone unspecified

EST = -05:00 Eastern Standard Time (5 hours earlier)EDT = -04:00 Eastern Daylight TimePST = -08:00 Pacific Standard Time

Relative Time Zones

ANSI Timestamps and Time Zones Page 5-17

Page 198: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Setting Time Zones There are three levels at which time zones may be set. The system administrator sets the system level time zone, which will otherwise default to 00:00. The setting is done by a modification to the DBSCONTROL record as part of system initialization procedures. A user may be defined with a specified time zone as a parameter in the CREATE USER command. The options are as follow::

CREATE USER ………… TIME ZONE = LOCAL at user creation, use the system level time zone = NULL no default is set - use system level at logon = '08:00' explicit time zone setting

Finally, the zone may be set at the session level and may be changed throughout the session. This is accomplished with the following options:

SET TIME ZONE LOCAL revert to the system level time zone SET TIME ZONE USER revert to the user specified time zone, if it exists SET TIME ZONE INTERVAL set an explicit time zone

ANSI Timestamps and Time Zones Page 5-18

Page 199: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Setting Time Zones

MODIFY GENERAL 16 = n /* Hour, n= -12 to +13 */MODIFY GENERAL 17 = n /* Minutes, n = -59 to +59 */MODIFY GENERAL 16 = n /* Hour, n= -12 to +13 */MODIFY GENERAL 17 = n /* Minutes, n = -59 to +59 */

System Level (DBSCONTROL record)

CREATE USER ped ……..TIME ZONE = LOCAL /* use system level */

= NULL /* no default, set atsystem or session level at logon*/

= '08:00' /* explicit setting*/= -'04:30‘ /* explicit setting*/

CREATE USER ped ……..TIME ZONE = LOCAL /* use system level */

= NULL /* no default, set atsystem or session level at logon*/

= '08:00' /* explicit setting*/= -'04:30‘ /* explicit setting*/

USER level

SET TIME ZONE LOCAL /* switch from session levelto system level */

SET TIME ZONE USER /* switch from session levelto user level */

SET TIME ZONE INTERVAL '05:00' HOUR TO MINUTE/* explicit setting */

SET TIME ZONE LOCAL /* switch from session levelto system level */

SET TIME ZONE USER /* switch from session levelto user level */

SET TIME ZONE INTERVAL '05:00' HOUR TO MINUTE/* explicit setting */

SESSION level

There are three levels at which the default time zone may be set:

ANSI Timestamps and Time Zones Page 5-19

Page 200: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

A Tale of Three Cities In the following sequence of pages, we will add three rows to a table, each of them from a different time zone. The first row is added in London which is GMT or 00:00 time zone. The HELP SESSION command provides both the DATEFORM setting and the Session Time Zone. In the example on the facing page, the local time zone setting is 00:00. When inserting a row into a table with a TIMESTAMP WITH ZONE column, it is not necessary to specify the time zone as part of the timestamp. The current time zone for the session will be stored with the timestamp by default. When the data is retrieved, the time zone will be displayed as a normal part of a TIMESTAMP WITH ZONE column.

ANSI Timestamps and Time Zones Page 5-20

Page 201: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

A Tale of Three Cities

User Name PED Account Name DBC Logon Date 02/02/17 Logon Time 10:33:44

Current DataBase PED Collation ASCII

Character Set ASCII Transaction Semantics Teradata

Current DateForm IntegerDate Session Time Zone 00:00

HELP SESSION; (System level time zone is 00:00)HELP SESSION; (System level time zone is 00:00)CREATE TABLE zone_test(loc_id CHAR(3),timestmp_with_zone

TIMESTAMP(0) WITH TIME ZONE);

CREATE TABLE zone_test(loc_id CHAR(3),timestmp_with_zone

TIMESTAMP(0) WITH TIME ZONE);

INSERT INTO zone_test VALUES('GMT',TIMESTAMP '2002-04-15 10:30:00');INSERT INTO zone_test VALUES('GMT',TIMESTAMP '2002-04-15 10:30:00');

SELECT loc_id, timestmp_with_zone FROM zone_test;SELECT loc_id, timestmp_with_zone FROM zone_test;

loc_Id timestmp_with_zoneGMT 2002-04-15 10:30:00+00:00 loc_Id timestmp_with_zoneGMT 2002-04-15 10:30:00+00:00

Assume that a person in London wishes to add a row to this table at 10:30 AM his time. The timestamp reflects the time and date that the row is inserted.

Note: Zone is explicitNote: Zone is explicit

Note: Zone is defaultedNote: Zone is defaulted

ANSI Timestamps and Time Zones Page 5-21

Page 202: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Los Angeles Adds a Row By using the SET TIME ZONE command, we may reset the time zone at any time within the session. Here we reset it to be -08:00 which is Pacific Standard Time (PST). A HELP SESSION command verifies that the new setting is in effect. If we now add another row to the same table, and again do not supply an explicit time zone for the column, it will store the current session time zone (-08:00) with the column. If we select this row after inserting it, we see the explicit time zone of -08:00.

ANSI Timestamps and Time Zones Page 5-22

Page 203: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Los Angeles Adds a Row

(To simulate the row being added in Los Angeles, we reset the time zone default to be local to PST.)User Name PED

Account Name DBC Logon Date 02/02/17 Logon Time 10:33:44

Current DataBase PED Collation ASCII

Character Set ASCII Transaction Semantics Teradata

Current DateForm IntegerDateSession Time Zone -08:00

A person in Los Angeles adds a row to this same table at 10:30 AM his time. (Remember, in England it is eight hours later.)

SELECT loc_id, timestmp_with_zone FROM zone_test; SELECT loc_id, timestmp_with_zone FROM zone_test; loc_Id timestmp_with_zoneGMT 2002-04-15 10:30:00+00:00 (10:30 GMT)PST 2002-04-15 10:30:00-08:00 (10:30 PST)

loc_Id timestmp_with_zoneGMT 2002-04-15 10:30:00+00:00 (10:30 GMT)PST 2002-04-15 10:30:00-08:00 (10:30 PST)

We can now look at both rows with their explicit time zones being displayed. Note, both rows represent 10:30 AM in their respective time zones.

INSERT INTO zone_test('PST',timestamp '2002-04-15 10:30:00');INSERT INTO zone_test('PST',timestamp '2002-04-15 10:30:00');

Note: zone is defaultedNote: zone is defaulted

SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE;SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE;

HELP SESSION;HELP SESSION; Session level time zone setting is -08:00Session level time zone setting is -08:00

ANSI Timestamps and Time Zones Page 5-23

Page 204: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Normalizing Timestamps It is possible to recast a TIMESTAMP WITH ZONE column to a TIMESTAMP column. This conversion has the effect of expressing the timestamp with an implicit time zone. In other words, the conversion does not drop the time zone. It implicitly incorporates it into the new timestamp. We refer to this as time zone normalization. If we select the row and recast it as TIMESTAMP without zone, we see that the timestamp has been normalized to 10:30:00, which is not surprising since -08:00 is the current session time zone setting. The previously inserted row has now been normalized to 02:30:00 PST. This should be interpreted to mean “When it’s 10:30 AM in London (GMT), its 02:30 AM in Los Angeles (PST)”. In other words, all times are normalized and will be displayed as they would appear on a clock in Los Angeles. Normalization can also be achieved by use of the AT LOCAL syntax. The one difference with this method is the display of the local default time zone on each row.

PST -

GMT 00.0

2:3 10:3

10:3 18:3

Insertion time of first row

Insertion time of second

ANSI Timestamps and Time Zones Page 5-24

Page 205: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Normalizing TimestampsThe L.A. person wants to see other rows to see what time they were inserted. He wants to see the times relative to PST since that is where he is and that is his default time zone.

The AT LOCAL option shows the time relative to the local time zone andexplicitly displays the local time zone.

The AT LOCAL option shows the time relative to the local time zone andexplicitly displays the local time zone.

SELECT loc_id, timestmp_with_zoneAT LOCAL FROM zone_test;

SELECT loc_id, timestmp_with_zoneAT LOCAL FROM zone_test;

loc_id timestmp_with_zone AT LOCALGMT 2002-04-15 02:30:00-08:00 PST 2002-04-15 10:30:00-08:00

loc_id timestmp_with_zone AT LOCALGMT 2002-04-15 02:30:00-08:00 PST 2002-04-15 10:30:00-08:00

Method 2: Use the AT LOCAL option which also normalizes the time zone into the timestamp.

SELECT loc_id, CAST(timestmp_with_zone

AS TIMESTAMP(0)) FROM zone_test;

SELECT loc_id, CAST(timestmp_with_zone

AS TIMESTAMP(0)) FROM zone_test;

loc_id timestmp_with_zoneGMT 2002-04-15 02:30:00 PST 2002-04-15 10:30:00

loc_id timestmp_with_zoneGMT 2002-04-15 02:30:00 PST 2002-04-15 10:30:00

Note: A Time or Timestamp is “normalized” when the zone is implicit.

Note: A Time or Timestamp is “normalized” when the zone is implicit.

Method 1: CAST the timezone to a TIMEZONE data type without the explicit time zone. This is called ‘normalizing’ the timestamp.

Note: All times are represented in PST(10:30 GMT = 02:30 PST)(10:30 PST = 10:30 PST)

Note: All times are represented in PST(10:30 GMT = 02:30 PST)(10:30 PST = 10:30 PST)

ANSI Timestamps and Time Zones Page 5-25

Page 206: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Hong Kong Adds a Row We insert a third row into the table, this time with an explicit time zone of +08:00, which is Hong Kong time. In all three cases we are inserting the time of 10:30 in the morning, however, in each case it is in a different time zone. This result is exactly what we see if we do a simple select of the three rows of the table as seen in the example on the facing page. Note, that the work done on this page never relies on the default time zone setting. The insertion of the row uses an explicit time zone, and the display of the rows is also done using explicit time zones without normalization. This row could have been added from any place in the world using this approach.

ANSI Timestamps and Time Zones Page 5-26

Page 207: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Hong Kong Adds a Row

INSERT INTO zone_test /* Hong Kong local time */ ('HKT',TIMESTAMP '2002-04-15 10:30:00+08:00'); INSERT INTO zone_test /* Hong Kong local time */ ('HKT',TIMESTAMP '2002-04-15 10:30:00 );

SELECT loc_id, timestmp_with_zone FROM zone_test;

+08:00'

SELECT loc_id, timestmp_with_zone FROM zone_test;

A Hong Kong person adds a row to the same table at 10:30 a.m. his time. (Remember, in England it is eight hours earlier and in L.A. it is sixteen hours earlier.)Instead of his ‘default’ time zone (+08:00), he uses an explicit time zone for his row.

Now, the Hong Kong person wants to see other rows that have been added. Because he is displaying the rows without ‘normalization, he sees the explicit time zones for each entry.

Note: In this example, when explicit time zones are output, all times are seen as 10:30 AM.Note: In this example, when explicit time zones are output, all times are seen as 10:30 AM.

loc_id timestmp_with_zoneGMT 2002-04-15 10:30:00+00:00 PST 2002-04-15 10:30:00- 08:00 HKT 2002-04-15 10:30:00+08:00

loc_id timestmp_with_zoneGMT 2002-04-15 10:30:00+00:00 PST 2002-04-15 10:30:00- 08:00 HKT 2002-04-15 10:30:00+08:00

Note: Time Zones are shown as explicit10:30 AM in London10:30 AM in L.A.10:30 AM in Hong Kong

Note: Time Zones are shown as explicit10:30 AM in London10:30 AM in L.A.10:30 AM in Hong Kong

(Zone is explicit)(Zone is explicit)

ANSI Timestamps and Time Zones Page 5-27

Page 208: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The View from L.A. If we normalize the times by recasting as TIMESTAMP without zone, we now see the times represented as they would appear on a clock in Los Angeles (PST). Hong Kong, it will be noted, is sixteen hours ahead of Los Angeles. When the time in Hong Kong is normalized to PST, it turns the clock back sixteen hours, effectively putting us into the previous day. This should be interpreted to mean, “When it's 10:30 AM in Hong Kong, its 06:30 PM of the previous day in Los Angeles..

ANSI Timestamps and Time Zones Page 5-28

Page 209: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The View from L.A.

SELECT loc_id, CAST(timestmp_with_zone AS TIMESTAMP(0)) FROM zone_test;SELECT loc_id, CAST(timestmp_with_zone AS TIMESTAMP(0)) FROM zone_test;

Meanwhile, the L.A. person wants to see the inserted rows and their timestamps relative to his own local time. He chooses to do this by the CASTing method.

PST-8.00

GMT00.00

HKT+8:00

2:30 18:3010:30

10:30

10:30

26:30(or 2:30 a.m. next day)

18:30

18:30(or 6:30 p.m. of previous day)

2:30

Insertion time of 1st row

Insertion time of 2nd row

Insertion time of 3rd row

loc_id timestmp_with_zoneGMT 2002 -04-15 02:30:00 PST 2002 -04-15 10:30:00HKT 2002 -04-14 18:30:00

loc_id timestmp_with_zoneGMT 2002 -04-15 02:30:00 PST 2002 -04-15 10:30:00HKT 2002 -04-14 18:30:00

(Times are normalized to PST, the default for L.A.)When it's 10:30 GMT, it's 02:30 PSTWhen it's 10:30 PST, it's 10:30 PSTWhen it's 10:30 in HK, it's 18:30 of previous day PST

(Times are normalized to PST, the default for L.A.)When it's 10:30 GMT, it's 02:30 PSTWhen it's 10:30 PST, it's 10:30 PSTWhen it's 10:30 in HK, it's 18:30 of previous day PST

ANSI Timestamps and Time Zones Page 5-29

Page 210: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The View from London If we next change the session default time zone back to 00:00 (GMT), notice that the times are normalized to how they would appear in London. Because London is only eight hours behind Hong Kong, eight hours are subtracted from 10:30 AM to normalize the time. This can be read as “when it's 10:30 AM in Hong Kong, its 02:30 AM of the same day in London. A second technique for producing normalized output is available with the AT LOCAL option (which has the same effect as the previous casting.) The AT LOCAL option will always use the default time zone setting as the basis for normalizing the time or timestamp columns.

PST -

GMT 00.0

HKT +8:0

2:30 18:310:3

10:3

10:3

26:30 (or 2:30 am next 18:3

18:30 (or 6:30 am of previous day)

2:3

Insertion time of first

Insertion time of second

Insertion time of third

ANSI Timestamps and Time Zones Page 5-30

Page 211: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The View From London

Reminders• For zone-independent processing, use default '00:00'• For zone-dependent processing, set default appropriately • Timestamp and Time normalize default time zone into display• Timestamp and Time WITH TIME ZONE show explicit zone

Reminders• For zone-independent processing, use default '00:00'• For zone-dependent processing, set default appropriately • Timestamp and Time normalize default time zone into display• Timestamp and Time WITH TIME ZONE show explicit zone

SET TIME ZONE LOCAL;SET TIME ZONE LOCAL;

SELECT loc_id, CAST(timestmp_with_zone AS TIMESTAMP(0)) FROM zone_test;SELECT loc_id, CAST(timestmp_with_zone AS TIMESTAMP(0)) FROM zone_test;

loc_id timestmp_with_zoneGMT 2002 -04-15 10:30:00PST 2002 -04-15 18:30:00 HKT 2002 -04-15 02:30:00

loc_id timestmp_with_zoneGMT 2002 -04-15 10:30:00PST 2002 -04-15 18:30:00 HKT 2002 -04-15 02:30:00

Also, the London person wants to see the added rows and their timestamps relative to his own local time. (To simulate his time zone, he sets the default time zone back to GMT.)

SELECT loc_id, timestmp_with_zone AT LOCAL FROM zone_test;

SELECT loc_id, timestmp_with_zone AT LOCAL FROM zone_test;

loc_id timestmp_with_zone AT LOCAL GMT 2002-04-15 10:30:00+00:00 PST 2002-04-15 18:30:00+00:00HKT 2002-04-15 02:30:00+00:00

loc_id timestmp_with_zone AT LOCAL GMT 2002-04-15 10:30:00+00:00 PST 2002-04-15 18:30:00+00:00HKT 2002-04-15 02:30:00+00:00

The same result could have been achieved using the AT LOCAL option.

Times are displayed in normalized GMT10:30 GMT = 10:30 GMT10:30 PST = 18:30 GMT10:30 in HK = 02:30 GMT

Times are displayed in normalized GMT10:30 GMT = 10:30 GMT10:30 PST = 18:30 GMT10:30 in HK = 02:30 GMT

Resets time zone back to 00:00 GMTResets time zone back to 00:00 GMT

ANSI Timestamps and Time Zones Page 5-31

Page 212: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ordering Time Zone Output Finally, we set the session time zone back to PST. We redisplay the rows, normalized to PST, and order by the normalized timestamp. This produces a chronological sequencing of the three rows with the earliest coming first. If we try ordering the rows by non-normalized timestamp, it appears to produce a random sequencing of the rows. In fact, it is ordering by the timestamp independent of the time zone. Because all rows have 10:30:00 as the non-normalized time, the output sequencing is arbitrary.

ANSI Timestamps and Time Zones Page 5-32

Page 213: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ordering Time Zone Output

SELECT loc_Id, CAST(timestmp_with_zone

AS TIMESTAMP(0)) FROM zone_test ORDER BY 2;

SELECT loc_Id, CAST(timestmp_with_zone

AS TIMESTAMP(0)) FROM zone_test ;

loc_id

ORDER BY 2

timestmp_with_zoneHKT 2002-04-14 18:30:00GMT 2002-04-15 02:30:00PST 2002-04-15 10:30:00

loc_id timestmp_with_zoneHKT 2002-04-14 18:30:00GMT 2002-04-15 02:30:00PST 2002-04-15 10:30:00

SELECT loc_id, timestmp_with_zone FROM zone_test O ;RDER BY 2SELECT loc_id, timestmp_with_zone FROM zone_test ;

loc_idORDER BY 2

timestmp_with_zone GMT 2002-04-15 10:30:00+00:00 HKT 2002-04-15 10:30:00+08:00 PST 2002-04-15 10:30:00-08:00

loc_id timestmp_with_zone GMT 2002-04-15 10:30:00+00:00 HKT 2002-04-15 10:30:00+08:00 PST 2002-04-15 10:30:00-08:00

SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE;SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE;

The L.A. person wants to see the inserted rows ordered by timestamp. (To simulate his time zone, we will set the default time zone back to PST.)

In this case, the order is based on normalized PST time.In this case, the order is based on normalized PST time.

In the non-normalized case, the order is based on the explicit timestamp, independent of the time zone. Because all times are 10:30 AM, the order is random.

In the non-normalized case, the order is based on the explicit timestamp, independent of the time zone. Because all times are 10:30 AM, the order is random.

(Set to PST)(Set to PST)

ANSI Timestamps and Time Zones Page 5-33

Page 214: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extracting from Timestamp with Zone The EXTRACT function also may be used to extract time zone hour and time zone minute from a TIME or TIMESTAMP WITH ZONE data type. The extracted time zone will be the zone explicitly stored with the row. It is possible to extract other portions of the timestamp which might be affected by normalization. Using the second example on the facing page, extracting the days from the non-normalized timestamp shows all days to be the 15th of the month. But in the last example which extracts the days from the PST normalized timestamp, two are on the 15th and one (Hong Kong) is on the 14th.

ANSI Timestamps and Time Zones Page 5-34

Page 215: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extracting from Timestamp with Zone

EXTRACTYEARMONTHDAYHOURMINUTESECONDTIMEZONE_HOURTIMEZONE_MINUTE

FROM (TIMESTAMP

WITH ZONE)

SELECT EXTRACT(timezone_hour FROM timestmp_with_zone)

FROM zone_test;

SELECT EXTRACT(timezone_hour FROM timestmp_with_zone)

FROM zone_test; EXTRACT(ZONE HOUR FROM timestmp_with_zone)

-8 0 8

EXTRACT(ZONE HOUR FROM timestmp_with_zone)-8 0 8

SELECT EXTRACT(DAY FROM timestmp_with_zone)

FROM zone_test;w

SELECT EXTRACT(DAY FROM timestmp_with_zone)

FROM zone_test;w EXTRACT(DAY FROM timestmp_with_zone)15 15 15

EXTRACT(DAY FROM timestmp_with_zone)15 15 15

SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE;SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE;

SELECT CAST(timestmp_with_zone AS TIMESTAMP) AS xyz ,EXTRACT(DAY FROM xyz)

FROM zone_test;

SELECT CAST(timestmp_with_zone AS TIMESTAMP) AS xyz ,EXTRACT(DAY FROM xyz)

FROM zone_test; xyz EXTRACT(DAY FROM timestmp_with_zone)2002-04-15 10:30:00.000000 15 2002-04-14 18:30:00.000000 14 2002-04-15 02:30:00.000000 15

xyz EXTRACT(DAY FROM timestmp_with_zone)2002-04-15 10:30:00.000000 15 2002-04-14 18:30:00.000000 14 2002-04-15 02:30:00.000000 15

ANSI Timestamps and Time Zones Page 5-35

Page 216: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extracting from Timestamp (without Zone) EXTRACT used with a TIMESTAMP without zone data type permits extracting any one of the six elements of a timestamp. The extraction may also be done from a calculation which results in a timestamp, such as the addition or subtraction of an interval from a timestamp. Zones do not factor into this case.

ANSI Timestamps and Time Zones Page 5-36

Page 217: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extracting from Timestamp (without Zone)

SELECT TIMESTAMP '2003-12-15 09:45:20' + INTERVAL '05 02:10'

DAY TO MINUTE;

SELECT TIMESTAMP '2003-12-15 09:45:20' + INTERVAL '05 02:10'

DAY TO MINUTE;

2003-12-20 11:55:20 2003-12-20 11:55:20

SELECT EXTRACT (DAY FROM (TIMESTAMP '2003-12-15 09:45:20'

+ INTERVAL '05 02:10' DAY TO MINUTE));

SELECT EXTRACT (DAY FROM (TIMESTAMP '2003-12-15 09:45:20'

+ INTERVAL '05 02:10' DAY TO MINUTE));

20 20

SELECT EXTRACT (MINUTE FROM (TIMESTAMP '2003-12-15 09:45:20'

+ INTERVAL '05 02:10' DAY TO MINUTE));

SELECT EXTRACT (MINUTE FROM (TIMESTAMP '2003-12-15 09:45:20'

+ INTERVAL '05 02:10' DAY TO MINUTE));

5555

EXTRACT

YEARMONTHDAYHOURMINUTESECOND

FROM (TIMESTAMP)

Add 5 days, 2 hrs and 10 mins to the specified timestamp:

Extract the day from the resulting timestamp:

Extract the minute from the resulting timestamp:

ANSI Timestamps and Time Zones Page 5-37

Page 218: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Overlaps Function The OVERLAPS function is an ANSI standard function which allows testing for overlapping intervals of time. There are a few important rules to keep in mind regarding OVERLAPS. For the function to report a “True” result, the intervals must share more than a single point in time. For example having two intervals start or end on the same day or time period will not produce a “True” overlap result. Secondly, the order in which the elements of a time interval are specified is irrelevant. If they are not properly specified as lowest preceding highest, the SQL parser will automatically resequence them prior to the test. Thirdly, if one element of an interval is a null, that interval is treated as a single value which must fall within the range of the second interval for an overlap condition to occur. If both intervals have a null, an overlap condition is not possible.

ANSI Timestamps and Time Zones Page 5-38

Page 219: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Overlaps FunctionThe OVERLAPS function tests whether there is an overlap between two time periods.

SELECT 'They overlap' WHERE(DATE '2001-01-01', DATE '2001-11-30') OVERLAPS(DATE '2001-10-15', DATE '2001-12-31');

SELECT 'They overlap' WHERE(DATE '2001-01-01', DATE '2001-11-30') OVERLAPS(DATE '2001-10-15', DATE '2001-12-31');

They overlap. They overlap.

SELECT 'They overlap' WHERE(DATE '2001-01-01', DATE '2001-10-15') OVERLAPS(DATE '2001-10-15', DATE '2001-12-31');

SELECT 'They overlap' WHERE(DATE '2001-01-01', DATE '2001-10-15') OVERLAPS(DATE '2001-10-15', DATE '2001-12-31');

*** Query completed. No rows found. *** Query completed. No rows found.

(Note: Sharing a single common point does not qualify as an overlap)

SELECT 'They overlap' WHERE(TIME '10:00:00', TIME '01:00:00') OVERLAPS(TIME '01:01:00', TIME '03:00:00');

SELECT 'They overlap' WHERE(TIME '10:00:00', TIME '01:00:00') OVERLAPS(TIME '01:01:00', TIME '03:00:00');

They overlap. They overlap.

(The following is an equivalent query)

SELECT 'They overlap' WHERE(TIME '01:00:00', TIME '10:00:00') OVERLAPS(TIME '01:01:00', TIME '03:00:00');

SELECT 'They overlap' WHERE(TIME '01:00:00', TIME '10:00:00') OVERLAPS(TIME '01:01:00', TIME '03:00:00');

They overlap. They overlap.

SELECT 'They overlap' WHERE(TIME '01:00:00', NULL) OVERLAPS(TIME '01:01:00', TIME '03:00:00');

SELECT 'They overlap' WHERE(TIME '01:00:00', NULL) OVERLAPS(TIME '01:01:00', TIME '03:00:00');

*** Query completed. No rows found. *** Query completed. No rows found.

(Note: When one side of interval is null, test if other side is within range)

(Note: Each interval is automatically sequenced putting earliest first.)

ANSI Timestamps and Time Zones Page 5-39

Page 220: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Overlapping Data Type Options Testing for an overlap condition is possible only with certain data types in certain combinations. An interval made up of two dates, two times or two timestamps, may be compared only with the exact same type of interval. Similarly, a date plus an interval, a time plus an interval or a timestamp plus an interval, may also only be compared with a similarly defined time period. When testing for overlap on a TIMESTAMP data type, a time overlap is all that is required to return an overlap condition.

ANSI Timestamps and Time Zones Page 5-40

Page 221: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Overlapping Data Type OptionsDate1, Date2Time1, Time2Timestamp1, Timestamp2Date1, Interval Time1, IntervalTimestamp1, Interval

OVERLAPS (Compatible interval)

SELECT 'They overlap' WHERE(TIMESTAMP '2001-10-15 01:00:00',TIMESTAMP'2001-10-15 02:00:00') OVERLAPS(TIMESTAMP '2001-10-15 01:01:00', TIMESTAMP '2001-10-15 03:00:00');

SELECT 'They overlap' WHERE(TIMESTAMP '2001-10-15 01:00:00',TIMESTAMP'2001-10-15 02:00:00') OVERLAPS(TIMESTAMP '2001-10-15 01:01:00', TIMESTAMP '2001-10-15 03:00:00');

They overlap. They overlap.

Note: Timestamp overlap requires only time overlap.

SELECT 'They overlap'WHERE (CURRENT_TIME, INTERVAL '1' HOUR) OVERLAPS

(CURRENT_TIME, INTERVAL -'1' HOUR);

SELECT 'They overlap'WHERE (CURRENT_TIME, INTERVAL '1' HOUR) OVERLAPS

(CURRENT_TIME, INTERVAL -'1' HOUR); *** Query completed. No rows found. *** Query completed. No rows found.

Note:Common point does not overlap.

SELECT 'They overlap'WHERE (CURRENT_TIME, CURRENT_TIME + INTERVAL '1' HOUR) OVERLAPS

(CURRENT_TIME, CURRENT_TIME - INTERVAL '1' HOUR);

SELECT 'They overlap'WHERE (CURRENT_TIME, CURRENT_TIME + INTERVAL '1' HOUR) OVERLAPS

(CURRENT_TIME, CURRENT_TIME - INTERVAL '1' HOUR); *** Query completed. No rows found. *** Query completed. No rows found. Note: Same as previous query.

SELECT 'They overlap‘WHERE (CURRENT_TIME, INTERVAL '1' HOUR) OVERLAPS

(CURRENT_TIME + INTERVAL '1' SECOND, INTERVAL -'1' HOUR);

SELECT 'They overlap‘WHERE (CURRENT_TIME, INTERVAL '1' HOUR) OVERLAPS

(CURRENT_TIME + INTERVAL '1' SECOND, INTERVAL -'1' HOUR);Note: They overlap by one second.

They overlap They overlap

Same query, different coding style.

ANSI Timestamps and Time Zones Page 5-41

Page 222: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercises

ANSI Timestamps and Time Zones Page 5-42

Page 223: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercises

a.) TIMESTAMP INTERVAL

b.) TIMESTAMP - TIMESTAMP

c.) CAST

d.) CREATE/MODIFY USER

e.) SET TIME ZONE

f.) EXTRACT

g.) HELP SESSION

h.) TIMESTAMP

i.) TIMESTAMP WITH TIME ZONE

j.) OVERLAPS

1.) Do two time intervals include the same date?___

2.) How many hours passed since this timestamp was taken?____

3.) What is the default time zone setting for this session?____

4.) Show me the timestamps with zone implicit in the time.____

5.) What will be the date in 1000 hours?____

6.) Set the time zone to be EST.____

7.) Show me the local time and time zone for each entry.____

8.) Display a list of all time zones for which we have activity.____

Match the appropriate technique from the lettered list with the appropriate question from the numbered list. Some questions may have more than one answer.

ANSI Timestamps and Time Zones Page 5-43

Page 224: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2) Lab exercises #5 through 10 on the facing page are designed for the student looking for an application-driven usage of the timestamp feature. The Repair_time table contains a subset of a larger table containing the repair times for a variety of different products, including televisions, VCRs, camcorders, etc. The table extract shows repair times for televisions only and only for the first few weeks of the year. The start and stop times reflect how long the unit was in the repair shop, not how long the technician actually worked on it.

CREATE TABLE Repair_time ( serial_number INTEGER

,product_desc CHAR(8) ,start_time TIMESTAMP(0) ,end_time TIMESTAMP(0)) UNIQUE PRIMARY INDEX (serial_number);

serial_number product_desc start_time end_time -------------------- ----------------- ---------------------------- ---------------------------- 100 TV 2000-01-15 10:30:00 2000-01-17 13:20:00 101 TV 2000-01-20 08:30:00 2000-01-23 12:20:00 102 TV 2000-01-25 13:40:00 2000-01-26 14:20:00 103 TV 2000-02-02 11:30:00 2000-02-09 08:50:00 104 TV 2000-02-07 09:00:00 2000-02-10 08:50:00 105 TV 2000-02-10 08:40:00 2000-02-12 14:50:00 106 TV 2000-02-15 12:30:00 2000-02-20 15:20:00 107 TV 2000-02-19 14:30:00 2000-02-21 10:50:00

108 TV 2000 02 21 11 30 00 2000 02 23 16 40 00

SELECT * FROM Repair_time ORDER BY 1;

ANSI Timestamps and Time Zones Page 5-44

Page 225: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)

Timestamp Lab Exercises1.) Assume current timestamp is '2000-06-18 12:30:25'. Subtract 30 days from this timestamp. 2.) Subtract one month from this timestamp. 3.) Show the elapsed time between the current timestamp (2003-06-18 12:30:25) and a past timestamp

(2002-12-25 01:01:01):In years?In months?In days?In hours?In days and hours?

4.) Does 1440 hours after the zero hour of January 1, 2000 take us into Febrary or March? In a separate query, determine which month 1439 hours returns.

5.) Using the Repair_time table described on the left page, report a count of how many TV set repairs were completed in February?

6.) Produce a report ordering each TV by serial number and how long in days, hours and minutes it took to repair the TV?

7.) What is the average amount of time it takes to repair a TV? Show the answer in days, hours and minutes.

8.) Show the serial number and the number of days for each TV that took longer than 2 days to repair.

Optional Labs

9.) Produce a list which pairs by serial number any two TV's that were being repaired at the same time.10.) What percentage of all TV's took more than 2 days to repair? (Note: The solutions for lab 10 uses a ‘derived table’ which is covered in the next module.)

ANSI Timestamps and Time Zones Page 5-45

Page 226: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

ANSI Timestamps and Time Zones Page 5-46

Page 227: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

Zone Lab Exercises1.) If you are working in BTEQ, create these macros:

2.) Set the session default time zone to PST (-08:00).Invoke the seton macro (BTEQ only).Verify that you time zone is set to -08:00Invoke the setoff macro (BTEQ only).

3.) Create this table in your own database:

4.) Insert these rows into the zone table:

5.) Select all rows from the table. Order results by the timestamp column. How do the results appear to be sorted?

6.) Select the rows of the table again, however, this time cast the timestamp column as a timestamp(0) with no zone. Sort by the recast timestamp column. How do the results appear to be sorted?

7.) Generate the same result as the previous query, but use the AT LOCAL option on the timestamp, instead of casting it.

8.) Reset the local session time zone to 00:00.9.) Verify the new session default time zone.10.) Resubmit the previous query against the zone table. How are results sorted?

CREATE MACRO seton AS (ECHO '.SET FOLDLINE ON;'; ECHO '.SET SIDETITLES ON;';);

CREATE MACRO setoff AS (ECHO '.SET FOLDLINE OFF;'; ECHO '.SET SIDETITLES OFF;';);

CREATE MACRO seton AS (ECHO '.SET FOLDLINE ON;'; ECHO '.SET SIDETITLES ON;';);

CREATE MACRO setoff AS (ECHO '.SET FOLDLINE OFF;'; ECHO '.SET SIDETITLES OFF;';);

Loc-id Timestamp LA 2000-01-01 09:00:00 (default time zone)NY 2000-01-01 09:00:00-05:00LON 2000-01-01 09:00:00+00:00SYD 2000-01-01 09:00:00+10:00LA 2000-01-01 09:30:00-08:00LON 2000-01-01 09:30:00-00:00

Loc-id Timestamp LA 2000-01-01 09:00:00 (default time zone)NY 2000-01-01 09:00:00-05:00LON 2000-01-01 09:00:00+00:00SYD 2000-01-01 09:00:00+10:00LA 2000-01-01 09:30:00-08:00LON 2000-01-01 09:30:00-00:00

CREATE TABLE zone_tbl(loc_id CHAR(3),timestmp_wz TIMESTAMP(0) WITH TIME ZONE);

CREATE TABLE zone_tbl(loc_id CHAR(3),timestmp_wz TIMESTAMP(0) WITH TIME ZONE);

ANSI Timestamps and Time Zones Page 5-47

Page 228: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

ANSI Timestamps and Time Zones Page 5-48

Page 229: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 6

Module 6Enhanced DATE and TIME Formatting Options

After completing this module, you should be able to:

• Apply appropriate enhanced formatting options to TIME, DATE and TIMESTAMP data types.

Enhanced Date/Time Formtting Options Page 6-1

Page 230: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Enhanced Date/Time Formtting Options Page 6-2

Page 231: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Enhanced Date/Time Formtting Options Page 6-3

Page 232: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Year, Month and Day Options Additional Date/Time display conventions are available which permit the format to be specified with user-defined formatting characters. They are defined in a file called the SDF file (Specification for Data Formatting). This file is maintained using the tdlocaldef utility.

Enhanced Date/Time Formtting Options Page 6-4

Page 233: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Year, Month and Day Options

Y4: 4-Digit yearYY: 2-Digit yearM4: Long Month - i.e. fully defined name for month M3: Short Month - i.e., abbreviated name for monthMM: 2-Digit month DD: 2-Digit day of monthD3: 3-Digit day of year (Julian date)

Y4: 4-Digit yearYY: 2-Digit yearM4: Long Month - i.e. fully defined name for month M3: Short Month - i.e., abbreviated name for monthMM: 2-Digit month DD: 2-Digit day of monthD3: 3-Digit day of year (Julian date)

The following SDF conventions are available for date formatting:

Note: Date formatting options may also use expanded syntax (YYYY,MMM,DD).Note: Date formatting options may also use expanded syntax (YYYY,MMM,DD).

Example 1

SELECT CURRENT_DATE (FORMAT 'Y4-M3-DD');SELECT CURRENT_DATE (FORMAT 'Y4-M3-DD'); Date2002-Nov-04Date2002-Nov-04

Example 2

SELECT CURRENT_DATE (FORMAT 'Y4:M4:D3');SELECT CURRENT_DATE (FORMAT 'Y4:M4:D3'); Date2002:November:308

Note: The date is the 308th day of the year 2002.Note: The date is the 308th day of the year 2002.

Enhanced Date/Time Formtting Options Page 6-5

Page 234: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Day of Week Format Options Day of the week may be specified in the SDF file in both a long and short form.

Enhanced Date/Time Formtting Options Page 6-6

Page 235: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Day of Week Format Options

EEEE: Long Day - i.e. fully defined name for day of week.EEE: Short Day- i.e., abbreviated name for day of week.EEEE: Long Day - i.e. fully defined name for day of week.EEE: Short Day- i.e., abbreviated name for day of week.

Example 1

DateMon, November 04, 2002DateMon, November 04, 2002

Note: B represents blanks.Note: B represents blanks.

Example 2

DateMonday, November 04, 2002DateMonday

'EEE

, November 04, 2002

SELECT CURRENT_DATE (FORMAT ,BM4BDD,BY4');SELECT CURRENT_DATE (FORMAT ' ,BM4BDD,BY4');

SELECT CURRENT_DATE (FORMAT ,BM4BDD,BY4');

EEE

'E4SELECT CURRENT_DATE (FORMAT ' ,BM4BDD,BY4');E4

Enhanced Date/Time Formtting Options Page 6-7

Page 236: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Hours, Minutes and Seconds Options Time may be specified in either a 12 or 24 hour format.

Enhanced Date/Time Formtting Options Page 6-8

Page 237: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Hours, Minutes and Seconds Options

The following conventions are available for Time formatting:

HH - 2-Digit hoursMI- 2-Digit minutesSS - 2-Digit seconds

HH - 2-Digit hoursMI- 2-Digit minutesSS - 2-Digit seconds

Display the current time without formatting.

SELECT CURRENT_TIME;SELECT CURRENT_TIME; Current Time(0)15:08:37+00:00

Current Time(0)15:08:37+00:00

CURRENT_TIME displays time zone by default.CURRENT_TIME displays time zone by default.

Display the current time using explicit time formatting.Current Time(0)

15:08:37Current Time(0)

15:08:37

T indicates to represent time in a 12-hour, rather than a 24 hour format and adds an AM/PM designator.T indicates to represent time in a 12-hour, rather than a 24 hour format and adds an AM/PM designator.

Represent current time using a 12-hour clock.Current Time(0)

03 PM:08:37 Current Time(0)

03 PM:08:37

SELECT CURRENT_TIME (FORMAT 'HH:MI:SS');SELECT CURRENT_TIME (FORMAT 'HH:MI:SS');

SELECT CURRENT_TIME (FORMAT 'HH:MI:SSB ');TSELECT CURRENT_TIME (FORMAT 'HH:MI:SSB ');T

Enhanced Date/Time Formtting Options Page 6-9

Page 238: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

TIME Precision Formatting D: Represents the RADIX symbol as defined in the SDF. Radix separates the whole number from the fractional part. In U.S. applications, this is usually a period (.). SDF symbols may not be mixed in the same FORMAT string with explicit FORMAT designations. For example, you cannot have 'D' and an explicit '.' in the same FORMAT S(n) represents the fractional number of seconds carried to n places. S(n) must always be preceded by a D designator. F: Represents the number of characters needed to display the fraction portion of numeric data. In the case of a DEC(10,2) data type, 9(F) is equivalent to 9(2). Current_time is always displayed as a Time(0) output, with specified formatting added.

Enhanced Date/Time Formtting Options Page 6-10

Page 239: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

TIME Precision Formatting

This is the character which separates the integer portion from the fraction. For American applications, this is usually a period.

S(n)S(n) D

represents the fractional number of seconds to n places.must always be preceded by a designator.

S(n)S(n) D

represents the fractional number of seconds to n places.must always be preceded by a designator.

Convert current time to a TIME(2) data type and use explicit radix and precision seconds to 2 places.

Current Time(0)14:56:23.00

Current Time(0)14:56:23.

Repeat the conversion but use F to represent fractional secondsCurrent Time(0)

00

15:00:00.00Current Time(0)

15:00:00.

is for the number of implied decimal places needed.

00

FF

D

is for the number of implied decimal places needed.

represents the 'radix symbol' for output.D represents the 'radix symbol' for output.

Current Time(6)14:41:14.000000Current Time(6)14:41:14.

The convention converts to any level of precision without adjusting the format.

000000

FThe convention converts to any level of precision without adjusting the format.

SELECT CURRENT_TIME (TIME(2)) (FORMAT 'HH:MI:SS ');

F

DS(2)SELECT CURRENT_TIME (TIME(2)) (FORMAT 'HH:MI:SS ');

SELECT CURRENT_TIME (TIME(2)) (FORMAT 'HH:MI:SS ');

DS(2)

DS(F)SELECT CURRENT_TIME (TIME(2)) (FORMAT 'HH:MI:SS ');

SELECT CURRENT_TIME (TIME(6)) (FORMAT 'HH:MI:SSD ');

DS(F)

S(F)SELECT CURRENT_TIME (TIME(6)) (FORMAT 'HH:MI:SSD ');S(F)

Enhanced Date/Time Formtting Options Page 6-11

Page 240: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Time Zone Placement Time Zone placement can be controlled by the FORMAT clause. Z controls the placement of the time zone in the output. Z can only appear at the beginning or at the end of the formatting string. Lowercase h, m, and s may be used as formatting choices. They are copied directly as literals to the output string.

Enhanced Date/Time Formtting Options Page 6-12

Page 241: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Time Zone Placement

Time Zone placement can be controlled by the FORMAT clause.

Z cZ can

ontrols the placement of the time zone in the output.only appear at the beginning or at the end of the formatting string.

Z cZ can

ontrols the placement of the time zone in the output.only appear at the beginning or at the end of the formatting string.

Convert Current Time to a TIME(2) data type and use explicit radix and precision seconds to 2 places.

Current Time(0)02:44:50 PM +00:00Current Time(0)02:44:50 PM

Move Time Zone to the front of the string.Current Time(0)

+00:00

+00:00 12:57:29 PMCurrent Time(0)+00:00 12:57:29 PM

Use lowercase time designators in the output.Current Time(0)+00:00 03 09 08 PMh m sCurrent Time(0)

+00:00 03 09 08 PM

SELECT CURRENT_TIME (FORMAT 'HH:MI:SSBTB ');

h m s

ZSELECT CURRENT_TIME (FORMAT 'HH:MI:SSBTB ');

SELECT CURRENT_TIME (FORMAT ' BHH:MI:SSBT');

Z

ZSELECT CURRENT_TIME (FORMAT ' BHH:MI:SSBT');

SELECT CURRENT_TIME(FORMAT 'ZBHHhMImSSsBT');

Z

SELECT CURRENT_TIME(FORMAT 'ZBHHhMImSSsBT');

Enhanced Date/Time Formtting Options Page 6-13

Page 242: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Timestamp Formatting (1 of 2) The examples seen here demonstrate usage of the previously discussed techniques for use in formatting a TIMESTAMP data type.

Enhanced Date/Time Formtting Options Page 6-14

Page 243: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Timestamp Formatting (1 of 2)

The formatting options for TIME and DATE also may be applied to TIMESTAMP.

Display the current timestamp without explicit formatting.

SELECT CURRENT_TIMESTAMP;SELECT CURRENT_TIMESTAMP; Current TimeStamp(6)2002-11-04 15:17:54.560000+00:00Current TimeStamp(6)2002-11-04 15:17:54.560000+00:00

Defalt timestamp format displays the time zone.Default time portion of Current_Timestamp is TIME(6).Defalt timestamp format displays the time zone.Default time portion of Current_Timestamp is TIME(6).

Display the current timestamp using the 12-hour format, long month and long day.SELECT CURRENT_TIMESTAMP (FORMAT 'E4,BM4BDD,Y4BHH:MI:SSBT');SELECT CURRENT_TIMESTAMP (FORMAT ' ,BM4BDD,Y4BHH:MI:SSBT');

Current TimeStamp(6)E4 Monday, November 04,2002 03:24:22 PM

Current TimeStamp(6)Monday

Z

, November 04,2002 03:24:22 PM

Display the current timestamp in European style sequence, showing the time zone and using the 12-hour format.SELECT CURRENT_TIMESTAMP(FORMAT 'DD.MM.Y4BHH:MI:SSBTB ');SELECT CURRENT_TIMESTAMP(FORMAT 'DD.MM.Y4BHH:MI:SSBTB ');

Current TimeStamp(6)Z 04.11.2002 03:16:56 PM +00:00

Current TimeStamp(6)04.11.2002 03:16:56 PM

SELECT CURRENT_TIMESTAMP (FORMAT ,BM4BDD,Y4BHH:MI:SSBT');

+00:00

'E4SELECT CURRENT_TIMESTAMP (FORMAT ' ,BM4BDD,Y4BHH:MI:SSBT');E4

Enhanced Date/Time Formtting Options Page 6-15

Page 244: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Timestamp Formatting (2 of 2) Here are some additional techniques for use in formatting a TIMESTAMP data type.

Enhanced Date/Time Formtting Options Page 6-16

Page 245: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Timestamp Formatting (2 of 2)

Display the current timestamp using the default fractional format.

SELECT CURRENT_TIMESTAMP (FORMAT 'DD.MM.Y4BHH:MI:SSDS(F)');SELECT CURRENT_TIMESTAMP (FORMAT 'DD.MM.Y4BHH:MI:SSD ');

Current TimeStamp(6)

S(F)

04.11.2002 15:18:48.900000Current TimeStamp(6)04.11.2002 15:18:48.

Display the current timestamp using all long formats, 12-hour time all uppercase.

SELECT CURRENT_TIMESTAMP (FORMAT ,BM4BDD,Y4BHH:MI:SSBT') (CHAR(40), UC);

900000

'E4SELECT CURRENT_TIMESTAMP (FORMAT' ,BM4BDD,Y4BHH:MI:SSBT') (CHAR(40), UC);

Current TimeStamp(6)

E4

MONDAY, NOVEMBER 04,2002 03:25:59 PMCurrent TimeStamp(6)MONDAY, NOVEMBER 04,2002 03:25:59 PM

• The UC converts all characters in the output string to uppercase.• The conversion to CHAR(40) is necessary because UC must be applied to an

explicit character string.

• The UC converts all characters in the output string to uppercase.• The conversion to CHAR(40) is necessary because UC must be applied to an

explicit character string.

Enhanced Date/Time Formtting Options Page 6-17

Page 246: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary The following is a summary of SDF TIME and TIMESTAMP formatting options. Y4: 4-Digit year

YY - 2-Digit year M4 - 'Long Month' - i.e. fully defined name for month (SDF defined). M3 - 'Short Month - i.e., abbreviated name for month (SDF defined). MM - 2-digit month DD - 2-Digit day of month D3 - 3-Digit day of year (Julian date)

EEEE: 'Long Day' - i.e. fully defined name for day of week (SDF defined).

EEE - 'Short Day- i.e., abbreviated name for day of week (SDF defined). HH: 2-Digit hours

MI- 2-Digit minutes SS - 2-digit seconds

H: Outputs a lowercase 'h' as part of TIME string result.

m - Outputs a lowercase 'm' as part of TIME string result. s - Outputs a lowercase 's' as part of TIME string result.

D: The 'radix symbol' for output. This is the character which separates the integer portion

from the fraction. For American applications, this is usually a period. S(n): The fractional number of seconds carried to n places. S(n) must always be preceded by a

D designator. T: Indicates to represent time in a 12-hour, rather than a 24 hour format.

This adds an appropriate AM/PM designator (as defined in tdlocaldef) to the output. Z: Controls the placement of the time zone in the output.

Z can only appear at the beginning or at the end of the formatting string.

Enhanced Date/Time Formtting Options Page 6-18

Page 247: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary

SDF TIME and TIMESTAMP formatting options:Y4: 4-digit yearYY: 2-digit yearM4: 'Long Month' M3: 'Short Month'MM: 2-digit month DD: 2-digit day of monthD3: 3-digit day of year (Julian date)

H: Outputs a lowercase 'h' as part of TIME string result.M: Outputs a lowercase 'm' as part of TIME string result.S: Outputs a lowercase 's' as part of TIME string result.

D: The 'radix symbol' for output. This is the character which separates the integerportion from the fraction.

S(n): The fractional number of seconds carried to n places. S(n) must always be preceded by a D designator.

T: Indicates to represent time in a 12-hour and adds an appropriate AM/PM designator.

Z: Controls the placement of the time zone in the output. It can only appear at the beginning or end of the formatting string.

EEEE: 'Long Day' EEE: 'Short Day

HH: 2-digit hoursMI: 2-digit minutesSS: 2-digit seconds

Enhanced Date/Time Formtting Options Page 6-19

Page 248: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise For each function listed on the facing page, pick two appropriate coding choices, one each from List 1 and List 2.

Enhanced Date/Time Formtting Options Page 6-20

Page 249: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise

Using the formatting options seen on the Summary page, create the FORMAT string which would result in a timestamp being output as seen here:

(Note: a maximum of 30 characters may be used in a format string.)

TUESDAY, JANUARY 20,2004 05:45:57.000000 PM +00:00

Enhanced Date/Time Formtting Options Page 6-21

Page 250: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab The facing page describes the tasks for your lab exercise.

Enhanced Date/Time Formtting Options Page 6-22

Page 251: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

LabApply the appropriate format to the CURRENT_TIMESTAMP to produce the formatting results displayed. Because current timestamp is used, answer sets values will vary.

(We recommended that you do these labs in BTEQ or BTEQWin. If an ODBC client such as SQL Assistant is used, the projected column must be CAST to CHARACTER with the correct precision, which may require some trial and error.)

a.) 2003-Apr-01 13:55:18

b.) 03-Apr-01 14:33:01.690000

c.) 14:19:59 +00:00

d.) Tuesday, 01 Apr, 2003

e.) 02:23:49 PM +00:00

f.) +00:00 02h45m28s PM

Enhanced Date/Time Formtting Options Page 6-23

Page 252: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Enhanced Date/Time Formtting Options Page 6-24

Page 253: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 7

Module 7 Correlated Subqueries and Derived Tables

After completing this module, you should be able to:

• Create a correlated subquery.

• Specify a derived table in a SELECT statement.

• Employ aggregate values as part of a WHERE clause condition.

Correlated Subqueries and Derived Tables Page 7-1

Page 254: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Correlated Subqueries and Derived Tables Page 7-2

Page 255: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Correlated Subqueries and Derived Tables Page 7-3

Page 256: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Traditional Subqueries Traditional subqueries are read from the bottom up. The lowest level query is resolved and the results are fed to the next level of query. An SQL statement may reference up to 64 tables or views, thus there may be up to 64 levels of subquery. For pre-V2R4.1 releases, the maximum number of levels is 16.

Correlated Subqueries and Derived Tables Page 7-4

Page 257: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Traditional Subqueries

SELECT last_nameFROM employeeWHERE department_number IN

(SELECT department_numberFROM departmentWHERE department_name LIKE ('%research%'));

SELECT last_nameFROM employeeWHERE department_number IN

(SELECT department_numberFROM departmentWHERE department_name LIKE ('%research%'));

Find all employees who work in any Research department.

last_nameSteinKanieski Kubic

last_nameSteinKanieski Kubic

Subquery processing:• Resolve bottom level query first• Use results as input to next level• Repeat until top level reached

Subquery processing:• Resolve bottom level query first• Use results as input to next level• Repeat until top level reached

Correlated Subqueries and Derived Tables Page 7-5

Page 258: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Correlated Subqueries Correlated subqueries are processed differently from traditional subqueries. Each row of the outer query is selected and is compared to the criteria in the subquery. Usually, correlated subqueries involve aggregates, although they may involve computed columns and other expressions. Correlated subqueries expand the functionality of traditional subqueries. The Teradata implementation of correlated subqueries uses a ‘shared spool’ technique which makes them particularly fast and efficient. Performance Note: The definition of a correlated subquery and its logical flow as seen on the facing page should be viewed as a logical processing description only. Because the Teradata optimizer has ‘query rewrite’ capabilities, it is able to implement correlated subqueries as joins which are much more efficient than the logic seen here.

Correlated Subqueries and Derived Tables Page 7-6

Page 259: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Correlated Subqueries

SELECT last_name,department_number AS deptno,salary_amount

FROM employee eeWHERE salary_amount =

(SELECT MAX (salary_amount)FROM employee emWHERE ee.department_number =

em.department_number);

last_name deptno salary_amountRunyon 501 66000.00Rogers 401 46000.00Trainer 100 100000.00Villegas 403 49700.00Daly 402 52500.00Morrissey 201 38750.00Kubic 301 57700.00Rogers 302 56500.00

This is a subquery whose outer query results are processed a row at a time, against the subquery result which is analyzed for each row processed.

How the following correlated subquery executes: 1. Read an employee row.2. Get max salary for his/her department from the subquery3. Compare his/her salary to the department max salary.4. If equal, output this row.5. Go to Step 1.

Find the employee with the highest salary in each department

Correlated Subqueries and Derived Tables Page 7-7

Page 260: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Another Correlated Subquery The example on the facing page shows a typical use of a correlated subquery. While correlated subqueries add substantial functionality to Teradata SQL, they do have limitations. Note that it would not be possible within this query to add a column showing each department’s average salary. Later we will see how derived tables allow us to do this.

Correlated Subqueries and Derived Tables Page 7-8

Page 261: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Another Correlated Subquery

SELECT last_name,salary_amount (FORMAT '$$$,$99.99')

FROM employee eeWHERE salary_amount >

(SELECT AVG (salary_amount)FROM employee emWHERE ee.department_number

= em.department_number);

SELECT last_name,salary_amount (FORMAT '$$$,$99.99')

FROM employee eeWHERE salary_amount >

(SELECT AVG (salary_amount)FROM employee emWHERE ee.department_number

= em.department_number);

Find all employees whose salary is greater than the department average salary.

last_name salary_amount Trader $37,850.00Runyon $66,000.00 Wilson $53,625.00 Rogers $46,000.00 Brown $43,700.00 Charles $39,500.00 Johnson $36,300.00Villegas $49,700.00 Brown $43,100.00Ratzlaff $54,000.00Kubic $57,700.00 Daly $52,500.00 Morrissey $38,750.00

last_name salary_amount Trader $37,850.00Runyon $66,000.00 Wilson $53,625.00 Rogers $46,000.00 Brown $43,700.00 Charles $39,500.00 Johnson $36,300.00Villegas $49,700.00 Brown $43,100.00Ratzlaff $54,000.00Kubic $57,700.00 Daly $52,500.00 Morrissey $38,750.00

Would it be possible to add a column to this report showing the average department salary being used for the comparison?

Would it be possible to add a column to this report showing the average department salary being used for the comparison?

Correlated Subqueries and Derived Tables Page 7-9

Page 262: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Correlated Subqueries and Joins Joins may be used in conjunction with correlated subqueries as seen in the facing example. A correlated subquery usually references two tables as does a join. In this case, both the employee and department tables are referenced in the join (outer query) and also in the correlated subquery (inner query).

Correlated Subqueries and Derived Tables Page 7-10

Page 263: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Correlated Subqueries and Joins

SELECT d.manager_employee_number AS mgr_emp_#,d.department_number,e.salary_amount

FROM department d INNER JOINemployee e

ON e.employee_number =d.manager_employee_number

WHERE e.salary_amount =(SELECT MAX (salary_amount)FROM employee emWHERE d.department_number

= em.department_number);

SELECT d.manager_employee_number AS mgr_emp_#,d.department_number,e.salary_amount

FROM department d INNER JOINemployee e

ON e.employee_number =d.manager_employee_number

WHERE e.salary_amount =(SELECT MAX (salary_amount)FROM employee emWHERE d.department_number

= em.department_number);

mgr_emp_# dept_num salary_amount1019 301 57700.001017 501 66000.001011 402 52500.001016 302 56500.00801 100 100000.00

mgr_emp_# dept_num salary_amount1019 301 57700.001017 501 66000.001011 402 52500.001016 302 56500.00801 100 100000.00

Show the employee number, department number, and salary of all department managers who have the highest salary in their department.

Joins may be used in conjunction with correlated subqueries.Joins may be used in conjunction with correlated subqueries.

Correlated Subqueries and Derived Tables Page 7-11

Page 264: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

EXISTS Operator EXISTS is an ANSI standard predicate which allows testing for the presence or absence of rows. It is typically used to answer ‘yes’ or ‘no’ types of questions. Whereas traditional subquery processing can only show you which rows qualify (and which do not), EXISTS allows a simple test - is there any occurrence of a condition or are there none. EXISTS logic may be used either from a positive or negative perspective as the examples show.

Correlated Subqueries and Derived Tables Page 7-12

Page 265: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

EXISTS Operator

Do any employees work in department 600?

SELECT 'There are employees' AS AnswerWHERE EXISTS

(SELECT * FROM employeeWHERE department_number = 600);

SELECT 'There are employees' AS AnswerWHERE EXISTS

(SELECT * FROM employeeWHERE department_number = 600);

Assume department 600 has no employees.

***** No rows found ********** No rows found *****

• EXISTS tests for the existence or absence of rows based on specified criteria.• EXISTS is used for ‘yes’ or ‘no’ types of questions.• EXISTS tests for the existence or absence of rows based on specified criteria.• EXISTS is used for ‘yes’ or ‘no’ types of questions.

SELECT 'There are no employees' AS AnswerWHERE NOT EXISTS

(SELECT * FROM employeeWHERE department_number = 600);

SELECT 'There are no employees' AS AnswerWHERE NOT EXISTS

(SELECT * FROM employeeWHERE department_number = 600);

AnswerThere are no employeesAnswerThere are no employees

Do any employees work in department 600?

Correlated Subqueries and Derived Tables Page 7-13

Page 266: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Correlated Subqueries Using EXISTS EXISTS used in conjunction with a correlated subquery may also produce results similar to a NOT IN subquery. While the coding of the statement may at first appear to be less intuitive than the NOT IN approach, there can be substantial advantages to using the EXISTS from a performance standpoint.

Correlated Subqueries and Derived Tables Page 7-14

Page 267: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Correlated Subqueries Using EXISTS

SELECT job_code FROM jobWHERE job_code NOT IN

(SELECT job_code FROM employee) ;

SELECT job_code FROM jobWHERE job_code NOT IN

(SELECT job_code FROM employee) ;

job_code104202104201412103322101

job_code104202104201412103322101

SELECT job_code FROM jobWHERE NOT EXISTS (SELECT *

FROM employee eeWHERE ee.job_code = job.job_code);

SELECT job_code FROM jobWHERE NOT EXISTS (SELECT *

FROM employee eeWHERE ee.job_code = job.job_code);

job_code104202104201412103322101

job_code104202104201412103322101

Which job codes are not assigned to any employee?

NOT IN shows us the difference between two sets.NOT EXISTS with correlated subquery can show same results.NOT IN shows us the difference between two sets.NOT EXISTS with correlated subquery can show same results.

Using NOT IN:

Using NOT EXISTS:

Correlated Subqueries and Derived Tables Page 7-15

Page 268: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Null Logic As seen in the table presented here, whenever a comparison is performed against two values, one of which is null, the result is considered ‘unknown’. Since only ‘true’ conditions are returned by a SELECT statement, unknown results will be discarded.

Correlated Subqueries and Derived Tables Page 7-16

Page 269: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

NULL Logic

SQL uses three point logic in dealing with Nulls.

NOTColA ColB Result Result5 = 5 True False5 = 4 False True5 > 4 True False5 = Null Unknown Unknown5 > Null Unknown UnknownNull = Null Unknown Unknown

Only conditions that are evaluated as ‘True’ are returned.

Correlated Subqueries and Derived Tables Page 7-17

Page 270: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

How NOT IN Works NOT IN has a specific set of rules that is uses for comparing each row of the inner table with each row of the outer table. Based on the results of each comparison, a decision is made to either qualify or disqualify the row. The truth table shows us that to qualify, a column must show ‘all true’ results across all of the NOT EQUAL comparisons.

Correlated Subqueries and Derived Tables Page 7-18

Page 271: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

How NOT IN Works

SELECT Dept# FROM Dept WHERE Dept# NOT IN(SELECT Dept# FROM Emp);

SELECT Dept# FROM Dept WHERE Dept# NOT IN(SELECT Dept# FROM Emp);

Answer: 600

Find all empty departments

SELECT Dept# FROM Dept WHERE Dept# NOT IN (401,403);SELECT Dept# FROM Dept WHERE Dept# NOT IN (401,403);

SELECT Dept# FROM Dept WHERE Dept# NOT = 401

AND Dept# NOT = 403;

SELECT Dept# FROM Dept WHERE Dept# NOT = 401

AND Dept# NOT = 403;

WHERE Dept# NOT = 401AND Dept# NOT = 403

WHERE Dept# NOT = 401AND Dept# NOT = 403

401 403 600 NullF T T ?T F T ?

becomes

becomes

TRUTH TABLE

Dept Emp

Tables

Only ‘all true’ qualifies for outputOnly ‘all true’ qualifies for output

Dept#401403600Null

Emp# Dept#1018 4011020 403

Correlated Subqueries and Derived Tables Page 7-19

Page 272: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

How NOT EXISTS Works NOT EXISTS has a different set of rules that is uses for comparing each row of the inner table with each row of the outer table. In the resulting truth table, ‘any true’ across a column will produce a qualify for the EXISTS condition. Any other columns are qualified as output for the NOT EXISTS condition.

Correlated Subqueries and Derived Tables Page 7-20

Page 273: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

How NOT EXISTS Works

Answer: 600, null

Find all empty departments

SELECT Dept# FROM Dept DWHERE NOT EXISTS(SELECT * FROM Emp EWHERE E. Dept # = D. Dept #);

SELECT Dept# FROM Dept DWHERE NOT EXISTS(SELECT * FROM Emp EWHERE E. Dept # = D. Dept #);

Dept#401403600Null

Emp# Dept#1018 4011020 403

Dept Emp

Tables

Outer Table

Inner Table

401 403 600 NullT F F ?F T F ?

TRUTH TABLE

‘Any true’ qualifies for EXISTS output

401403

‘Any true’ qualifies for EXISTS output

All others qualify for NOT EXISTS outputAll others qualify for NOT EXISTS output

Correlated Subqueries and Derived Tables Page 7-21

Page 274: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

NOT IN with Null Inner Rows NOT IN with null rows in the inner set must always return an empty set. This is because all comparisons with the inner null result in an ‘unknown’ result, thus making it impossible for the truth table to reflect ‘all true’ in any column.

Correlated Subqueries and Derived Tables Page 7-22

Page 275: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

NOT IN with Null Inner Rows

Find all empty departments

Answer:No Rows Found

WHERE Dept# NOT = 401AND Dept# NOT = 403AND Dept# IS NOT NULL

WHERE Dept# NOT = 401AND Dept# NOT = 403AND Dept# IS NOT NULL

401 403 600 NullF T T ?T F T ?? ? ? ?

TRUTH TABLE

Only ‘all true’ columns qualifies for output.Null comparisons are always unknown.Thus, no rows qualify.

Only ‘all true’ columns qualifies for output.Null comparisons are always unknown.Thus, no rows qualify.

SELECT Dept# FROM Dept WHERE Dept# NOT IN(SELECT Dept# FROM Emp);

SELECT Dept# FROM Dept WHERE Dept# NOT IN(SELECT Dept# FROM Emp);

Se Dept# FrWh e Dept# No(Sel t Dept# Fr mp);

lect om Dept er t Inec om E

De401403

pt#

600Null

Dept Emp

Tables

Em101102 41022 Null

p# Dept#8 4010 03

Correlated Subqueries and Derived Tables Page 7-23

Page 276: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

NOT EXISTS with Null Inner Rows NOT EXISTS with null inner rows returns the same result as without the null inner rows. This is due to the fact that the inner null row causes each comparison with the unmatched outer rows to result in an unknown result. Thus, there is no change to the output seen in the prior example without the inner null.

Correlated Subqueries and Derived Tables Page 7-24

Page 277: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

NOT EXISTS with Null Inner Rows

Answer: 600,

Dept#401403600Null

Dept Emp

Tables

Find all empty departments

Select Dept# From Dept DWhere Not Exists(Select * From Emp EWhere E.Dept# = D.Dept#);

Emp# Dept#1018 4011020 403

Select Dept# From Dept DWhere Not Exists(Select * From Emp EWhere E.Dept# = D.Dept#);

null

1022 Null

401 403 600 NullT F F ?F T F ?? ? ? ?

TRUTH TABLE

‘Any true’ qualifies for EXISTS output

401403

‘Any true’ qualifies for EXISTS output

All others qualify for NOT EXISTS outputAll others qualify for NOT EXISTS output

Null

Correlated Subqueries and Derived Tables Page 7-25

Page 278: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary of Null Exclusion Processing Null exclusion processing rules are shown, both as rules and as examples.

Correlated Subqueries and Derived Tables Page 7-26

Page 279: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary of Null Exclusion Processing

Set A NOT IN Set B • Returns unmatched A rows only• Returns nothing if Set B has a null

Set A NOT EXISTS Set B• Returns unmatched A rows and any nulls

Set A NOT IN Set B • Returns unmatched A rows only• Returns nothing if Set B has a null

Set A NOT EXISTS Set B• Returns unmatched A rows and any nulls

Assume Set A has both unmatched rows and nulls.

A(1,2,Null) NOT IN B(1) yields (2)A(1,2,Null) NOT IN B(1,Null) yields (nothing)A(1,2,Null) NOT EX B(1) yields (2,Null)A(1,2,Null) NOT EX B(1,Null) yields (2,Null)

A(1,2,Null) NOT IN B(1) yields (2)A(1,2,Null) NOT IN B(1,Null) yields (nothing)A(1,2,Null) NOT EX B(1) yields (2,Null)A(1,2,Null) NOT EX B(1,Null) yields (2,Null)

Summary by rule:

Summary by example:

Correlated Subqueries and Derived Tables Page 7-27

Page 280: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Interim Tables Interim tables are permanent tables that are used to produce aggregates if those aggregates are to be used for additional computations and comparisons, such as showing the difference between each salary and the average salary. Interim tables must be created, populated, queried and then deleted by the user when no longer needed. They are considered ‘permanent’ tables in that they require permanent space just like any permanent table. Once they have served their purpose for ‘interim computations’ they are typically discarded unless they may service other query requests. A considerable amount of system overhead is involved in the creation and dropping of interim tables. Once the definition of the table has been written to the Data Dictionary, access rights for the table must also be written to the appropriate dictionary tables. When the table is dropped, all such entries in the Data Dictionary must be cleared and the permanent space returned to the available unused allocation. Alternative Syntax

SELECT e.last_name ,e.salary_amount (FORMAT '$,$$$,$99.99') ,t.avgsal (FORMAT '$,$$$,$99.99') FROM employee e , my_temp t WHERE e.salary_amount > t.avgsal ORDER BY 2 DESC;

Correlated Subqueries and Derived Tables Page 7-28

Page 281: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Interim Tables

CREATE TABLE my_temp (avgsal DECIMAL (10,2));CREATE TABLE my_temp (avgsal DECIMAL (10,2));

INSERT INTO my_temp SELECT AVG (salary_amount) FROM employee;

INSERT INTO my_temp SELECT AVG (salary_amount) FROM employee;

SELECT e.last_name,e.salary_amount (FORMAT '$,$$$,$99.99'),t.avgsal (FORMAT '$,$$$,$99.99')

FROM employee e CROSS JOIN my_temp tWHERE e.salary_amount > t.avgsalORDER BY 2 DESC;

SELECT e.last_name,e.salary_amount (FORMAT '$,$$$,$99.99'),t.avgsal (FORMAT '$,$$$,$99.99')

FROM employee e CROSS JOIN my_temp tWHERE e.salary_amount > t.avgsalORDER BY 2 DESC;

DROP TABLE my_temp;DROP TABLE my_temp;

Find employees with salary greater than the company average.

First, create an interim table:

Populate the table with one row:

Perform the query:

Drop the interim table:

Results:

These are temporary tables created as permanent tables to assist in producing short-term query results. They are usually deleted when the results are achieved.

last_name salary_amount avgsalTrainer $100,000.00 $42,386.54Runyon $66,000.00 $42,386.54

. . .

. . .

last_name salary_amount avgsalTrainer $100,000.00 $42,386.54Runyon $66,000.00 $42,386.54

. . .

. . .

Correlated Subqueries and Derived Tables Page 7-29

Page 282: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Derived Tables A derived table is a named temporary table obtained from a subquery. A derived table allows you to directly access the derived results in other parts of the overall SQL statement. Derived tables allow the user the benefit of interim tables but within the syntax of a single SQL statement. Conceptually, the approach is the same, however instead of four separate SQL statements, it may be achieved in one single statement. The syntax in a SELECT statement supports an explicitly named derived table: FROM (subquery) [AS] derived_name [(column_list)] Aggregates in the WHERE clause Expressions involving aggregates are not allowed directly in a WHERE clause. To reference aggregates indirectly in a WHERE clause, define the aggregate as a column of a derived table, then reference the derived table’s column in the WHERE clause. Derived Table Usage Considerations

• Using derived tables as temp tables requires one step instead of four steps, as seen in this example.

• Multiple derived tables may exist within the same query. • Derived table results are created in user’s spool space and deleted at query end.

Alternative Syntax

SELECT last_name ,salary_amount (FORMAT '$,$$$,$99.99') ,avgsal (FORMAT '$,$$$,$99.99') FROM (SELECT AVG (salary_amount) FROM employee) my_temp (avgsal) , employee WHERE salary_amount > avgsal ORDER BY 2 DESC;

Correlated Subqueries and Derived Tables Page 7-30

Page 283: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Derived Tables

SELECT last_name,salary_amount (FORMAT '$,$$$,$99.99'),avgsal (FORMAT '$,$$$,$99.99')

FROM (SELECT AVG (salary_amount) FROM employee) my_temp (avgsal)CROSS JOIN employee

WHERE salary_amount > avgsalORDER BY 2 DESC;

SELECT last_name,salary_amount (FORMAT '$,$$$,$99.99'),avgsal (FORMAT '$,$$$,$99.99')

FROM (SELECT AVG (salary_amount) FROM employee) my_temp (avgsal)CROSS JOIN employee

WHERE salary_amount > avgsalORDER BY 2 DESC;

last_name salary_amount avgsalTrainer $100,000.00 $42,386.54Runyon $66,000.00 $42,386.54

. . .

. . .

last_name salary_amount avgsalTrainer $100,000.00 $42,386.54Runyon $66,000.00 $42,386.54

. . .

. . .

Results:

• Using derived tables as interim tables requires one step instead of four steps, as seen in this example.

• Multiple derived tables may exist within the same query.• Derived table results are created in user’s spool space and deleted at query end.

• Using derived tables as interim tables requires one step instead of four steps, as seen in this example.

• Multiple derived tables may exist within the same query.• Derived table results are created in user’s spool space and deleted at query end.

Show all employees whose salary is greater than the company average.

• Derived tables are temporary tables that are created and dropped as part of a query.• They permit the use of aggregates in the WHERE clause.• Derived tables are temporary tables that are created and dropped as part of a query.• They permit the use of aggregates in the WHERE clause.

Correlated Subqueries and Derived Tables Page 7-31

Page 284: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Derived Tables with Groups Derived tables may be used to represent any type of relational table. They may include all data types as well as aggregates of data. In the example at hand, each row of the derived table contains a department number and also the average salary for that department. It may be noted that the requirement for this report is similar to a report generated earlier using correlated subqueries. However, there is a difference in that the correlated subquery was unable to produce the column showing the departmental averages. Alternative Syntax

SELECT last_name AS last ,salary_amount (FORMAT '$,$$$,$99.99') AS sal ,department_number AS dep ,avgsal (FORMAT '$,$$$,$99.99') FROM (SELECT department_number ,AVG (salary_amount) FROM employee GROUP BY department_number) my_temp (deptno, avgsal) , employee WHERE dep = my_temp.deptnoAND sal > avgsal ORDER BY 2 DESC;

Correlated Subqueries and Derived Tables Page 7-32

Page 285: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Derived Tables with Groups

SELECT last_name AS last,salary_amount (FORMAT '$,$$$,$99.99')

AS sal,department_number AS dep,avgsal (FORMAT '$,$$$,$99.99')

FROM (SELECT department_number,AVG (salary_amount)

FROM employeeGROUP BY department_number)my_temp (deptno, avgsal)

INNER JOIN employeeON dep = my_temp.deptnoWHERE sal > avgsalORDER BY 2 DESC;

SELECT last_name AS last,salary_amount (FORMAT '$,$$$,$99.99')

AS sal,department_number AS dep,avgsal (FORMAT '$,$$$,$99.99')

FROM (SELECT department_number,AVG (salary_amount)

FROM employeeGROUP BY department_number)my_temp (deptno, avgsal)

INNER JOIN employeeON dep = my_temp.deptnoWHERE sal > avgsalORDER BY 2 DESC;

last sal dep avgsalRunyon $66,000.00 501 $50,031.25Kubic $57,700.00 301 $38,800.00Ratzlaff $54,000.00 501 $50,031.25

. . . .

. . . .

last sal dep avgsalRunyon $66,000.00 501 $50,031.25Kubic $57,700.00 301 $38,800.00Ratzlaff $54,000.00 501 $50,031.25

. . . .

. . . .

Show all employees whose salary is greater than the departmental average.

Results:

(This query was previously done using a correlated subquery. Correlated subquery was not able to produce AVGSAL column.)(This query was previously done using a correlated subquery. Correlated subquery was not able to produce AVGSAL column.)

Correlated Subqueries and Derived Tables Page 7-33

Page 286: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab The facing page describes the tasks for your lab exercise.

Correlated Subqueries and Derived Tables Page 7-34

Page 287: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

1.) Use a correlated subquery to produce a report showing employee last name, salary, and department number for all employees whose salary is greater than their department average. Sort by salary within department number.

2.) Using a derived table, produce the same report as in Lab #1 but adding an additional column showing the department average after the employee’s salary.

3.) Produce a list of all department numbers which have no employees using NOT IN. Redesign the query using NOT EXISTS to get the same results.

Correlated Subqueries and Derived Tables Page 7-35

Page 288: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (Optional) The facing page describes an additional lab exercise.

Correlated Subqueries and Derived Tables Page 7-36

Page 289: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (Optional)

4.) Modify Lab 1 to produce the same report but adding department name to the listing. Show only the first 13 characters of department name and give the column a title of ‘department’. Retain the sort by department number and salary for ease of comparison.

Correlated Subqueries and Derived Tables Page 7-37

Page 290: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Correlated Subqueries and Derived Tables Page 7-38

Page 291: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 8

Module 8Advanced Ranking Functions

After completing this module, you should be able to:

• Use the RANK function to produce advanced ranking queries.

• Use the PERCENT_RANK function to produce relative rankings of data listings.

• Use t he ROW_NUMBER function to generate output row numbers.

Advanced Ranking Functions Page 8-1

Page 292: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Advanced Ranking Functions Page 8-2

Page 293: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Advanced Ranking Functions Page 8-3

Page 294: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ranking Review The Ranking function permits a column to be ranked, either based on high or low order, against other rows in the answer set. You may specify the order of ranking by use of the ORDER BY clause within the RANK function. Descending order will provide traditional ranking wherein the largest value is assigned a rank of one. Ascending order will associate the largest value with the highest rank value. Teradata Extension AlternativeTeradata provides a non-ANSI equivalent ‘short hand’ version of the function. The Teradata RANK function is defined as follows: RANK(colname ASC|DESC)) Where: colname = the column to be ranked and the descending sort key of the result

storeid prodid sales ----------- --------- ------------- 1001 A 100000.00 1001 C 60000.00 1001 D 35000.00 1001 F 150000.00 1002 A 40000.00 1002 C 35000.00 1002 D 25000.00 1003 A 30000.00 1003 B 65000.00 1003 C 20000.00 1003 D 50000.00

Salestbl contents

storeid prodid sales Rank ----------- ---------- -------------- ----------- 1001 F 150000.00 1 1001 A 100000.00 2 1001 C 60000.00 3 1001 D 35000.00 4

SELECT storeid ,prodid ,sales ,RANK(sales) AS "Rank" FROM salestbl WHERE storeid = 1001;

SELECT storeid ,prodid ,sales ,RANK(sales ASC) AS "Rank" FROM salestbl

storeid prodid sales Rank ----------- --------- -------------- ------------ 1001 D 35000.00 1

Equivalent queries to facing page queries using Teradata

Advanced Ranking Functions Page 8-4

Page 295: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ranking Review

Show the ranking of product sales for store 1001.SELECT storeid

,prodid,sales,RANK( ) OVER (ORDER BY sales DESC) AS "Rank"

FROM salestblWHERE storeid = 1001;

SELECT storeid,prodid,sales,RANK( ) OVER (ORDER BY sales DESC) AS "Rank"

FROM salestblWHERE storeid = 1001;

WHERE clause qualifies rows to be ranked.DESC order means amount descends as rank ascendsWHERE clause qualifies rows to be ranked.DESC order means amount descends as rank ascends

SELECT storeid,prodid,sales,RANK( ) OVER (ORDER BY sales ASC) AS "Rank"

FROM salestblWHERE storeid = 1001;

SELECT storeid,prodid,sales,RANK( ) OVER (ORDER BY sales ASC) AS "Rank"

FROM salestblWHERE storeid = 1001;

ASC order means amount ascends as rank ascends.Rank #1 always appears at top of list unless overridden by ORDER BY.

ASC order means amount ascends as rank ascends.Rank #1 always appears at top of list unless overridden by ORDER BY.

storeid prodid sales Rank1001 D 35000.00 11001 C 60000.00 21001 A 100000.00 31001 F 150000.00 4

storeid prodid sales Rank1001 D 35000.00 11001 C 60000.00 21001 A 100000.00 31001 F 150000.00 4

Reverse the ranking sequence of product sales for store 1001.

storeid prodid sales Rank 1001 F 150000.00 1 1001 A 100000.00 2 1001 C 60000.00 3 1001 D 35000.00 4

storeid prodid sales Rank 1001 F 150000.00 1 1001 A 100000.00 2 1001 C 60000.00 3 1001 D 35000.00 4

Advanced Ranking Functions Page 8-5

Page 296: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Qualified Ranking The QUALIFY clause allows restriction of rows to be output in the final result. In the facing-page query, the QUALIFY clause restricts the result to the top three sales amounts.

storeid prodid sales Ranking ------------- ----------- -------------- ------------ 1001 F 150000.00 1 1001 A 100000.00 2 1003 B 65000.00 3

SELECT storeid ,prodid ,sales ,RANK(sales) AS "Ranking" FROM salestbl QUALIFY Ranking <= 3;

Teradata RANK

Advanced Ranking Functions Page 8-6

Page 297: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Qualified Ranking

storeid prodid sales Ranking 1001 F 150000.00 1 1001 A 100000.00 2 1003 B 65000.00 3

storeid prodid sales Ranking 1001 F 150000.00 1 1001 A 100000.00 2 1003 B 65000.00 3

SELECT storeid,prodid,sales,RANK( ) OVER (ORDER BY sales DESC) AS Ranking

FROM salestblQUALIFY Ranking <= 3;

Get top 3 sales for any product in any store:

QUALIFY the ranking for the top 3 sales amounts onlyQUALIFY the ranking for the top 3 sales amounts only

Salestbl Contentsstoreid prodid sales 1001 A 100000.00 1001 C 60000.00 1001 D 35000.00 1001 F 150000.00 1002 A 40000.00 1002 C 35000.00 1002 D 25000.00 1003 A 30000.00 1003 B 65000.00 1003 C 20000.00 1003 D 50000.00

Advanced Ranking Functions Page 8-7

Page 298: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ranking with Qualification and PARTITION BY The PARTITION BY clause may be used in conjunction with a RANK function to change the scope of the ranking. Without a PARTITION BY clause, the scope defaults to the RANK column. In the example on the preceding page, the scope was the sales amount and rankings were done strictly based on that amount. In the example on the facing page, the PARTITION BY clause adds the store id to the scope, thus rankings will be based upon sales within a store. Each store will have its top three ranking sales output. Whereas the RANK ORDER BY clause controls the default sort key, the PARTITION BY clause adds another level of sort to the output. RANK sort rules: Default major sort -- Descending by RANK ORDER BY column Minor sort -- Ascending by PARTITION BY column(s) Teradata Equivalent Alternative The Teradata RANK function uses the GROUP BY clause to control the scope of the ranking. The following is an equivalent example to the one on the facing page which generates the same output result.

SELECT storeid ,prodid ,sales ,RANK( sales) AS Ranking FROM salestbl GROUP BY storeid QUALIFY Ranking <= 3;

• GROUP BY clause controls scope, i.e., rank sales within store. • Without GROUP BY, scope would default to sales. • QUALIFY the ranking for the top 3 sales amounts per store. • Default Teradata RANK sort sequence is descending by ranking column (sales). • Due to GROUP BY, sort is by sales (DESC) within store (ASC).

Advanced Ranking Functions Page 8-8

Page 299: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ranking with Qualification and PARTITION BY

storeid prodid sales Ranking 1001 F 150000.00 1 1001 A 100000.00 2 1001 C 60000.00 3 1002 A 40000.00 1 1002 C 35000.00 2 1002 D 25000.00 3 1003 B 65000.00 1 1003 D 50000.00 2 1003 A 30000.00 3

storeid prodid sales Ranking 1001 F 150000.00 1 1001 A 100000.00 2 1001 C 60000.00 3 1002 A 40000.00 1 1002 C 35000.00 2 1002 D 25000.00 3 1003 B 65000.00 1 1003 D 50000.00 2 1003 A 30000.00 3

• PARTITION BY clause controls scope, i.e., rank sales within store.• Without PARTITION BY, scope would default to sales. • QUALIFY the ranking for the top 3 sales amounts per store.• Sort sequence of sales is descending.• Due to PARTITION BY, sort is by sales (DESC) within store (ASC).• Note: No aggregation is done in this query

• PARTITION BY clause controls scope, i.e., rank sales within store.• Without PARTITION BY, scope would default to sales. • QUALIFY the ranking for the top 3 sales amounts per store.• Sort sequence of sales is descending.• Due to PARTITION BY, sort is by sales (DESC) within store (ASC).• Note: No aggregation is done in this query

Get the top three selling products by store.

SELECT storeid,prodid,sales,RANK( ) OVER (PARTITION BY storeidORDER BY sales DESC) AS Ranking

FROM salestblQUALIFY Ranking <= 3;

Use PARTITION BY clause to control scope of rankings.Use PARTITION BY clause to control scope of rankings.

Advanced Ranking Functions Page 8-9

Page 300: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Expanding RANK Scope In the facing-page example, an additional level of scope has been added via the PARTITION BY clause. Our scope is now sales by product within store. Because these three elements make up a ranking group and because the distinct elements store and product only yield a single row, there are no rankings below rank one. This is a case where the scope matches the granularity of the table, thus no reasonable ranking can be produced. Teradata Equivalent Alternative The Teradata RANK function uses the GROUP BY clause to control the scope of the ranking. The following is an equivalent example to the one on the facing page which generates the same output result.

SELECT storeid ,prodid ,sales ,RANK(sales) AS Ranking FROM salestbl GROUP BY storeid, prodid QUALIFY RANK(sales) <= 3;

Advanced Ranking Functions Page 8-10

Page 301: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Expanding RANK Scope

SELECT storeid,prodid,sales,RANK( ) OVER (PARTITION BY storeid, prodid

ORDER BY sales DESC) AS RankingFROM salestblQUALIFY Ranking <= 3;

SELECT storeid,prodid,sales,RANK( ) OVER (PARTITION BY storeid, prodid

ORDER BY sales DESC) AS RankingFROM salestblQUALIFY Ranking <= 3;

• An additional PARTITION BY column is added.• Scope is sales by product within store.• Each product exists only once in each store.• Thus, each row gets a rank of 1.

• An additional PARTITION BY column is added.• Scope is sales by product within store.• Each product exists only once in each store.• Thus, each row gets a rank of 1.

storeid prodid sales Ranking 1001 A 100000.00 1 1001 C 60000.00 1 1001 D 35000.00 1 1001 F 150000.00 1 1002 A 40000.00 1 1002 C 35000.00 1 1002 D 25000.00 1 1003 A 30000.00 1 1003 B 65000.00 1 1003 C 20000.00 1 1003 D 50000.00 1

storeid prodid sales Ranking 1001 A 100000.00 1 1001 C 60000.00 1 1001 D 35000.00 1 1001 F 150000.00 1 1002 A 40000.00 1 1002 C 35000.00 1 1002 D 25000.00 1 1003 A 30000.00 1 1003 B 65000.00 1 1003 C 20000.00 1 1003 D 50000.00 1

Show the ranking of sales within product-id, within store.

Advanced Ranking Functions Page 8-11

Page 302: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ranking with Aggregations OLAP aggregations (RANK) and group aggregations (SUM) may be combined in a single query to produce a ranking based on an aggregation.

Prodid Sumsales

A 170000 B 65000 C 115000 D 110000

F 150000

DT (Derived Table

The contents of the realized derived table DT are shown:

Advanced Ranking Functions Page 8-12

Page 303: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ranking with Aggregations

Get top three selling items across all stores:

SELECT prodid, SUM(sales) AS sumsales, RANK( ) OVER (ORDER BY sumsales DESC)

AS RankingFROM salestblGROUP BY 1 QUALIFY Ranking <= 3;

Prodid Sumsales RankA 170000.00 1 F 150000.00 2 C 115000.00 3

Prodid Sumsales RankA 170000.00 1 F 150000.00 2 C 115000.00 3

Advanced Ranking Functions Page 8-13

Page 304: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Low Order Ranking Sometimes a ranking by low order is appropriate, (e.g., show the ten poorest selling products, the five lowest salaried employees or the three smallest department budgets.) A low-order ranking is accomplished by changing the ORDER BY clause of the RANK function from descending to ascending. In the case of RANK() (OVER (ORDER BY col DESC), it will produce the list in descending rank-column order. The highest value will come out first and will be assigned a rank of one. The RANK ORDER BY clause is used to assign this ranking sequence. In the QUALIFY clause, a different RANK ORDER BY sequence is used. This tells the optimizer that in qualifying the output rows, it should pick the first three which would appear at the top of the list assuming the rank-column is in ascending sequence. This means that the lowest amount (assigned a rank of five) will be at the top of the list. Note that the QUALIFY clause is not looked at for absolute mathematical value. The clause QUALIFY <= 3 means produce a list of the first three, whatever the sequencing of the list. Notice that the result shows the rankings with numbers greater than three, which seems contradictory if viewing it from a purely mathematical standpoint.

Advanced Ranking Functions Page 8-14

Page 305: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Low Order RankingGet top three selling items across all stores (repeated):

Get three poorest selling items across all stores:

Prodid Sumsales RankingB 65000.00 5 D 110000.00 4 C 115000.00 3

Prodid Sumsales RankingB 65000.00 5 D 110000.00 4 C 115000.00 3

• ORDER BY Sumsales ASC gives bottom three rows.• Note: Rank value outputs may be greater than 3.• ‘Qualify’ says ‘order by sales ascending and give me first three’.

• ORDER BY Sumsales ASC gives bottom three rows.• Note: Rank value outputs may be greater than 3.• ‘Qualify’ says ‘order by sales ascending and give me first three’.

SELECT Prodid, Sumsales, RANK( ) OVER (ORDER BY Sumsales DESC)

AS RankingFROM (SELECT prodid, SUM(sales)

FROM salestblGROUP BY 1) AS dt(Prodid, Sumsales)

QUALIFY Ranking <= 3;

Prodid Sumsales Ranking A 170000.00 1 F 150000.00 2 C 115000.00 3

Prodid Sumsales Ranking A 170000.00 1 F 150000.00 2 C 115000.00 3

SELECT Prodid, Sumsales, RANK( ) OVER (ORDER BY Sumsales DESC)

AS RankingFROM (SELECT prodid, SUM(sales)

FROM salestblGROUP BY 1) AS dt(Prodid, Sumsales)

QUALIFY RANK( ) OVER (ORDER BY Sumsales ASC) <= 3;

Advanced Ranking Functions Page 8-15

Page 306: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

RANK and ORDER BY A ranking is generated by first looking at the sort operator of the RANK function. The default is descending, which produces a ranking in descending rank-column sequence. The highest value will receive a rank of one and is placed at the top of the list. The QUALIFY clause then determines how many elements from the top of the list will be produced in the output. The ORDER BY clause in the SELECT statement may always be used to control the final order of the result set.

Advanced Ranking Functions Page 8-16

Page 307: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

RANK and ORDER BYGet three poorest selling items across all stores, order by Prodid.

Same 3 spool rows generated as prior example. List is ordered by ascending product id.Same 3 spool rows generated as prior example. List is ordered by ascending product id.

If query test is changed to <=2, which rows are output?

SELECT prodid, SUM(sales) AS Sumsales,RANK( ) OVER (ORDER BY Sumsales DESC)

AS Ranking FROM salestblGROUP BY 1 QUALIFY RANK( ) OVER (ORDER BY Sumsales ASC) <= 3ORDER BY 1;

SELECT prodid, SUM(sales) AS Sumsales,RANK( ) OVER (ORDER BY Sumsales DESC)

AS Ranking FROM salestblGROUP BY 1 QUALIFY RANK( ) OVER (ORDER BY Sumsales ASC) <= 2ORDER BY 1;

Prodid Sumsales RankingB 65000.00 5 D 110000.00 4

Prodid Sumsales RankingB 65000.00 5 D 110000.00 4

Prodid Sumsales RankingB 65000.00 5 C 115000.00 3 D 110000.00 4

Prodid Sumsales RankingB 65000.00 5 C 115000.00 3 D 110000.00 4

Advanced Ranking Functions Page 8-17

Page 308: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ROW_NUMBER Function (1 of 2) The ROW_NUMBER function returns the sequential row number of a group starting with the number one. ROW_NUMBER:

• is an ANSI SQL-99 function. • is the same as ANSI RANK function except in the event of a tie. • doesn't report duplicate values, unlike RANK.

The following query uses the RANK function to achieve the same results as the query on the facing page:

SELECT storeid, prodid, sales, RANK () OVER (PARTITION BY storeid ORDER BY sales DESC) AS rank_sales FROM salestbl QUALIFY rank_sales <= 3;

storeid prodid sales rank_sales ----------- ------ --------------- --------------- 1001 F 150000.00 1 1001 A 100000.00 2 1001 C 60000.00 3 1002 A 40000.00 1 1002 C 35000.00 2 1002 D 25000.00 3 1003 B 65000.00 1 1003 D 50000.00 2 1003 A 30000.00 3

Advanced Ranking Functions Page 8-18

Page 309: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ROW_NUMBER Function (1 of 2)

The ROW_NUMBER function returns the sequential row number of a group starting with the number one.The ROW_NUMBER function returns the sequential row number of a group starting with the number one.

Rank product sales within a store using the ROW_NUMBER functionSELECT storeid, prodid, sales,

ROW_NUMBER () OVER (PARTITION BY storeidORDER BY sales DESC) AS rank_sales

FROM salestblQUALIFY rank_sales <= 3;

SELECT storeid, prodid, sales, ROW_NUMBER () OVER (PARTITION BY storeidORDER BY sales DESC) AS rank_sales

FROM salestblQUALIFY rank_sales <= 3;

ROW_NUMBER• Is an ANSI SQL-99 function. • Is the same as the ANSI RANK function except in the event of a tie. • Doesn't report duplicate values, unlike RANK.

ROW_NUMBER• Is an ANSI SQL-99 function. • Is the same as the ANSI RANK function except in the event of a tie. • Doesn't report duplicate values, unlike RANK.

storeid prodid sales rank_sales1001 F 150000.00 11001 A 100000.00 21001 C 60000.00 31002 A 40000.00 11002 C 35000.00 21002 D 25000.00 31003 B 65000.00 1 1003 D 50000.00 21003 A 30000.00 3

storeid prodid sales rank_sales1001 F 150000.00 11001 A 100000.00 21001 C 60000.00 31002 A 40000.00 11002 C 35000.00 21002 D 25000.00 31003 B 65000.00 1 1003 D 50000.00 21003 A 30000.00 3

Advanced Ranking Functions Page 8-19

Page 310: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ROW_NUMBER Function (2 of 2) The ROW_NUMBER function always assigns discrete numbers to each row, unlike RANK which can produce tied results. The following query uses the RANK function to achieve a similar but different result compared to the query on the facing page:

SELECT itemid, salesdate, sales, RANK() OVER (ORDER BY sales DESC) WHERE salesdate BETWEEN DATE '2004-01-01' AND DATE '2004-03-01’ AND itemid = 10 FROM daily_sales_2004;

itemid salesdate sales Rank(sales) ----------- ------------- ----------- ----------------- 10 04/01/10 550.00 1 10 04/02/17 550.00 1 10 04/02/20 450.00 3 10 04/02/06 350.00 4 10 04/02/27 350.00 4 10 04/01/05 350.00 4 10 04/01/03 250.00 7 10 04/02/03 250.00 7 10 04/01/25 200.00 9 10 04/01/02 200.00 9 10 04/01/21 150.00 11 10 04/02/01 150.00 11 10 04/01/01 150.00 11 10 04/01/31 100.00 14

Note, ranking positions which results in ties do not reuse the tied position number. For example, the seventh row in this list still maintains a rank of seven, even though the previous row has a rank of four.

Advanced Ranking Functions Page 8-20

Page 311: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ROW_NUMBER Function (2 of 2)

Rank sales of item 10 by day between the two dates specified using ROW_NUMBER.

SELECT itemid, salesdate, sales,ROW_NUMBER() OVER (ORDER BY sales DESC) WHERE salesdate BETWEEN

DATE '2004-01-01' AND DATE '2004-03-01’AND itemid = 10FROM daily_sales_2004;

Note:

• Ties are always assigned an incremented sequence number with ROW_NUMBER.

• Ties are always assigned the same number with RANK function.

Note:

• Ties are always assigned an incremented sequence number with ROW_NUMBER.

• Ties are always assigned the same number with RANK function.

itemid salesdate sales Row_Number()10 04/01/10 550.00 110 04/02/17 550.00 210 04/02/20 450.00 310 04/02/06 350.00 410 04/02/27 350.00 510 04/01/05 350.00 610 04/01/03 250.00 710 04/02/03 250.00 810 04/01/25 200.00 910 04/01/02 200.00 1010 04/01/21 150.00 1110 04/02/01 150.00 1210 04/01/01 150.00 1310 04/01/31 100.00 14

itemid salesdate sales Row_Number()10 04/01/10 550.00 110 04/02/17 550.00 210 04/02/20 450.00 310 04/02/06 350.00 410 04/02/27 350.00 510 04/01/05 350.00 610 04/01/03 250.00 710 04/02/03 250.00 810 04/01/25 200.00 910 04/01/02 200.00 1010 04/01/21 150.00 1110 04/02/01 150.00 1210 04/01/01 150.00 1310 04/01/31 100.00 14

Advanced Ranking Functions Page 8-21

Page 312: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

PERCENT_RANK Function (1 of 2) The PERCENT_RANK function returns the 'relative rank' of all rows based on a defined column or expression. It is a close relative of the RANK function and is most easily understood in that context. PERCENT_RANK is always a value between 0.0 and 1.0 inclusive. The following explains the meaning of PERCENT_RANK value: A PERCENT_RANK of:

0.0 indicates that no rows precede this row in the ranking. 0.1 indicates that 10% of rows precede this row in the ranking. 0.2 indicates that 20% of rows precede this row in the ranking ........... 0.9 indicates that 90% of rows precede this row in the ranking. 1.0 indicates that all rows precede this row in the ranking.

The formula for calculating the PERCENT_RANK is the following: PERCENT_RANK = (Row Ranking - 1) / (# of rows - 1) ODBC Consideration: To make the query seen on the facing page work correctly in the ODBC environment, it is necessary to submit the query as follows:

SELECT storeid, prodid, sales ,RANK() OVER (ORDER BY sales DESC) AS Rank_Sales ,PERCENT_RANK() OVER (ORDER BY sales DESC) (FORMAT '9.9(6)') (CHAR(8)) AS Pct_Rank_Sales FROM salestbl ;

Advanced Ranking Functions Page 8-22

Page 313: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

PERCENT_RANK Function (1 of 2)

Returns the 'relative rank' of all rows based on a defined column or expression. Returns the 'relative rank' of all rows based on a defined column or expression.

Show sales for all products in all stores and rank by sales amount and also show the relative ranking.

SELECT storeid, prodid, sales,RANK() OVER (ORDER BY sales DESC)

AS Rank_Sales ,PERCENT_RANK() OVER (ORDER BY sales DESC)

AS Pct_Rank_Sales FROM salestbl ;

storeid prodid sales Rank_Sales Pct_Rank_Sales1001 F 150000.00 1 0.0000001001 A 100000.00 2 0.1000001003 B 65000.00 3 0.2000001001 C 60000.00 4 0.3000001003 D 50000.00 5 0.4000001002 A 40000.00 6 0.5000001001 D 35000.00 7 0.6000001002 C 35000.00 7 0.6000001003 A 30000.00 9 0.8000001002 D 25000.00 10 0.9000001003 C 20000.00 11 1.000000

storeid prodid sales Rank_Sales Pct_Rank_Sales1001 F 150000.00 1 0.0000001001 A 100000.00 2 0.1000001003 B 65000.00 3 0.2000001001 C 60000.00 4 0.3000001003 D 50000.00 5 0.4000001002 A 40000.00 6 0.5000001001 D 35000.00 7 0.6000001002 C 35000.00 7 0.6000001003 A 30000.00 9 0.8000001002 D 25000.00 10 0.9000001003 C 20000.00 11 1.000000

• PERCENT_RANK is always a value between 0.0 and 1.0 inclusive.

• It’s value represents the portion of rows in the answer set which precede any given row on the list.

• PERCENT_RANK is always a value between 0.0 and 1.0 inclusive.

• It’s value represents the portion of rows in the answer set which precede any given row on the list.

50% of rows precede this row.

Advanced Ranking Functions Page 8-23

Page 314: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

PERCENT_RANK Function (2 of 2) The example on the facing page has changed the order of the listing by changing the ORDER BY clause of the PERCENT_RANK function. By changing it to ascending, the top seller is now moved to the bottom of the list, even though his ranking as #1 is preserved. Because the PERCENT-RANK function reflects a row’s position in the answer set, any reordering of rows will affect this value. Points to consider about this example:

• The ranking assignments are preserved - the top seller is still #1 in ranking. • This is due to the RANK OVER sales descending. • The top selling entry is now however at the bottom of the ranking list. • This is due to the PERCENT_RANK OVER sales ascending. • Accordingly, the top seller ($150,000) is assigned a 'relative rank' of 1.0 because all rows

precede it on this ranking list based on how the ranking list is defined. • The final order of the report is determined by the last specified ORDER, in this case sales

ascending. ODBC Consideration: To make the query seen on the facing page work correctly in the ODBC environment, it is necessary to submit the query as follows:

SELECT storeid, prodid, sales ,RANK() OVER (ORDER BY sales DESC) AS Rank_Sales ,PERCENT_RANK() OVER (ORDER BY sales ASC) (FORMAT '9.9(6)') (CHAR(8)) AS Pct_Rank_Sales FROM salestbl ;

Advanced Ranking Functions Page 8-24

Page 315: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

PERCENT_RANK Function (2 of 2)Show sales for all products in all stores and rank by sales amount and also show the relative ranking. This time order percent_rank ascending.

SELECT storeid, prodid, sales, RANK() OVER (ORDER BY sales DESC)

AS Rank_Sales ,PERCENT_RANK()

OVER (ORDER BY sales ASC) AS Pct_Rank_Sales

FROM salestbl ;

storeid prodid sales Rank_Sales Pct_Rank_Sales1003 C 20000.00 11 0.0000001002 D 25000.00 10 0.1000001003 A 30000.00 9 0.2000001001 D 35000.00 7 0.3000001002 C 35000.00 7 0.3000001002 A 40000.00 6 0.5000001003 D 50000.00 5 0.6000001001 C 60000.00 4 0.7000001003 B 65000.00 3 0.8000001001 A 100000.00 2 0.9000001001 F 150000.00 1 1.000000

storeid prodid sales Rank_Sales Pct_Rank_Sales1003 C 20000.00 11 0.0000001002 D 25000.00 10 0.1000001003 A 30000.00 9 0.2000001001 D 35000.00 7 0.3000001002 C 35000.00 7 0.3000001002 A 40000.00 6 0.5000001003 D 50000.00 5 0.6000001001 C 60000.00 4 0.7000001003 B 65000.00 3 0.8000001001 A 100000.00 2 0.9000001001 F 150000.00 1 1.000000

• Rankings are preserved - top seller is still #1 in ranking.

• This is due to the RANK OVERsales descending.

• The top seller is now, however, at the bottom of the list.

• This is due to the PERCENT_RANK OVER sales ascending.

• Top seller has ‘relative rank’ of 1 because all rows precede it.

• Low seller has ‘relative rank’ of 0 because no rows precede it.

• Rankings are preserved - top seller is still #1 in ranking.

• This is due to the RANK OVERsales descending.

• The top seller is now, however, at the bottom of the list.

• This is due to the PERCENT_RANK OVER sales ascending.

• Top seller has ‘relative rank’ of 1 because all rows precede it.

• Low seller has ‘relative rank’ of 0 because no rows precede it.

50% of rows precede this row.

Advanced Ranking Functions Page 8-25

Page 316: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

Advanced Ranking Functions Page 8-26

Page 317: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab1a.) Rank the top three departments based on their budget_amounts. Show department number, budget

amount and rank.1b.) Repeat the query using the ROW_NUMBER function in place of the RANK function. Is there any

difference in the result?2.) Rank the top three departments in terms of highest total salary amounts. Show department number,

total salary amounts and rank.3.) Rank the bottom three department in terms of total salary_amount. Show department number and total

salary amounts.

Optional Lab Exercises4.) Create a merged answer set based on the top three salary departments and the top three budget

departments. Use the UNION operator to combine the answer sets, one of which can be taken from lab #2 above and the other from Lab#1a above. Make the resulting output look as follows:

deptno dollar_amt ranking 401 budget 982300.00 1 401 salary 245575.00 1 403 budget 932000.00 2 403 salary 233000.00 2 301 budget 465600.00 3 501 salary 200125.00 3

5a.) Create a listing of the top three selling products across all stores as found in the 'salestbl'. Show the store id, product id, sales amount, the ranking and the relative ranking in descending order. Use PERCENT_RANK to produce the relative ranking.

5b.) Modify the query in 5a) to produce the same listing in ascending order of relative ranking.

Advanced Ranking Functions Page 8-27

Page 318: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Advanced Ranking Functions Page 8-28

Page 319: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 9

Module 9Quantiles and Width Buckets

After completing this module, you should be able to:

• Produce reports using the QUANTILE function.

• Produce reports using the WIDTH_BUCKET function.

Quantiles and Width Buckets Page 9-1

Page 320: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Quantiles and Width Buckets Page 9-2

Page 321: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Quantiles and Width Buckets Page 9-3

Page 322: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

OLAP Aggregation Functions In this module we will look at the OLAP functions QUANTILE and WIDTH_BUCKET. Many business applications require grouping data into intervals, a task not readily performed with ANSI-standard interactive SQL, particularly for large volumes of data. QUANTILE performs this calculation and distribution with a simple function call and two arguments. WIDTH_BUCKET returns the number of the partition to which value_expression is assigned. The Sales table will be used for the examples in this module.

Quantiles and Width Buckets Page 9-4

Page 323: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

OLAP Aggregation Functions

QUANTILE: A function which divides a set of rows into a set of partitions such as percentiles, quartiles, tertiles.

WIDTH_BUCKET: A function which assigns partition number to values based on a specified range and the number of desired partitions.

QUANTILE: A function which divides a set of rows into a set of partitions such as percentiles, quartiles, tertiles.

WIDTH_BUCKET: A function which assigns partition number to values based on a specified range and the number of desired partitions.

OLAP aggregation functions to be considered include:

Quantiles and Width Buckets Page 9-5

Page 324: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Quantiles A quantile is a generic interval of user-defined width. For example, percentiles divide data among 100 evenly spaced intervals, deciles among 10 evenly spaced intervals, quartiles among 4, and so on. A quantile score indicates the fraction of rows having a value lower than the current value. For example, a percentile score of 98 means that 98 percent of the rows in the list have a value lower than the current value. Quantiles are used to divide a number of rows into a number of partitions of roughly equal size. The most common quantile is the percentile which is based on a value of 100. It is also not uncommon to have quartiles (based on 4), tertiles (based on 3) or deciles (based on 10). The standard form of the quantile function is: QUANTILE (quantile_constant,sortlist) Where:

quantile_constant = constant used to define number of quantile partitions sortlist = column reference to be used for the partitioning and to sort the output

QUANTILE is not an ANSI standard function. To compute QUANTILE(q, s) using ANSI window functions, use the following:

(RANK() OVER (ORDER BY s) - 1) * q / COUNT(*) OVER()

Quantiles and Width Buckets Page 9-6

Page 325: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Quantiles

SELECT employee_number, salary_amount,QUANTILE (100, salary_amount) AS Quant

FROM employeeWHERE department_number = 401;

SELECT employee_number, salary_amount,QUANTILE (100, salary_amount) AS Quant

FROM employeeWHERE department_number = 401;

Quantiles are used to divide rows into a number (n) of evenly spaced intervals:

Show the salaries for department 401 and their percentile.

QUANTILE (n, colname) where n (width) can be any integer

QUANTILE (n, colname) where n (width) can be any integer

Show the salaries for department 401 in the 70+ percentile.SELECT employee_number, salary_amount

,QUANTILE (100, salary_amount) AS QuantFROM employeeWHERE department_number = 401QUALIFY Quant > 70;

SELECT employee_number, salary_amount,QUANTILE (100, salary_amount) AS Quant

FROM employeeWHERE department_number = 401QUALIFY Quant > 70;

employee_number salary_amount Quant1013 24500.00 01001 25525.00 141022 32300.00 281004 36300.00 421003 37850.00 571002 43100.00 711010 46000.00 85

employee_number salary_amount Quant1013 24500.00 01001 25525.00 141022 32300.00 281004 36300.00 421003 37850.00 571002 43100.00 711010 46000.00 85

employee_number salary_amount Quant1002 43100.00 711010 46000.00 85

employee_number salary_amount Quant1002 43100.00 711010 46000.00 85

Quantiles and Width Buckets Page 9-7

Page 326: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Aggregates and Quantiles While the first query on the facing page returns all employees in the top 25 percentile of company salaries, it is the second which is more interesting. It may seem tempting to get the sum of the top 25 percent of salaries by constructing the query seen at mid-page, however, once again, the problem of mixing aggregates and OLAP functions arises. And, once again, the way to overcome this problem is to use a derived or temporary table.

Quantiles and Width Buckets Page 9-8

Page 327: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Aggregates and Quantiles

SELECT salary_amount, QUANTILE (100, salary_amount)FROM employeeQUALIFY QUANTILE(100, salary_amount) >=75;

SELECT salary_amount, QUANTILE (100, salary_amount)FROM employeeQUALIFY QUANTILE(100, salary_amount) >=75;

Show all employee salaries in the top 25 % of the company:salary_amount Quantile

53625.00 76 54000.00 80 56500.00 84 57700.00 88 66000.00 92

100000.00 96

salary_amount Quantile53625.00 76 54000.00 80 56500.00 84 57700.00 88 66000.00 92

100000.00 96

Get the sum of salaries of the top 25% of the company:SELECT SUM(salary_amount)FROM employeeQUALIFY QUANTILE(100, salary_amount) >= 75;

SELECT SUM(salary_amount)FROM employeeQUALIFY QUANTILE(100, salary_amount) >= 75;

SELECT SUM(sals) FROM(SELECT salary_amount FROM employeeQUALIFY QUANTILE(100, salary_amount) >= 75) temp(sals);

SELECT SUM(sals) FROM(SELECT salary_amount FROM employeeQUALIFY QUANTILE(100, salary_amount) >= 75) temp(sals);

Sum(sals)387825.00

Sum(sals)387825.00

Error: Aggregates are allowed only with the windowed OLAP functions.

Error: Aggregates are allowed only with the windowed OLAP functions.

Correct approach: Use a derived table.Correct approach: Use a derived table.

Quantiles and Width Buckets Page 9-9

Page 328: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Quantiles and Derived Tables By default, both the quantile column and the quantile value itself will be output in ascending sequence. As always, the ORDER BY clause may be used to reorder the output. Aggregations may not be combined with OLAP functions, thus a derived or temporary table is sometimes necessary to produce the aggregation before the function is applied. The standard form of the quantile function is: QUANTILE (quantile_constant,sortlist) Where:

quantile_constant = constant used to define number of quantile partitions sortlist = column reference to be used for the partitioning and to sort the output

storeid prodid sales ----------- --------- --------------- 1001 A 100000.00 1001 C 60000.00 1001 D 35000.00 1001 F 150000.00 1002 A 40000.00 1002 C 35000.00 1002 D 25000.00 1003 A 30000.00 1003 B 65000.00 1003 C 20000.00 1003 D 50000.00

Salestbl 11

Quantiles and Width Buckets Page 9-10

Page 329: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Quantiles and Derived Tables

SELECT t.prodid, t.sumsales, QUANTILE (100,sumsales)FROM (SELECT a.prodid, SUM(a.sales)

FROM salestbl aGROUP BY 1) AS t(prodid, sumsales);

SELECT t.prodid, t.sumsales, QUANTILE (100,sumsales)FROM (SELECT a.prodid, SUM(a.sales)

FROM salestbl aGROUP BY 1) AS t(prodid, sumsales);

Show products in the 60+ percentile based on total sales:

Derived table used to do aggregation.Sort by ascending quantile column is report default.Width = 100, quantile range is 0-99.

Derived table used to do aggregation.Sort by ascending quantile column is report default.Width = 100, quantile range is 0-99.

Show each product percentile based on total sales:

SELECT t.prodid, t.sumsales, QUANTILE (100,sumsales)FROM (SELECT a.prodid, SUM(a.sales)

FROM salestbl aGROUP BY 1) AS t(prodid, sumsales)

QUALIFY QUANTILE(100,sumsales) >= 60;

SELECT t.prodid, t.sumsales, QUANTILE (100,sumsales)FROM (SELECT a.prodid, SUM(a.sales)

FROM salestbl aGROUP BY 1) AS t(prodid, sumsales)

QUALIFY QUANTILE(100,sumsales) >= 60;

prodid sumsales QuantileB 65000.00 0 D 110000.00 20 C 115000.00 40 F 150000.00 60 A 170000.00 80

prodid sumsales QuantileB 65000.00 0 D 110000.00 20 C 115000.00 40 F 150000.00 60 A 170000.00 80

prodid sumsales QuantileF 150000.00 60 A 170000.00 80

prodid sumsales QuantileF 150000.00 60 A 170000.00 80

Quantiles and Width Buckets Page 9-11

Page 330: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Varying Quantiles In the top example on the right hand page, you will notice the function as follows: QUANTILE(3, sumsales DESC) Descending is the default for the sort columns of the QUANTILE function. Looking at the report, the output appears to be sorted by sumsales ascending. This might appear, at first glance, to be a contradiction but, in reality, it is not. The DESC qualifier on the sort column indicates only that column “sumsales” will descend as the quantile value itself descends, regardless of the ordering of the output. This means that the lowest “sumsales” value (65,000) will correspond to the lowest quantile value (0) and conversely the highest “sumsales” (170,000) will correspond to the highest quantile value (2). Notice in the lower example on the right page, the function reads as follows: QUANTILE(3, sumsales ASC) In this case, the column “sumsales” will ascend as the quantile value descends. In other words, the highest “sumsales” (170,000) will correspond to the lowest quantile value (0) and visa versa. This is the non-default option. Note: In both cases, the output of the report is in ascending order of the quantile value. An ORDER BY clause can be used to override the report sequencing, but it has no effect on the correlative sequencing between the target column and its quantile projection.

Quantiles and Width Buckets Page 9-12

Page 331: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Varying Quantiles

DESC means as quantile descends, column value descends.It also means as quantile ascends, column ascends as seen.This is the default for any quantile function.Reported output will always show quantile ascending.

DESC means as quantile descends, column value descends.It also means as quantile ascends, column ascends as seen.This is the default for any quantile function.Reported output will always show quantile ascending.

ASC means as quantile descends, column value ascends.Lower quantiles get extra rows if distribution is not uniform.ASC means as quantile descends, column value ascends.Lower quantiles get extra rows if distribution is not uniform.

SELECT t.prodid, t.sumsales, QUANTILE (3,sumsales DESC)FROM (SELECT a.prodid, SUM(a.sales)

FROM salestbl aGROUP BY 1) AS t(prodid, sumsales);

SELECT t.prodid, t.sumsales, QUANTILE (3,sumsales DESC)FROM (SELECT a.prodid, SUM(a.sales)

FROM salestbl aGROUP BY 1) AS t(prodid, sumsales);

Show each product tertile based on total sales:

SELECT t.prodid, t.sumsales, QUANTILE (3,sumsales ASC)FROM (SELECT a.prodid, SUM(a.sales)

FROM salestbl aGROUP BY 1) AS t(prodid, sumsales);

SELECT t.prodid, t.sumsales, QUANTILE (3,sumsales ASC)FROM (SELECT a.prodid, SUM(a.sales)

FROM salestbl aGROUP BY 1) AS t(prodid, sumsales);

prodid sumsales Quantile B 65000.00 0 D 110000.00 0 C 115000.00 1 F 150000.00 1 A 170000.00 2

prodid sumsales Quantile B 65000.00 0 D 110000.00 0 C 115000.00 1 F 150000.00 1 A 170000.00 2

prodid sumsales QuantileA 170000.00 0 F 150000.00 0 C 115000.00 1 D 110000.00 1 B 65000.00 2

prodid sumsales QuantileA 170000.00 0 F 150000.00 0 C 115000.00 1 D 110000.00 1 B 65000.00 2

Quantiles and Width Buckets Page 9-13

Page 332: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ordering Quantiles In the top example on the facing page, we notice the quantile function as follows: QUANTILE (100, salary_amount, employee_number) The purpose of the “employee_number” here is to handle a situation where there is a tie on the salary amount. Remember, the “salary_amount” parameter (with a default of DESC) tells us that the salary_amount will descend as the quantile value descends. With the addition of the employee_number, we are stipulating that in the event of a tie on salary_amount, employee_number will also descend as the quantile value descends. The output on this report reflects this stipulation. In the second example on the facing page, we see the quantile function as: QUANTILE (100, salary_amount, employee_number ASC) In this case, the employee_number (in the event of a tie on salary) will ascend as the quantile value descends. This is reflected in the reversal of the two employee numbers from the previous example. Remember, you may always provide explicit reporting sequencing by using an ORDER BY clause.

Quantiles and Width Buckets Page 9-14

Page 333: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Ordering Quantiles

SELECT employee_number, salary_amount,QUANTILE (100, salary_amount , employee_number)FROM employee

QUALIFY QUANTILE(100, salary_amount) < 25;

SELECT employee_number, salary_amount,QUANTILE (100, salary_amount , employee_number)FROM employee

QUALIFY QUANTILE(100, salary_amount) < 25;

Show all employees in lowest 25th percentile of salaries:

• Default for quantiles: Column descends as quantile descends.

• Reporting output default is ascending quantile.• If salary has a tie, employee # descends as quantile

descends.• Explicit ORDER BY may be used to reorder results.

• Default for quantiles: Column descends as quantile descends.

• Reporting output default is ascending quantile.• If salary has a tie, employee # descends as quantile

descends.• Explicit ORDER BY may be used to reorder results.

If salary has a tie, employee # ascends as quantile descends.If salary has a tie, employee # ascends as quantile descends.

SELECT employee_number, salary_amount,QUANTILE (100, salary_amount , employee_number ASC) FROM employee

QUALIFY QUANTILE(100, salary_amount) < 25;

SELECT employee_number, salary_amount,QUANTILE (100, salary_amount , employee_number ASC) FROM employee

QUALIFY QUANTILE(100, salary_amount) < 25;

emp_num salary_amt Quantile1014 24500.00 03 1013 24500.00 001001 25525.00 07 1023 26500.00 11 1008 29250.00 15 1006 29450.00 19 1009 31000.00 23

emp_num salary_amt Quantile1014 24500.00 03 1013 24500.00 001001 25525.00 07 1023 26500.00 11 1008 29250.00 15 1006 29450.00 19 1009 31000.00 23

emp_num salary_amt Quantile 1013 24500.00 031014 24500.00 001001 25525.00 07 1023 26500.00 11 1008 29250.00 15 1006 29450.00 19 1009 31000.00 23

emp_num salary_amt Quantile 1013 24500.00 031014 24500.00 001001 25525.00 07 1023 26500.00 11 1008 29250.00 15 1006 29450.00 19 1009 31000.00 23

Quantiles and Width Buckets Page 9-15

Page 334: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

WIDTH_BUCKET (1 of 3) The WIDTH_BUCKET function assigns partition numbers to values associated with a column or expression, based on a specified range and based on the number of partitions desired. Like many OLAP and aggregate functions, WIDTH_BUCKET is used to extract statistical information from sample populations. The format of the function is as follows:

WIDTH_BUCKET (column_expr, lower_bound, upper_bound, partition_count) Where:

Column expression is the column or expression containing the values. Partition count is the number of partitions specified for the range. Lower bound is the lowest value in the range to be partitioned. Upper bound is the highest value in the range to be partitioned.

The actual number or partitions created is always equal to n + 2 where n is the specified partition_count. This is because:

• A zero partition is defined for values below the lower bound. • An n + 1 partition is defined for values greater than or equal to the upper bound.

The following shows the actual partition assignments for the example seen here. Bucket Range --------- ---------------------- 0 0 - 99,999 1 100,000 - 199,999 2 200,000 - 299,999 3 300,000 - 399,999 4 400,000 - 499,999 5 500,000 - 599,999 6 600,000 - 699,999 7 700,000 - 799,999 8 800,000 - 899,999 9 900,000 - 999,999 10 1,000,000+

Quantiles and Width Buckets Page 9-16

Page 335: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

WIDTH_BUCKET (1 of 3)The WIDTH_BUCKET function assigns partition number to values based on a specified range and the number of desired partitions.

Create 9 departmental budget partitions, with $100,000 as the lower bound and $1,000,000 as upper bound.

SELECT department_number AS Dept ,budget_amount AS Budget,WIDTH_BUCKET(budget_amount,100000,1000000,9)

AS WB FROM department ORDER BY 3,2;

SELECT department_number AS Dept ,budget_amount AS Budget,WIDTH_BUCKET(budget_amount,100000,1000000,9)

AS WB FROM department ORDER BY 3,2;

• Each bucket represents a range of about 100,000 values.• Any null amount is always assigned a null partition.• Number of buckets created is always partition_count + 2• Eleven buckets are actually created but not all are used.• There are no values assigned to buckets 0, 1, 5, 6, 7, 8, or 10.

• Each bucket represents a range of about 100,000 values.• Any null amount is always assigned a null partition.• Number of buckets created is always partition_count + 2• Eleven buckets are actually created but not all are used.• There are no values assigned to buckets 0, 1, 5, 6, 7, 8, or 10.

WIDTH_BUCKET(column, lower_bound, upper_bound, partition_count)WIDTH_BUCKET(column, lower_bound, upper_bound, partition_count)

Dept Budget WB600 ? ?302 226000.00 2201 293800.00 2402 308000.00 3501 308000.00 3100 400000.00 4301 465600.00 4403 932000.00 9401 982300.00 9

Dept Budget WB600 ? ?302 226000.00 2201 293800.00 2402 308000.00 3501 308000.00 3100 400000.00 4301 465600.00 4403 932000.00 9401 982300.00 9

Quantiles and Width Buckets Page 9-17

Page 336: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

WIDTH_BUCKET (2 of 3) The following are points to consider for this example.

• Each bucket again represents a range of about 100,000 values. • Eight buckets are actually created but not all are used. • Values below the lower bound are assigned partition #0. • Values greater than or equal to the upper bound are in partition #7.

The following shows the actual partition assignments for this example. Bucket Range ---------- --------------------------- 0 0 - 299,999 1 300,000 - 399,999 2 400,000 - 499,999 3 500,000 - 599,999 4 600,000 - 699,999 5 700,000 - 799,999 6 800,000 - 899,999 7 900,000+

Quantiles and Width Buckets Page 9-18

Page 337: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

WIDTH_BUCKET (2 of 3)

Partition the departmental budgets into 6 partitions, with $300,000 as the lower bound and $900,000 as the upper bound.

SELECT department_number AS Dept ,budget_amount AS Budget,WIDTH_BUCKET(budget_amount,300000,900000,6)

AS WBFROM department ORDER BY 3,2;

SELECT department_number AS Dept ,budget_amount AS Budget,WIDTH_BUCKET(budget_amount,300000,900000,6)

AS WBFROM department ORDER BY 3,2;

Dept Budget WB600 ? ?302 226000.00 0201 293800.00 0402 308000.00 1501 308000.00 1100 400000.00 2301 465600.00 2403 932000.00 7401 982300.00 7

Dept Budget WB600 ? ?302 226000.00 0201 293800.00 0402 308000.00 1501 308000.00 1100 400000.00 2301 465600.00 2403 932000.00 7401 982300.00 7

• Each bucket again represents a range of about 100,000 values.• Eight buckets are actually created but not all are used.• Values below the lower bound are assigned partition #0.• Values greater than or equal to upper bound are in partition #7• There are no values assigned to buckets #3, 4, 5 or 6.

• Each bucket again represents a range of about 100,000 values.• Eight buckets are actually created but not all are used.• Values below the lower bound are assigned partition #0.• Values greater than or equal to upper bound are in partition #7• There are no values assigned to buckets #3, 4, 5 or 6.

Quantiles and Width Buckets Page 9-19

Page 338: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

WIDTH_BUCKET (3 of 3) The following are points to consider for this example.

• Each bucket represents a range of about 200,000 values. • Seven buckets are actually created but not all are used. • No values are below the lower bound, thus no partition #0. • No values are below $200,000, thus no partition #1 assignments. • No values exceed $1,000,000, thus no partition #6 assignments.

Quantiles and Width Buckets Page 9-20

Page 339: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

WIDTH_BUCKET (3 of 3)

Partition the departmental budgets into 5 partitions, with $0 as the lower bound and $1,000,000 as the upper bound.

SELECT department_number AS Dept ,budget_amount AS Budget,WIDTH_BUCKET(budget_amount,0,1000000,5)

AS WB FROM department ORDER BY 3,2;

SELECT department_number AS Dept ,budget_amount AS Budget,WIDTH_BUCKET(budget_amount,0,1000000,5)

AS WB FROM department ORDER BY 3,2;

Dept Budget WB600 ? ?302 226000.00 2201 293800.00 2402 308000.00 2501 308000.00 2100 400000.00 3301 465600.00 3403 932000.00 5401 982300.00 5

Dept Budget WB600 ? ?302 226000.00 2201 293800.00 2402 308000.00 2501 308000.00 2100 400000.00 3301 465600.00 3403 932000.00 5401 982300.00 5

• Each bucket represents a range of about 200,000 values.• Seven buckets are actually created but not all are used.• No values are below the lower bound, thus no partition #0.• No values are below $200,000 thus no partition #1 assignment.• No values exceed $1,000,000 thus no partition #6 assignment.

• Each bucket represents a range of about 200,000 values.• Seven buckets are actually created but not all are used.• No values are below the lower bound, thus no partition #0.• No values are below $200,000 thus no partition #1 assignment.• No values exceed $1,000,000 thus no partition #6 assignment.

Quantiles and Width Buckets Page 9-21

Page 340: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercises

Quantiles and Width Buckets Page 9-22

Page 341: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercises

CSUM RANKMSUM QUANTILEMDIFF PERCENT_RANKMAVG WIDTH_BUCKET

Pick the appropriate function(s) w hich would be needed to solve the following problems:

1. What are the three highest moving averages during February?2. What percentage of stores took in less than $25 million last year?3. Show sales figures for March incremented daily with a final total.4. Show the disparity between sales on Saturday’s in Q1.5. What percent of stores rank ahead of those who make $1 million per day?6. Given a selection of six products, what is the moving average of

their product sales for the twelve months of 2003?7. Divide our stores into 8 partitions based on their yearly sales.

Quantiles and Width Buckets Page 9-23

Page 342: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

Quantiles and Width Buckets Page 9-24

Page 343: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

1.) Produce a listing of all employees who are in the top twenty percentile of salary amount. Show the employee number, salary amount, and quantile.

2.) Modify query #1 to show the bottom twenty percentile. Order by descending quantile.

3.) Create a listing of all employee salaries from the employee table, and a partition assignment using the WIDTH_BUCKET function. Use a lower bound of zero(0), and upper bound of 50,000 and a partition count of five (5).

Optional Lab Exercise

4.) Produce a distinct list of departments in which employees with the top twenty percentile of salary reside. Do this in a single query.

Quantiles and Width Buckets Page 9-25

Page 344: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Quantiles and Width Buckets Page 9-26

Page 345: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 10

Module 10Advanced Sampling and Random Functions

After completing this module, you should be able to:

• Use the SAMPLE function to generate sample data.

• Use the RANDOM function to generate random numbers.

• Use the RANDOM function to generate random samples.

• Use the RANDOM function to generate test data.

Advanced Sampling and Random Functions Page 10-1

Page 346: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Advanced Sampling and Random Functions Page 10-2

Page 347: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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 Exercises ...........................................................................................................................10-36

Advanced Sampling and Random Functions Page 10-3

Page 348: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sampling The SAMPLE function allows a user to specify a sample of results returned by a specified SELECT statement. The sample can be represented as either a percentage of the returned rows or as an absolute number. Sampling is useful for many purposes, including generating samples of production data for purposes of testing out new applications. This may be done using one of two techniques: Technique #1 SAMPLE n -- where n is an integer will yield a sample of n rows If the number n is greater than the number of rows in the table, the sample will consist of the number of rows in the table. Rows are not reused within the same sample. Technique #2 SAMPLE n -- where n is a decimal value less than 1.00 and greater than .00 Here a fraction of the rows is returned based on the decimal value. Note that calculations resulting in fractional rows must be greater than or equal to .5 to actually return a row. In the example at hand, 25% of the rows of the employee table are to be returned. The employee table has 26 rows. 26 * .25 = 6.50 = 7 rows in the sample

Advanced Sampling and Random Functions Page 10-4

Page 349: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sampling

SAMPLE function allows sampling of data based on either:• An actual number of rows• A percentage of a table

SAMPLE function allows sampling of data based on either:• An actual number of rows• A percentage of a table

department_number401 401 403 401 301 401 403 402 401 401

department_number401 401 403 401 301 401 403 402 401 401

SELECT department_numberFROM employeeSAMPLE 10;

SELECT department_numberFROM employeeSAMPLE 10;

department_number403 401 402 301 501 403302

department_number403 401 402 301 501 403302

SELECT department_numberFROM employeeSAMPLE .25;

SELECT department_numberFROM employeeSAMPLE .25;

7 Rows (out of 26)

10 Rows

Get a sampling of department numbers from 10 employees:

Get a sampling of department numbers from 25% of employees:

Note: Fractional results must be > = .5 to generate an added row.Note: Fractional results must be > = .5 to generate an added row.

Advanced Sampling and Random Functions Page 10-5

Page 350: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sampling Distinct Values In the first example, we are asking for a sampling of 13 employees and counting how many distinct departments they represent. In fact, the query does not produce this result. All employee rows are considered and the number of distinct departments is counted to be 8. The SAMPLE 13 indicates that 13 rows of output are expected. Clearly a COUNT function can only produce a single row of output, thus a warning message is generated in the BTEQ environment. The warning is not seen in the ODBC environment. To correctly satisfy this request, a temporary or derived table must be used to first capture the 13 row sample, then to perform the count against the sample. This procedure is shown in the second example on the facing page. Repeated executions of the query may produce different results based on different sample sets.

Advanced Sampling and Random Functions Page 10-6

Page 351: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sampling Distinct Values

Count(Distinct(department_number))8

Count(Distinct(department_number))8

SELECT COUNT(DISTINCT department_number) FROM employee SAMPLE 13;SELECT COUNT(DISTINCT department_number) FROM employee SAMPLE 13;

Get a sampling of 13 employees and count the distinct department numbers they represent:

Warning because attempts to return 13 rows.Only one row returned for count.Sample is based on entire employee table, not just 13 rows.

Warning because attempts to return 13 rows.Only one row returned for count.Sample is based on entire employee table, not just 13 rows.

SELECT COUNT(DISTINCT dept) FROM (SELECT department_number FROM employee SAMPLE 13)

temp(dept);

SELECT COUNT(DISTINCT dept) FROM (SELECT department_number FROM employee SAMPLE 13)

temp(dept);

Solution to problem -- use a derived table to get sample: Count(Distinct(dept))6

Count(Distinct(dept))6

Count(Distinct(dept))5

Count(Distinct(dept))5

First try

Second try

Derived table produces sample of 13 rows.DISTINCT is applied against derived table.Subsequent attempts will return different samples.Different samples will produce different results.

Derived table produces sample of 13 rows.DISTINCT is applied against derived table.Subsequent attempts will return different samples.Different samples will produce different results.

*** Warning: 7473 Requested sample is larger than table rows. All rows returned.*** Warning: 7473 Requested sample is larger than table rows. All rows returned.

Advanced Sampling and Random Functions Page 10-7

Page 352: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Advanced Sampling Features The sample function has the following additional features:

• Stratified sampling: T he ability to generate different sized samples from different groupings of the data.

• Replacement sampling: T the ability to sample a row and return it to the pool of rows from which it came, making it again a candidate for sampling.

• Randomized sampling: The ability to sample randomly across the entire system instead of each AMP returning an equal share of the sample.

Advanced Sampling and Random Functions Page 10-8

Page 353: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Advanced Sampling Features

The sample function has the following additional features:

• Stratified sampling: The ability to generate different sized samples from different groupings of the data.

• Replacement sampling: The ability to sample a row and return it to the pool of rows from which it came, making it again a candidate for sampling.

• Ranndomized sampling: The ability to sample randomly across the entire system instead of each AMP returning an equal share of the sample.

Advanced Sampling and Random Functions Page 10-9

Page 354: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Stratified Sampling Setup Stratified samplings permit differing sampling criteria to be applied to different sets of rows, all within a single query. To demonstrate the capabilities of stratified sampling, the example on the facing page is set up for the examples to follow.

Advanced Sampling and Random Functions Page 10-10

Page 355: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Stratified Sampling Setup

Create five groupings of employees based on the range of department numbers.

SELECT CASE WHEN department_number BETWEEN 100 AND 199 THEN 'GROUP 1'WHEN department_number BETWEEN 200 AND 299 THEN 'GROUP 2'WHEN department_number BETWEEN 300 AND 399 THEN 'GROUP 3'WHEN department_number BETWEEN 400 AND 499 THEN 'GROUP 4'WHEN department_number BETWEEN 500 AND 599 THEN 'GROUP 5'ELSE NULL END AS Group_Name,COUNT (*)

FROM employeeGROUP BY 1ORDER BY 1;

SELECT CASE WHEN department_number BETWEEN 100 AND 199 THEN 'GROUP 1'WHEN department_number BETWEEN 200 AND 299 THEN 'GROUP 2'WHEN department_number BETWEEN 300 AND 399 THEN 'GROUP 3'WHEN department_number BETWEEN 400 AND 499 THEN 'GROUP 4'WHEN department_number BETWEEN 500 AND 599 THEN 'GROUP 5'ELSE NULL END AS Group_Name,COUNT (*)

FROM employeeGROUP BY 1ORDER BY 1;

Group_Name Count(*)GROUP 1 1GROUP 2 2GROUP 3 4GROUP 4 15GROUP 5 4

Group_Name Count(*)GROUP 1 1GROUP 2 2GROUP 3 4GROUP 4 15GROUP 5 4

Advanced Sampling and Random Functions Page 10-11

Page 356: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Stratified Sampling Example The stratified sampling example on the facing page shows how a sampling percentage can be applied to each group within a defined set.

Advanced Sampling and Random Functions Page 10-12

Page 357: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Stratified Sampling ExampleFor each defined grouping assign a sample-id, and take a 25% sample from each group.

SELECT last_name (CHAR(10)),department_number (FORMAT '999') AS Deptno, SAMPLEIDFROM employee SAMPLE

WHEN department_number BETWEEN 100 AND 199 THEN .25WHEN department_number BETWEEN 200 AND 299 THEN .25WHEN department_number BETWEEN 300 AND 399 THEN .25WHEN department_number BETWEEN 400 AND 499 THEN .25WHEN department_number BETWEEN 500 AND 599 THEN .25END

ORDER BY 2;

SELECT last_name (CHAR(10)),department_number (FORMAT '999') AS Deptno, SAMPLEIDFROM employee SAMPLE

WHEN department_number BETWEEN 100 AND 199 THEN .25WHEN department_number BETWEEN 200 AND 299 THEN .25WHEN department_number BETWEEN 300 AND 399 THEN .25WHEN department_number BETWEEN 400 AND 499 THEN .25WHEN department_number BETWEEN 500 AND 599 THEN .25END

ORDER BY 2; last_name Deptno SampleIdShort 201 2 Stein 301 3 Phillips 401 4 Rogers 401 4Villegas 403 4Hopkins 403 4Ratzlaff 501 5

last_name Deptno SampleIdShort 201 2 Stein 301 3 Phillips 401 4 Rogers 401 4Villegas 403 4Hopkins 403 4Ratzlaff 501 5

• SAMPLEID reflects the sample pool into which each row falls.• In some cases, the fractional amount is less than a full row, thus no rows are

returned for that sampling.• No rows are returned for SAMPLEID 1 because 25% of one row rounds to zero.

• SAMPLEID reflects the sample pool into which each row falls.• In some cases, the fractional amount is less than a full row, thus no rows are

returned for that sampling.• No rows are returned for SAMPLEID 1 because 25% of one row rounds to zero.

Things to notice:

(.25 * 2 = .50 ROUNDING TO 1)(.25 * 4 = 1)(.25 * 15 = 3.75 ROUNDING TO 4)

(.25 * 4 = 1)

Advanced Sampling and Random Functions Page 10-13

Page 358: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sampling with Replacement (1 of 2) The SAMPLE function can be used with replacement sampling - the ability to sample a row and return it to the pool of rows from which it came, making it again a candidate for sampling.

Advanced Sampling and Random Functions Page 10-14

Page 359: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sampling with Replacement (1 of 2)Perform the same sampling seen previously, but with replacement sampling.

SELECT last_name (CHAR(10)),department_number (FORMAT '999') AS Deptno, SAMPLEID

FROM employeeSAMPLE WITH REPLACEMENT

WHEN department_number BETWEEN 100 AND 199 THEN .50WHEN department_number BETWEEN 200 AND 299 THEN .24WHEN department_number BETWEEN 300 AND 399 THEN .24WHEN department_number BETWEEN 400 AND 499 THEN .19WHEN department_number BETWEEN 500 AND 599 THEN .88END

ORDER BY 2;

SELECT last_name (CHAR(10)),department_number (FORMAT '999') AS Deptno, SAMPLEID

FROM employeeSAMPLE

WHEN department_number BETWEEN 100 AND 199 THEN .50WHEN department_number BETWEEN 200 AND 299 THEN .24WHEN department_number BETWEEN 300 AND 399 THEN .24WHEN department_number BETWEEN 400 AND 499 THEN .19WHEN department_number BETWEEN 500 AND 599 THEN .88END

ORDER BY 2; last_name

WITH REPLACEMENT

Deptno SampleIdTrainer 100 1Rogers 302 3Johnson 401 4Phillips 401 4Lombardo 403 4Rabbit 501 5Wilson 501 5Ratzlaff 501 5Ratzlaff 501 5

last_name Deptno SampleIdTrainer 100 1Rogers 302 3Johnson 401 4Phillips 401 4Lombardo 403 4Rabbit 501 5Wilson 501 5Ratzlaff 501 5Ratzlaff 501 5

• Because WITH REPLACEMENT is specified, duplicate rows are possible within a grouping.

• Thus, Ratzlaff appears twice in Sample-id #5.• Sampling results can not be replicated - they

will vary.

• Because WITH REPLACEMENT is specified, duplicate rows are possible within a grouping.

• Thus, Ratzlaff appears twice in Sample-id #5.• Sampling results can not be replicated - they

will vary.

Things to notice:

Advanced Sampling and Random Functions Page 10-15

Page 360: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sampling with Replacement (2 of 2) Another example of replacement sampling is demonstrated.

Advanced Sampling and Random Functions Page 10-16

Page 361: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sampling with Replacement (2 of 2)Create a sample consisting of two samples of 4 rows each from Group 4 and two samples of 2 rows each from Group 5. Use replacement sampling.

SELECT last_name (CHAR(10)),department_number, SAMPLEID

FROM employeeSAMPLE WITH REPLACEMENTWHEN department_number BETWEEN 400 AND 499 THEN 4, 4WHEN department_number BETWEEN 500 AND 599 THEN 2, 2END

ORDER BY 3, 2, 1;

SELECT last_name (CHAR(10)),department_number, SAMPLEID

FROM employeeSAMPLEWHEN department_number BETWEEN 400 AND 499 THEN 4, 4WHEN department_number BETWEEN 500 AND 599 THEN 2, 2END

ORDER BY 3, 2, 1;last_name

WITH REPLACEMENT

department_number SampleIdJohnson 401 1Rogers 401 1Hopkins 403 1Ryan 403 1Machado 401 2Rogers 401 2Hopkins 403 2Villegas 403 2Rabbit 501 3Wilson 501 3Rabbit 501 4Wilson 501 4

last_name department_number SampleIdJohnson 401 1Rogers 401 1Hopkins 403 1Ryan 403 1Machado 401 2Rogers 401 2Hopkins 403 2Villegas 403 2Rabbit 501 3Wilson 501 3Rabbit 501 4Wilson 501 4

Things to Notice:• WITH REPLACEMENT makes the samples

non-mutually exclusive.• Duplication within and across groups is

possible.• Rogers and Hopkins show up in both

samples 1 and 2.• Wilson and Rabbit show up in both

samples 3 and 4. • Rerunning this query will produce differing

results.

Things to Notice:• WITH REPLACEMENT makes the samples

non-mutually exclusive.• Duplication within and across groups is

possible.• Rogers and Hopkins show up in both

samples 1 and 2.• Wilson and Rabbit show up in both

samples 3 and 4. • Rerunning this query will produce differing

results.

Advanced Sampling and Random Functions Page 10-17

Page 362: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sample Function with Randomization The SAMPLE function can be used with randomized sampling - the ability to sample randomly across the entire system instead of each AMP returning an equal share of the sample. In most cases, the difference in the data sampled with randomized versus non-randomized sampling will not be immediately apparent. The randomizing feature does however provide a more mathematically robust randomization capability, than does AMP-localized randomization.

• The WITH REPLACEMENT clause again makes the samples non-mutually exclusive. • Duplication within and across groups is possible. • The samples are generated system-wide, not AMP proportional. • The results do not reflect this difference in any tangible way.

Advanced Sampling and Random Functions Page 10-18

Page 363: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Sample Function with RandomizationThe SAMPLE function can be used with randomized sampling.

This capability samples randomly across the entire system instead of each AMP returning an equal share of the sample.

Rerun the previous query, this time using randomized allocation.

SELECT last_name (CHAR(10)), department_number ,SAMPLEIDFROM employeeSAMPLE WITH REPLACEMENT RANDOMIZED ALLOCATIONWHEN department_number BETWEEN 400 AND 499 THEN 4, 4WHEN department_number BETWEEN 500 AND 599 THEN 2,2END ORDER BY 3, 2, 1;

SELECT last_name (CHAR(10)), department_number ,SAMPLEIDFROM employeeSAMPLE WITH REPLACEMENT WHEN department_number BETWEEN 400 AND 499 THEN 4, 4WHEN department_number BETWEEN 500 AND 599 THEN 2,2END ORDER BY 3, 2, 1;

last_name

RANDOMIZED ALLOCATION

department_number SampleIdPhillips 401 1Phillips 401 1Phillips 401 1Lombardo 403 1Brown 401 2Phillips 401 2Rogers 401 2Crane 402 2Rabbit 501 3Rabbit 501 3Rabbit 501 4Ratzlaff 501 4

last_name department_number SampleIdPhillips 401 1Phillips 401 1Phillips 401 1Lombardo 403 1Brown 401 2Phillips 401 2Rogers 401 2Crane 402 2Rabbit 501 3Rabbit 501 3Rabbit 501 4Ratzlaff 501 4

• WITH REPLACEMENT clause makes samples non-mutually exclusive.

• Duplication within and across groups is possible.

• The samples are generated system-wide, not AMP proportional.

• The results do not reflect this difference in any tangible way.

• WITH REPLACEMENT clause makes samples non-mutually exclusive.

• Duplication within and across groups is possible.

• The samples are generated system-wide, not AMP proportional.

• The results do not reflect this difference in any tangible way.

Advanced Sampling and Random Functions Page 10-19

Page 364: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

RANDOM Function The RANDOM function may be used to generate a random number between a specified range. RANDOM (Lower limit, Upper limit) returns a random number between the lower and upper limits inclusive. Both limits must be specified. The function can return an integer between the following two limits: -2147483648 to 2147483647 RANDOM is a Teradata extension to the ANSI SQL-99 standard. Consider the department table, which consists of nine rows.

SELECT department_number FROM department;

department_number --------------------------- 501 301 201 600 100 402 403 302 401

Example: Assign a random number between 1 and 9 to each department.

SELECT department_number, RANDOM(1,9) FROM department; department_number Random(1,9) ---------------------------- ----------------- 501 2 301 6 201 3 600 7 100 3 402 2 403 1 302 5 401 1

Note it is possible for random numbers to repeat. The RANDOM function is activated for each row processed, thus duplicate random values are possible.

Advanced Sampling and Random Functions Page 10-20

Page 365: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

RANDOM Function

The RANDOM function generates a random integer between a specified range.

RANDOM (Lower limit, Upper limit) returns a random number between the lower and upper limits inclusive. Both limits must be specified.

Note: It is possible for random numbers to repeat. The RANDOM function is activated for each row processed, thus duplicate random values are possible.

Note: It is possible for random numbers to repeat. The RANDOM function is activated for each row processed, thus duplicate random values are possible.

department_number Random(1,9)501 2301 6201 3600 7100 3402 2403 1302 5401 1

department_number Random(1,9)501 2301 6201 3600 7100 3402 2403 1302 5401 1

SELECT department_number,RANDOM(1,9) FROM department;

SELECT department_number,RANDOM(1,9) FROM department;

Assign a random number between 1 and 9 to each department.

Advanced Sampling and Random Functions Page 10-21

Page 366: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Duplicate RANDOM Values (1 of 2) Duplicate value likelihood may be reduced by increasing the size of the RANDOM interval relative to the size of the table. Example: Assign a random number between 1 and 100 to each department.

SELECT department_number, RANDOM(1,100) from department; department_number Random(1,100) ----------------- ------------- 501 15 301 19 201 71 600 75 100 61 402 41 403 81 302 31 401 59 Note that no duplicates were generated because the pool of possible values is over ten times the number of rows to be assigned.

Advanced Sampling and Random Functions Page 10-22

Page 367: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Duplicate RANDOM Values (1 of 2)

Duplicate value likelihood may be reduced by increasing the size of the RANDOM interval relative to the size of the table.

Note that no duplicates were generated because the pool of possible values is over ten times the number of rows to be assigned.Note that no duplicates were generated because the pool of possible values is over ten times the number of rows to be assigned.

department_number Random(1,100)501 15301 19201 71600 75100 61402 41403 81302 31401 59

department_number Random(1,100)501 15301 19201 71600 75100 61402 41403 81302 31401 59

SELECT department_number, RANDOM(1,100)

FROM department;

SELECT department_number, RANDOM(1,100)

FROM department;

Assign a random number between 1 and 100 to each department.

Advanced Sampling and Random Functions Page 10-23

Page 368: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Duplicate RANDOM Values (2 of 2) Duplicate random values can be increased, by decreasing the size of the RANDOM interval relative to the size of the table. Example: Assign a random number between 1 and 3 to each department.

SELECT department_number, RANDOM(1,3) FROM department; department_number Random(1,3) --------------------------- ----------------- 501 2 301 3 201 3 600 1 100 3 402 2 403 1 302 2 401 1 With only three values to distribute over nine rows, duplicates are necessary.

Advanced Sampling and Random Functions Page 10-24

Page 369: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Duplicate RANDOM Values (2 of 2)

Duplicate random values can be increased, by decreasing the size of the RANDOM interval relative to the size of the table.

With only three values to distribute over nine rows, duplicates are necessary.With only three values to distribute over nine rows, duplicates are necessary.

department_number Random(1,3)501 2301 3201 3600 1100 3402 2403 1302 2401 1

department_number Random(1,3)501 2301 3201 3600 1100 3402 2403 1302 2401 1

SELECT department_number, RANDOM(1,3)

FROM department;

SELECT department_number, RANDOM(1,3)

FROM department;

Assign a random number between 1 and 3 to each department.

Advanced Sampling and Random Functions Page 10-25

Page 370: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using RANDOM for Sampling Consider the following distribution of employee salaries. Salary Range Count ------------- ------- $ 0 to < $30K 6 $30 to < $40K 9 $40 to < $50K 4 $50K + 7 Problem: Select a sample representing two thirds of the employees making under $30,000. Use the RANDOM function to accomplish this. Solution 1: SELECT employee_number, salary_amount FROM employee WHERE (salary_amount < 30000 AND RANDOM(1,3) < 3); employee_number salary_amount --------------- ------------- 1006 29450.00 1023 26500.00 1013 24500.00 Because of the nature of random number generation, we end up with a 50% sample (3 out of 6) instead of a 67% sample (4 out of 6).

Advanced Sampling and Random Functions Page 10-26

Page 371: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using RANDOM for Sampling

Consider the following distribution of employee salaries.

Because of the nature of random number generation, we end up with a 50% sample (3 out of 6) instead of a 67% sample (4 out of 6). The RANDOM function is not appropriate for generating precise sampling.

Because of the nature of random number generation, we end up with a 50% sample (3 out of 6) instead of a 67% sample (4 out of 6). The RANDOM function is not appropriate for generating precise sampling.

employee_number salary_amount1006 29450.001023 26500.001013 24500.00

employee_number salary_amount1006 29450.001023 26500.001013 24500.00

SELECT employee_number,salary_amount

FROM employeeWHERE (salary_amount < 30000 AND RANDOM(1,3) < 3);

SELECT employee_number,salary_amount

FROM employeeWHERE (salary_amount < 30000 AND RANDOM(1,3) < 3);

Solution 1:

Select a sample representing two thirds of the employees making under $30,000. Use the RANDOM function to accomplish this.

Salary Range Count$ 0 to < $30K 6$30 to < $40K 9$40 to < $50K 4$50K + 7

Advanced Sampling and Random Functions Page 10-27

Page 372: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using SAMPLE for Sampling A sample can also be generated and with more accuracy using the SAMPLE function. Solution 2: SELECT employee_number, salary_amount FROM employee WHERE salary_amount < 30000 SAMPLE .67; employee_number salary_amount --------------- ------------- 1006 29450.00 1023 26500.00 1008 29250.00 1014 24500.00

Advanced Sampling and Random Functions Page 10-28

Page 373: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using SAMPLE for Sampling

A sample can also be generated and with more accuracy using the SAMPLE function. Here, the SAMPLE function is used to solve the same problem seen previously.

employee_number salary_amount1006 29450.001023 26500.001008 29250.001014 24500.00

employee_number salary_amount1006 29450.001023 26500.001008 29250.001014 24500.00

SELECT employee_number,salary_amount

FROM employee WHERE salary_amount < 30000 SAMPLE .67;

SELECT employee_number,salary_amount

FROM employee WHERE salary_amount < 30000 SAMPLE .67;

Solution 2:

In this case, we end up with a 67% sample (4 out of 6).

The SAMPLE function is a better choice for generating precise samples.

In this case, we end up with a 67% sample (4 out of 6).

The SAMPLE function is a better choice for generating precise samples.

Advanced Sampling and Random Functions Page 10-29

Page 374: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using RANDOM for Sample Data The RANDOM function is particularly useful for generating sample data for relational tables.

Advanced Sampling and Random Functions Page 10-30

Page 375: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using RANDOM for Sample Data

RANDOM function is a useful tool for generation of test data.RANDOM function is a useful tool for generation of test data.

CREATE TABLE dept_test(department_number SMALLINT,budget_amount DECIMAL(10,2))

UNIQUE PRIMARY INDEX ( department_number );

CREATE TABLE dept_test(department_number SMALLINT,budget_amount DECIMAL(10,2))

UNIQUE PRIMARY INDEX ( department_number );

Create a test table of department numbers and budget amounts.

Populate the table with existing department numbers and a randomly generated budget amount between $200,000 and $1,000,000.

INSERT INTO dept_testSELECT department_number

,RANDOM (200000,1000000)FROM department;

INSERT INTO dept_testSELECT department_number

,RANDOM (200000,1000000)FROM department;

Select the rows to see the generated data.

SELECT * FROM dept_testORDER BY 1;

SELECT * FROM dept_testORDER BY 1;

dept_num budget_amt100 499038.00201 883477.00301 547601.00302 963964.00401 779233.00402 312297.00403 201842.00501 381087.00600 748086.00

dept_num budget_amt100 499038.00201 883477.00301 547601.00302 963964.00401 779233.00402 312297.00403 201842.00501 381087.00600 748086.00

Advanced Sampling and Random Functions Page 10-31

Page 376: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Limitations on Use of RANDOM RANDOM is non-ANSI standard RANDOM may be used in a SELECT list or a WHERE clause, but not both RANDOM may be used in Updating, Inserting or Deleting rows RANDOM may not be used with aggregate or OLAP functions RANDOM cannot be referenced by numeric position in a GROUP BY or ORDER BY clause

Advanced Sampling and Random Functions Page 10-32

Page 377: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Limitations on Use of RANDOM

• RANDOM is non-ANSI standard.

• It supports integers between -2147483648 and +2147483647 only.

• RANDOM may be used in a SELECT list or a WHERE clause, but not both.

• RANDOM may be used in Updating, Inserting or Deleting rows.

• RANDOM may not be used to generate a primary index value.

• RANDOM may not be used with aggregate or OLAP functions.

• RANDOM cannot be referenced by numeric position in a GROUP BY orORDER BY clause.

• RANDOM is non-ANSI standard.

• It supports integers between -2147483648 and +2147483647 only.

• RANDOM may be used in a SELECT list or a WHERE clause, but not both.

• RANDOM may be used in Updating, Inserting or Deleting rows.

• RANDOM may not be used to generate a primary index value.

• RANDOM may not be used with aggregate or OLAP functions.

• RANDOM cannot be referenced by numeric position in a GROUP BY orORDER BY clause.

Advanced Sampling and Random Functions Page 10-33

Page 378: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Questions The facing page contains review questions.

Advanced Sampling and Random Functions Page 10-34

Page 379: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Questions

1._____ Sampling may be specified as a number or rows or as a percentage of rows.

2._____ The specified sampling percentage, multiplied by the number of candidate rows, must equal at least .50 for a sample row to be returned.

3._____ SAMPLE output can produce more rows than there are in the source table.

4._____ The RANDOM (0.0 , 20000) is a legal expression

5._____ Duplicate value likelihood may be reduced by increasing the size of the RANDOM interval relative to the size of the table.

6._____ Randomized allocation means that random numbers are generated system-wide, and not AMP proportional.

7._____ SAMPLE may be done using replacement or not, whereas RANDOM is always done with replacement.

8._____ RANDOM may be used in Updating, Inserting or Deleting rows.

The following are True/False questions:

Advanced Sampling and Random Functions Page 10-35

Page 380: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

Advanced Sampling and Random Functions Page 10-36

Page 381: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab1.) Create a table using the following description:

CREATE TABLE userid.empsamp( empno INTEGER, deptno INTEGER, job INTEGER, sampid BYTEINT)

PRIMARY INDEX ( empno );

2a.) Delete the rows from empsamp and repopulate the table with three samples of 15 rows each. Select the resulting table and order by Sample-id. Notice any duplicated employees.

2b.) Delete the rows from empsamp and repopulate the table with three samples of 15 rows each using replacement sampling. Select the resulting table and order by Sample-id.

3.) Create a table as follows:

CREATE TABLE userid.test_data(agent_id INTEGER ,sales_amt DEC(8,2)) ;

Populate the columns as follows: Agent_id - Select a 10% random sample of the 234 agent_ids in the agent_sales table. Sales_amt - Generate a random sales amount between $50,000 and $100,000 for each agent.

(Hint: This can be done in a single step using both the RANDOM and SAMPLE functions.)

4.) Select the rows of the table to validate your sample. Show how many agents in the sample are in each of the $10,000 ranges.

Populate this table with three samples from the employee table, identifiable by sample-id and 1, 2 and 3. Let each sample contain 33% of the rows of the table. Select all columns against the newly populated table and order by results by sample-id.

Advanced Sampling and Random Functions Page 10-37

Page 382: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Advanced Sampling and Random Functions Page 10-38

Page 383: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 11

Module 11Adding Table Constraints and Attributes

After completing this module, you should be able to:

• Create table or column level constraints• Primary key constraints• Unique constraints• Check constraints• Referencing constraints

• Recognize techniques other than constraints for controlling data insertion.

• Recognize and invoke the various forms of column compression.

Adding Table Constraints and Attributes Page 11-1

Page 384: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Adding Table Constraints and Attributes Page 11-2

Page 385: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Adding Table Constraints and Attributes Page 11-3

Page 386: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE Elements When executed, the CREATE TABLE statement creates and stores the table structure definition in the Teradata Data Dictionary. The CREATE TABLE statement includes:

• Create Table options • Column definitions • Table or column-level constraints • Index definitions

In this module, we will focus our attention on column definitions and constraints.

Adding Table Constraints and Attributes Page 11-4

Page 387: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE Elements

CREATE <SET/MULTISET> TABLE employee<Create Table Options><Column Definitions><Constraints><Index Definitions>;

CREATE <SET/MULTISET> TABLE employee<Create Table Options><Column Definitions><Constraints><Index Definitions>;

CREATE TABLE statements have four major elements:

Create Table options: Specify physical attributes of table:FALLBACKJOURNALINGFREESPACEDATABLOCKSIZE

Column attributes: Define attributes for each columnCOMPRESS

Constraints: Define Constraints:

PRIMARY KEYUNIQUECHECKREFERENCES

Index definitions: Specify indexes for data access

Adding Table Constraints and Attributes Page 11-5

Page 388: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Column Definitions You can define up to 2048 columns in a single CREATE TABLE statement. Columns may be added or dropped using the ALTER TABLE command, however the maximum of 2048 cannot be exceeded. You can define the following aspects of each column:

• Column name: Name of the column • Data type: Data type (INTEGER, CHAR, etc.) • Data type Attributes: Teradata extensions for DEFAULT, FORMAT, TITLE, NULL,

CASESPECIFIC, UPPERCASE • Column Storage Attributes: Compress data for NULL or specific value for this column. • Column-level Check Constraint Attributes: Specify legal values for this column.

Adding Table Constraints and Attributes Page 11-6

Page 389: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Column Definitions

Column name: Name of the column

Data type: Declare the column to be a character, byte, numeric, or graphic data type.

Data type Attributes: Specify DEFAULT, FORMAT, AS, TITLE, NULL, CASESPECIFIC, UPPERCASE.

Column Storage Attributes: Compress NULLs or a specified value.

Column-level Constraint Attributes: Specify column constraints at the column level.

Table-level Constraint Attributes: Specify column constraints at the table level

Adding Table Constraints and Attributes Page 11-7

Page 390: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Null Compression The COMPRESS phrase allows values in one or more columns of a permanent table to be compressed to zero space, thus reducing the physical storage required for the table. In the both examples on the facing page, null department numbers will be compressed.

Adding Table Constraints and Attributes Page 11-8

Page 391: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Null Compression

CREATE TABLE emp_data(employee_number INTEGER,department_number INTEGER COMPRESS

:

CREATE TABLE emp_data(employee_number INTEGER,department_number INTEGER

:

Examples:

The COMPRESS phrase let nulls in one or more columns of a permanent table becompressed to zero space, reducing the physical storage required for the table.

Null compression has two variations:

COMPRESS Nulls are compressed to zero space.

COMPRESS NULL Nulls are compressed to zero space.

CREATE TABLE emp_data(employee_number INTEGER,department_number INTEGER

:

COMPRESS

COMPRESS NULL

CREATE TABLE emp_data(employee_number INTEGER,department_number INTEGER

:

In both cases, nulls in the department_number column will be compressed to take zero space in the row.

COMPRESS NULL

In both cases, nulls in the department_number column will be compressed to take zero space in the row.

Adding Table Constraints and Attributes Page 11-9

Page 392: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Value Compression The COMPRESS phrase allows values in a column of a permanent table to be compressed to zero space, thus reducing the physical storage required for the table. In the first example on the facing page, only nulls are compressed. In the second example, both null and 'Savings' account types will be compressed. The value of 'Savings' is written in the table header for the table bank_account_data on each AMP in the system. The compressed value is stored case-sensitive. In the third example, the values 'savings', 'checking', and 'stock' will all be compressed. Each of these values is written to the table header on each AMP. Zero space is taken in the physical row for each of these values. This can significantly reduce the amount of space needed to contain the table. Up to 255 values can be specified on a column for compression. Compression has two primary benefits:

• It reduces system storage costs. • It enhances system performance.

Adding Table Constraints and Attributes Page 11-10

Page 393: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Value Compression

CREATE TABLE emp_data(employee_number INTEGER,department_number INTEGER COMPRESS

:

CREATE TABLE emp_data(employee_number INTEGER,department_number INTEGER

:

CREATE TABLE bank_account_data(customer_id INTEGER,account_type CHAR(10)

:

COMPRESS

COMPRESS 'Savings'

CREATE TABLE bank_account_data(customer_id INTEGER,account_type CHAR(10)

:

Example 1 - Only nulls are compressed.

The COMPRESS phrase allows up to 255 values in a column of a permanent table to be compressed to zero space, thus reducing the physical storage required for the table.

CREATE TABLE bank_account_data(customer_id INTEGER,account_type CHAR(10)

:

COMPRESS 'Savings'

COMPRESS ('Savings','Checking','Stock')

CREATE TABLE bank_account_data(customer_id INTEGER,account_type CHAR(10)

:

Example 2 - 'Savings' and nulls are compressed.

Example 3 - 'Savings', 'Checking', 'Stock' and nulls are compressed.

Two primary benefits:• Reduces system storage costs. • Enhances system performance.

• Nulls are also compressed in addition to the specified values.

• Up to 255 compressed values may be specified per column.

Two primary benefits:• Reduces system storage costs. • Enhances system performance.

COMPRESS ('Savings','Checking','Stock')

Adding Table Constraints and Attributes Page 11-11

Page 394: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Compression Suggestions Examples of Highly Compressible Values Any of the following should be considered candidates for compression, when the frequency of their occurrence is high:

• Nulls • Zeroes • Spaces • Default Values

Suggested Application Columns for Compression Any column with high frequency values, or with a relatively small number of values should be considered a candidate for compression. The following is a list of possible candidate columns.

• State • City • Country • Automobile Make • Credit Card Type • Account Type • First Name • Last Name

Adding Table Constraints and Attributes Page 11-12

Page 395: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Compression Suggestions

Any of the following should be considered candidates for compression, when the frequency of their occurrence is high:

• Nulls • Zeroes • Spaces • Default Values

Examples of highly compressible values:

Any column with a high frequency values, or with a relatively small number of values, should be considered a candidate for compression. The following is a list of possible candidates:

• State • City • Country • Automobile Make • Credit Card Type • Account Type • Frequently occuring first names• Frequently occuring last names

Suggested Application Columns For Compression:

Adding Table Constraints and Attributes Page 11-13

Page 396: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Uppercase Compression The COMPRESS phrase allows values in a column of a permanent table to be compressed to zero space, thus reducing the physical storage required for the table. In the first example, both null and 'Savings' account types will be compressed. The value of 'Savings' is written in the table header for the table bank_account_data on each AMP in the system. The compressed value is stored case-sensitive. In the second example, the UPPERCASE attribute is used to insure that the value ‘Savings’ is always stored in uppercase. Compression will therefore be applied to any variation on the word ‘Savings’, such are ‘savings’, ‘savINGS’ or ‘SAVINGS’. Zero space is taken in the physical row for each of these values. This can significantly reduce the amount of space needed to contain the table. Up to 255 values can be specified on a column for compression. Compression has two primary benefits:

• It reduces system storage costs. • It enhances system performance.

Adding Table Constraints and Attributes Page 11-14

Page 397: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Uppercase Compression

CREATE TABLE bank_account_data(customer_id INTEGER,account_type CHAR(10) COMPRESS 'Savings'

:

CREATE TABLE bank_account_data(customer_id INTEGER,account_type CHAR(10)

:

Values specified in a COMPRESS clause will be compressed only if the specified case matches the case of the input value.

Example 1

Because the value will always be stored as uppercase, compression will always occur.

COMPRESS 'Savings'

Because the value will always be stored as uppercase, compression will always occur.

INSERT INTO bank_account_date VALUES (9001, ’Savings’…..)INSERT INTO bank_account_date VALUES (9001, ’Savings’…..) Compression occurs.

INSERT INTO bank_account_date VALUES (9001, ’SAVINGS’…..)INSERT INTO bank_account_date VALUES (9001, ’SAVINGS’…..) Compression does not occurs.

CREATE TABLE bank_account_data(customer_id INTEGER,account_type CHAR(10)

:COMPRESS 'SAVINGS’ UPPERCASE

CREATE TABLE bank_account_data(customer_id INTEGER,account_type CHAR(10)

:

Example 2

INSERT INTO bank_account_date VALUES (9001, ’Savings’…..)

COMPRESS 'SAVINGS’ UPPERCASE

INSERT INTO bank_account_date VALUES (9001, ’Savings’…..) Compression occurs.

Compression occurs.INSERT INTO bank_account_date VALUES (9001, ’SAVINGS’…..)INSERT INTO bank_account_date VALUES (9001, ’SAVINGS’…..)

Adding Table Constraints and Attributes Page 11-15

Page 398: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Compression Limitations Limitations

• A maximum of 255 values may be compressed per column. • The maximum size of a compressed value is 255 bytes. • Only columns with a fixed physical length may be compressed - e.g. CHAR but not

VARCHAR. • Primary index columns cannot be compressed. • ALTER TABLE may be used to add a compressed column to a table, but it cannot be

used to compress an existing column with existing data , thus either a new column or a new table must be created, populated and renamed.

• The aggregate of all compressed values may not exceed the maximum size of a table header (64K).

What Is Not Compressible The following data types are not compressible:

• INTERVAL • TIME • TIMESTAMP • VARCHAR • VARBYTE • VARGRAPHIC

Adding Table Constraints and Attributes Page 11-16

Page 399: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Compression Limitations

INTERVAL TIME TIMESTAMP VARCHAR VARBYTE VARGRAPHICBLOB CLOB

Limitations:• A maximum of 255 values may be compressed per column. • The maximum size of a compressed value is 255 bytes. • Only columns with a fixed physical length may be compressed - e.g. CHAR

but not VARCHAR. • ALTER TABLE may be used to add a compressed column to a table, but it

cannot be used to compress an existing column with existing data.• The aggregate of all compressed values may not exceed the maximum size

of a table header (64K).

What is Not Compressible:• Component of the primary index• Volatile table columns• Derived table columns• Referenced primary key columns• Referencing foreign key columns

The following data types are not compressible:

Adding Table Constraints and Attributes Page 11-17

Page 400: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Primary Key vs. Primary Index Primary Key A primary key is defined as one or more columns used to uniquely identify the rows in a table. Primary Keys are:

• Used in conjunction with foreign keys to define the important column relationships in a database.

• Always unique and cannot be null. • Not known to the Teradata RDBMS as keys. Teradata RDBMS implements a primary

key as an index. Primary Index A primary index is defined as one or more columns used to distribute and locate the rows in a table. Choice of primary index will affect distribution, access, and thus performance. Indexes (primary or secondary) may be used to enforce uniqueness, improve access, and may be unique or non-unique. All uniqueness and primary key constraints are implemented physically as unique indexes.

Adding Table Constraints and Attributes Page 11-18

Page 401: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Primary Key vs. Primary Index

One or more columns used to uniquely identify the rows in a tableUsed in conjunction with foreign keys to define dependent columnrelationships in a databaseAlways unique and cannot be nullNot known to the Teradata RDBMS as keys

One or more columns used to distribute and locate the rows in a table.Choice of primary index will affect distribution, access and thus performance.Indexes (primary or secondary) may be used to enforce uniqueness.Indexes (primary or secondary) may be used to improve access.Indexes (primary or secondary) may be unique or non-unique.

Primary Index

Primary Key

Adding Table Constraints and Attributes Page 11-19

Page 402: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Indexes The primary index may be explicitly specified at table create time. If not, a primary index choice will be made based on other choices made. The secondary index may be created at the time of the table creation or after the fact.

UPI = Unique Primary Index NUPI = Non Unique Primary Index USI = Unique Secondary Index NUSI= Non Unique Secondary

Legen

Adding Table Constraints and Attributes Page 11-20

Page 403: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Indexes

A Primary Index may be unique (UPI) or non-unique (NUPI).It is created when the table is created.It cannot be changed or dropped without recreating the table.If no PI information is provided, it will default to a NUPI on the first column.

A Primary Index may be unique (UPI) or non-unique (NUPI).It is created when the table is created.It cannot be changed or dropped without recreating the table.If no PI information is provided, it will default to a NUPI on the first column.

A Secondary Index may be unique (USI) or non-unique (NUSI).It may be created when the table is created, or afterwards.It can be dropped and recreated at any time.It creates a separate physical structure called the index subtable.

A Secondary Index may be unique (USI) or non-unique (NUSI).It may be created when the table is created, or afterwards.It can be dropped and recreated at any time.It creates a separate physical structure called the index subtable.

Primary Index

Secondary Indexes

CREATE TABLE ……. UNIQUE PRIMARY INDEX (empnum);

CREATE TABLE ……. PRIMARY INDEX (last_name);

CREATE TABLE ……. UNIQUE INDEX (department_name);

CREATE TABLE ……. INDEX (hire_date);

Adding Table Constraints and Attributes Page 11-21

Page 404: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE Example Performing a SHOW TABLE returns the current definition of the table. This definition may not be an exact replica of the originally submitted DDL, due to Teradata specific conversions. Results of SHOW TABLE: CREATE MULTISET TABLE emp_data ,FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, DATABLOCKSIZE = 10240 BYTES, FREESPACE = 30 PERCENT, CHECKSUM = DEFAULT ( employee_number INTEGER NOT NULL, department_number SMALLINT, job_code INTEGER COMPRESS , last_name CHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL, first_name VARCHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC, street_address VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC TITLE 'Address', city CHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT 'Boise ' COMPRESS 'Boise ', state CHAR(2) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT ' ', birthdate DATE FORMAT 'mm/dd/yy', salary_amount DECIMAL(10,2), gender CHAR(1) CHARACTER SET LATIN UPPERCASE NOT CASESPECIFIC, CONSTRAINT dep_check CHECK ( (department_number >= 100 ) AND (department_number <= 999 ) )) UNIQUE PRIMARY INDEX ( employee_number ) INDEX ( department_number );

Space Management Options FREESPACE - % cylinder freespace to maintain during loading operation. DATABLOCKSIZE - maximum block size for multi-row data blocks. (Current maximum is 130560). Defaults are provided for both options if they are not specified.

Adding Table Constraints and Attributes Page 11-22

Page 405: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE Example

CREATE TABLE emp_data,FALLBACK,NO BEFORE JOURNAL,NO AFTER JOURNAL,FREESPACE = 30,DATABLOCKSIZE = 10000 BYTES

(employee_number INTEGER NOT NULL,department_number SMALLINT

CONSTRAINT dep_checkCHECK (department_number BETWEEN 100 AND 999)

,job_code INTEGER COMPRESS,last_name CHAR(20) NOT NULL,first_name VARCHAR(20),street_address VARCHAR(30) TITLE 'Address',city CHAR(15) DEFAULT 'Boise‘ COMPRESS 'Boise',state CHAR(2) WITH DEFAULT,birthdate DATE FORMAT 'mm/dd/yy',salary_amount DECIMAL (10,2),gender CHAR(1) UPPERCASE)UNIQUE PRIMARY INDEX (employee_number)INDEX (department_number);

CREATE TABLE emp_data,FALLBACK,NO BEFORE JOURNAL,NO AFTER JOURNAL,FREESPACE = 30,DATABLOCKSIZE = 10000 BYTES

(employee_number INTEGER NOT NULL,department_number SMALLINT

CONSTRAINT dep_checkCHECK (department_number BETWEEN 100 AND 999)

,job_code INTEGER COMPRESS,last_name CHAR(20) NOT NULL,first_name VARCHAR(20),street_address VARCHAR(30) TITLE 'Address',city CHAR(15) DEFAULT 'Boise‘ COMPRESS 'Boise',state CHAR(2) WITH DEFAULT,birthdate DATE FORMAT 'mm/dd/yy',salary_amount DECIMAL (10,2),gender CHAR(1) UPPERCASE)

30% of cylinder space is reserved during loading.

Datablock size will be rounded up to 10240 bytes (multiple of 512).

An explicit UPI is defined on the employee number.

An explicit NUSI is defined on the department number.

UNIQUE PRIMARY INDEX (employee_number)INDEX (department_number);

Adding Table Constraints and Attributes Page 11-23

Page 406: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Column-level Constraints ANSI standards permit constraints to be placed at the column level. Constraints may be named or unnamed.

• PRIMARY KEY: May only be defined on NOT NULL columns. Guarantees uniqueness.

• UNIQUE: May only be defined on NOT NULL columns. Guarantees uniqueness. • CHECK: Allows range or value constraints to be placed on the column • REFERENCES: Requires values to be referenced checked before being allowed

It is possible to have more than one constraint defined on a single column. Note: Columns with a REFERENCES constraint must refer to a column that has been defined as a unique column with a NOT NULL attribute.

Adding Table Constraints and Attributes Page 11-24

Page 407: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Column-level Constraints

CREATE TABLE employee_badge(emp_id INTEGER NOT NULL

CONSTRAINT primary_1 PRIMARY KEY

UNIQUE

CHECK

REFERENCES

,id_badge_number INTEGER NOT NULLCONSTRAINT unique_1

,salary INTEGERCONSTRAINT check_1 (salary>0)

,job_code INTEGERCONSTRAINT ref_1

job(job_code));

Example:

• All constraints are named.• All constraints are at column level.• PRIMARY KEY columns must be declared NOT NULL.• UNIQUE columns must be declared NOT NULL.• Referenced and referencing columns must be the same data type.

ConstraintTypes:

PRIMARY KEY No Nulls, No DuplicatesUNIQUE No Nulls, No DuplicatesCHECK Verify values or rangeREFERENCES References other column values

(as a foreign key)

Adding Table Constraints and Attributes Page 11-25

Page 408: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Constraints as Indexes The primary index may be explicitly specified at table create time. If not, a primary index choice will be made based on other choices made. Primary key and uniqueness constraints are always implemented by the DBS as unique indexes, either primary or secondary. The index implementation schedule is as follows: PI specified? No PK specified? PK = UPI PK specified and PK = UPI UNIQUE(s) specified? UNIQUE(s) = USI(s) UNIQUE(s) only specified 1st UNIQUE = UPI Other UNIQUE(s) = USI(s) Neither specified? 1st column = NUPI Yes PK specified? PK = USI PK specified and PK = USI UNIQUE(s) specified? UNIQUE(s) = USI(s) UNIQUE(s) only specified UNIQUE(s) = USI(s)

Adding Table Constraints and Attributes Page 11-26

Page 409: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Constraints as Indexes

If PK specified PK = UPI*Else 1st UNIQUE col specified col = UPI*Else 1st col specified col = NUPI.

*(Other UNIQUE cols col = USI)

If PK specified PK = UPI*Else 1st UNIQUE col specified col = UPI*Else 1st col specified col = NUPI.

*(Other UNIQUE cols col = USI)

If PK specified PK = USI.Any UNIQUE col specified col = USI.If PK specified PK = USI.Any UNIQUE col specified col = USI.

PK = Primary KeyPI = Primary IndexUPI = Unique Primary IndexNUPI = Non Unique Primary IndexUSI = Unique Secondary Index

PK = Primary KeyPI = Primary IndexUPI = Unique Primary IndexNUPI = Non Unique Primary IndexUSI = Unique Secondary Index

Unspecified PI Specified PI

Notes:• All PRIMARY KEY and UNIQUE constraints are

implemented as unique indexes.• The above rules also apply to multi-column constraints.• Mixing Teradata and ANSI syntax is not recommended.• Use either CONSTRAINTs or index definitions, not both.

Notes:• All PRIMARY KEY and UNIQUE constraints are

implemented as unique indexes.• The above rules also apply to multi-column constraints.• Mixing Teradata and ANSI syntax is not recommended.• Use either CONSTRAINTs or index definitions, not both.

Adding Table Constraints and Attributes Page 11-27

Page 410: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table-level Constraints The fundamental difference between defining constraints at the table level instead of at the column level is that in a table-level definition, multiple columns from the current table may participate in a single constraint. In a column-level definition, a constraint can only apply to the single specified column. In either case, constraints may be named or unnamed. Table-level constraints always follow the last defined column. Note: Internally, all named column-level constraints are converted to table level constraints or to indexes. This is reflected in the SHOW TABLE output seen here.

SHOW TABLE employee_badge; *** Text of DDL statement returned. *** Total elapsed time was 1 second. --------------------------------------------------------------------------- CREATE SET TABLE employee_badge ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( emp_id INTEGER NOT NULL, badge_num INTEGER NOT NULL, salary INTEGER, job_code INTEGER, CONSTRAINT check_1 CHECK ( (salary > 0 ) AND ((job_code >= 100000 ) AND (job_code <= 499999 )) ), FOREIGN KEY ( job_code ) REFERENCES SQL36.JOB ( job_code )) UNIQUE PRIMARY INDEX primary_1 ( emp_id ) UNIQUE INDEX ( badge_num );

Adding Table Constraints and Attributes Page 11-28

Page 411: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table-level Constraints

CREATE TABLE employee_badge(emp_id INTEGER NOT NULL,badge_num INTEGER NOT NULL,salary INTEGER,job_code INTEGER

,CONSTRAINT primary_1 PRIMARY KEY (eUNIQUECHECK

FOREIGN KEYREFERENCES

mp_id), (badge_num),CONSTRAINT check_1 (salary > 0 AND

job_code BETWEEN 100000 AND 499999), (job_code)

job (job_code));

CREATE TABLE employee_badge(emp_id INTEGER NOT NULL,badge_num INTEGER NOT NULL,salary INTEGER,job_code INTEGER

,CONSTRAINT primary_1 emp_id), (badge_num),CONSTRAINT check_1 (salary > 0 AND

job_code BETWEEN 100000 AND 499999), (job_code)

job (job_code));

How does Teradata implement the PRIMARY KEY constraint?How does Teradata implement the UNIQUE constraint?Answers are on facing page.

PRIMARY KEY (UNIQUECHECK

FOREIGN KEYREFERENCES

How does Teradata implement the PRIMARY KEY constraint?How does Teradata implement the UNIQUE constraint?Answers are on facing page.

• An alternate way to set column constraints• Useful for multi-column constraints• Coded following last column definition• May be named or unnamed

All constraints here are defined at the table level. Some constraints are named, some unnamed.

All constraints here are defined at the table level. Some constraints are named, some unnamed.

Adding Table Constraints and Attributes Page 11-29

Page 412: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE with PK Constraint Performing a SHOW TABLE returns the current definition of the table. This definition is not an exact replica of the originally submitted DDL, due to Teradata specific conversions. Results of SHOW TABLE: CREATE TABLE emp_data ,FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, DATABLOCKSIZE = 10240 BYTES, FREESPACE = 30 PERCENT ( employee_number INTEGER NOT NULL, department_number SMALLINT CHECK ( (department_number >= 100 ) AND (department_number <= 999 ) ), job_code INTEGER COMPRESS , last_name CHAR(20) NOT CASESPECIFIC NOT NULL, first_name VARCHAR(20) NOT CASESPECIFIC, street_address VARCHAR(30) NOT CASESPECIFIC TITLE 'Address', city CHAR(15) NOT CASESPECIFIC DEFAULT 'Boise' COMPRESS 'Boise ', state CHAR(2) NOT CASESPECIFIC DEFAULT ' ', birthdate DATE FORMAT 'mm/dd/yyyy', salary_amount DECIMAL(10,2), sex CHAR(1) UPPERCASE NOT CASESPECIFIC) UNIQUE PRIMARY INDEX emp_key ( employee_number ) INDEX ( department_number );

Adding Table Constraints and Attributes Page 11-30

Page 413: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE with PK Constraint

CREATE TABLE emp_data,FALLBACK,NO BEFORE JOURNAL,NO AFTER JOURNAL,FREESPACE = 30,DATABLOCKSIZE = 10000 BYTES

(employee_number INTEGER NOT NULL,department_number SMALLINT

CHECK (department_number BETWEEN 100 AND 999),job_code INTEGER COMPRESS,last_name CHAR(20) NOT NULL,first_name VARCHAR(20),street_address VARCHAR(30) TITLE 'Address',city CHAR(15) DEFAULT 'Boise‘ COMPRESS 'Boise',state CHAR(2) WITH DEFAULT,birthdate DATE FORMAT 'mm/dd/yy',salary_amount DECIMAL (10,2),sex CHAR(1) UPPERCASE

,CONSTRAINT emp_key PRIMARY KEY (employee_number))INDEX (department_number);

CREATE TABLE emp_data,FALLBACK,NO BEFORE JOURNAL,NO AFTER JOURNAL,FREESPACE = 30,DATABLOCKSIZE = 10000 BYTES

(employee_number INTEGER NOT NULL,department_number SMALLINT

CHECK (department_number BETWEEN 100 AND 999),job_code INTEGER COMPRESS,last_name CHAR(20) NOT NULL,first_name VARCHAR(20),street_address VARCHAR(30) TITLE 'Address',city CHAR(15) DEFAULT 'Boise‘ COMPRESS 'Boise',state CHAR(2) WITH DEFAULT,birthdate DATE FORMAT 'mm/dd/yy',salary_amount DECIMAL (10,2),sex CHAR(1) UPPERCASE

, )INDEX (department_number);

Questions:• How many constraints are

defined? Named vs. unnamed?

• What primary and secondary indexes will be implemented?

Questions:• How many constraints are

defined? Named vs. unnamed?

• What primary and secondary indexes will be implemented?

CONSTRAINT emp_key PRIMARY KEY (employee_number)

Adding Table Constraints and Attributes Page 11-31

Page 414: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE Exercise Perform the exercise on the facing page as directed by your instructor. Indicate which index choices will be made for the two columns C1 and C2 in each instance.

Adding Table Constraints and Attributes Page 11-32

Page 415: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE Exercise

CREATE TABLE abc(C1 INT NOT NULL PRIMARY KEY C1 =,C2 INT); C2 =

CREATE TABLE abc(C1 INT NOT NULL PRIMARY KEY C1 =,C2 INT) C2 =

UNIQUE PRIMARY INDEX (C2);

CREATE TABLE abc(C1 INT NOT NULL UNIQUE C1 =,C2 INT); C2 =

CREATE TABLE abc(C1 INT NOT NULL PRIMARY KEY C1 =,C2 INT NOT NULL UNIQUE); C2 =

CREATE TABLE abc(C1 INT C1 =,C2 INT); C2 =

CREATE TABLE abc(C1 INT NOT NULL C1 =,C2 INT NOT NULL UNIQUE) C2 =

PRIMARY INDEX (C1);

In these examples, each CREATE TABLE statement uses varying syntax choices to define the table indexes. Choose which type of index will be assigned to columns C1 and C2 based on how the table is defined. (UPI, NUPI, USI or None)

Index Assigned

Adding Table Constraints and Attributes Page 11-33

Page 416: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE with FK and PK Constraints Performing a SHOW TABLE returns the current definition of the table. This definition is not an exact replica of the originally submitted DDL, due to Teradata specific conversions. Results of SHOW TABLE: CREATE SET TABLE emp_data ,FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, DATABLOCKSIZE = 10240 BYTES, FREESPACE = 30 PERCENT, CHECKSUM = DEFAULT ( employee_number INTEGER NOT NULL, department_number SMALLINT CHECK ( (department_number >= 100 ) AND (department_number <= 999 ) ), job_code INTEGER COMPRESS , last_name CHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL, first_name VARCHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC, street_address VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC TITLE 'Address', city CHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT 'Boise' COMPRESS 'Boise ', state CHAR(2) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT ' ', birthdate DATE FORMAT 'mm/dd/yy', salary_amount DECIMAL(10,2), gender CHAR(1) CHARACTER SET LATIN UPPERCASE NOT CASESPECIFIC, CONSTRAINT dep_fk FOREIGN KEY ( department_number ) REFERENCES SQL100. DEPARTMENT ( department_number )) UNIQUE PRIMARY INDEX emp_key ( employee_number ) INDEX ( department_number );

Adding Table Constraints and Attributes Page 11-34

Page 417: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE emp_data,FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL,FREESPACE = 30,DATABLOCKSIZE = 10000 BYTES

(employee_number INTEGER NOT NULL,department_number SMALLINT

CHECK (department_number BETWEEN 100 AND 999)CONSTRAINT dep_fkREFERENCES department (department_number)

,job_code INTEGER COMPRESS,last_name CHAR(20) NOT NULL,first_name VARCHAR(20),street_address VARCHAR(30) TITLE 'Address',city CHAR(15) DEFAULT 'Boise'

COMPRESS 'Boise',state CHAR(2) WITH DEFAULT,birthdate DATE FORMAT 'mm/dd/yy',salary_amount DECIMAL (10,2),gender CHAR(1) UPPERCASE

,CONSTRAINT emp_key PRIMARY KEY (employee_number))INDEX (department_number);

CREATE TABLE emp_data,FALLBACK, NO BEFORE JOURNAL, NO AFTER JOURNAL,FREESPACE = 30,DATABLOCKSIZE = 10000 BYTES

(employee_number INTEGER NOT NULL,department_number SMALLINT

CHECK (department_number BETWEEN 100 AND 999)CONSTRAINT dep_fkREFERENCES department (department_number)

,job_code INTEGER COMPRESS,last_name CHAR(20) NOT NULL,first_name VARCHAR(20),street_address VARCHAR(30) TITLE 'Address',city CHAR(15) DEFAULT 'Boise'

COMPRESS 'Boise',state CHAR(2) WITH DEFAULT,birthdate DATE FORMAT 'mm/dd/yy',salary_amount DECIMAL (10,2),gender CHAR(1) UPPERCASE

,CONSTRAINT emp_key PRIMARY KEY (employee_number))INDEX (department_number);

CREATE TABLE with FK and PK Constraints

Questions:• How many

constraints are defined?

• Named vs. unnamed? Column vs. table-level?

• What primary and secondary indexes will be implemented?

Adding Table Constraints and Attributes Page 11-35

Page 418: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

HELP CONSTRAINT The HELP CONSTRAINT statement returns information on named constraints (PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK, or REFERENCES) for a particular table. For information on unnamed constraints, use SHOW TABLE. Constraints defined as PRIMARY KEY or UNIQUE are implemented by the Teradata RDBMS as indexes, thus a SHOW TABLE will display them implemented as indexes. NOTE: HELP CONSTRAINT may sometimes produce a very wide report. In this case, the best way to view the output is to use the .SET FOLDLINE and .SET SIDETITLES features of BTEQ. CREATE SET TABLE emp_data ,FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, DATABLOCKSIZE = 10240 BYTES, FREESPACE = 30 PERCENT, CHECKSUM = DEFAULT ( employee_number INTEGER NOT NULL, department_number SMALLINT CHECK ( (department_number >= 100 ) AND (department_number <= 999 ) ), job_code INTEGER COMPRESS , last_name CHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL, first_name VARCHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC, street_address VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC TITLE 'Address', city CHAR(15) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT 'Boise' COMPRESS 'Boise ', state CHAR(2) CHARACTER SET LATIN NOT CASESPECIFIC DEFAULT ' ', birthdate DATE FORMAT 'mm/dd/yy', salary_amount DECIMAL(10,2), sex CHAR(1) CHARACTER SET LATIN UPPERCASE NOT CASESPECIFIC, CONSTRAINT dep_fk FOREIGN KEY ( department_number ) REFERENCES SQL100.DEPARTMENT ( department_number )) UNIQUE PRIMARY INDEX emp_key ( employee_number ) INDEX ( department_number );

Adding Table Constraints and Attributes Page 11-36

Page 419: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

HELP CONSTRAINT

.SET FOLDLINE OFF; /* Each row occupies one line */

.SET SIDETITLES OFF; /* Titles are above column values */.SET FOLDLINE OFF; /* Each row occupies one line */.SET SIDETITLES OFF; /* Titles are above column values */

Given: the emp_data table defined earlier

Name EMP_KEYType PRIMARY KEYUnique? YIndex Id 1Column Names EMPLOYEE_NUMBERz

Name EMP_KEYType PRIMARY KEYUnique? YIndex Id 1Column Names EMPLOYEE_NUMBERz

HELP CONSTRAINT emp_data.emp_key;HELP CONSTRAINT emp_data.emp_key;

Name DEP_FKType REFERENCEState VALIDIndex Id 0Foreign Key Columns DEPARTMENT_NUMBERParent DB Name SQL100Parent Table Name DEPARTMENTParent Index Id 1Parent Key Columns DEPARTMENT_NUMBER

Name DEP_FKType REFERENCEState VALIDIndex Id 0Foreign Key Columns DEPARTMENT_NUMBERParent DB Name SQL100Parent Table Name DEPARTMENTParent Index Id 1Parent Key Columns DEPARTMENT_NUMBER

HELP CONSTRAINT emp_data.dep_fk;HELP CONSTRAINT emp_data.dep_fk;

.SET FOLDLINE ON; /* Give each column value a new line */

.SET SIDETITLES ON; /* Put titles adjacent to value */.SET FOLDLINE ON; /* Give each column value a new line */ .SET SIDETITLES ON; /* Put titles adjacent to value */

Adding Table Constraints and Attributes Page 11-37

Page 420: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Altering Table Constraints Constraints may be added, modified and dropped using the ALTER TABLE command. Some restrictions apply. CHECK - These constraints may be added to an existing table provided the existing table data does not violate the proposed constraint. Only ‘named CHECK’ constraints may be modified. Changing Table Attributes Both DATABLOCKSIZE and FREESPACE may be changed dynamically with the ALTER TABLE command. When data block size is altered, two scenarios are possible: Deferred alteration: This is the default. Block sizes will be altered as new rows are added and blocks are updated and reformulated. Immediate alteration: This will cause all blocks to be immediately modified to the newly specified blocksize for multi-row blocks. This is requested by specifying the IMMEDIATE option. The operation may be more time intensive.

Adding Table Constraints and Attributes Page 11-38

Page 421: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Altering Table Constraints

ALTER TABLE tablenameADD CONSTRAINT constrname CHECKADD CONSTRAINT constrname UNIQUEADD CONSTRAINT constrname PRIMARY KEYADD CONSTRAINT constrname FOREIGN KEY

ALTER TABLE tablenameADD CONSTRAINT constrname CHECKADD CONSTRAINT constrname UNIQUEADD CONSTRAINT constrname PRIMARY KEYADD CONSTRAINT constrname FOREIGN KEY

To add constraints to a table

ALTER TABLE tablenameDROP CONSTRAINT constrname

ALTER TABLE tablenameDROP CONSTRAINT constrname

ALTER TABLE tablenameMODIFY CONSTRAINT constrname

ALTER TABLE tablenameMODIFY CONSTRAINT constrname

To modify existing CHECK constraints

To drop constraints

• PRIMARY KEY, UNIQUE and REFERENCES constraints may also be added or dropped, with some restrictions.

• Only named CHECK constraints may be modified.

• CHECK constraints cannot be added if existing data violates proposed constraints.

• PRIMARY KEY, UNIQUE and REFERENCES constraints may also be added or dropped, with some restrictions.

• Only named CHECK constraints may be modified.

• CHECK constraints cannot be added if existing data violates proposed constraints.

Adding Table Constraints and Attributes Page 11-39

Page 422: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Parent-Child Relationships The operative rule of parent-child relationships is that a child must have a parent. Any maintenance done to the table must honor the integrity of this relationship as specified in the constraint definition. Child tables are called 'referencing tables' and parent tables are called 'referenced tables'.

Adding Table Constraints and Attributes Page 11-40

Page 423: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Parent-Child RelationshipsParent tables contain PK's (Referenced tables)Child tables contain FK's (Referencing tables)

A Child Must Have a Parent

PK FK

Referenced table (Parent) Referencing table (Child)

Insert row Validate FK against Parent, else errorUpdate FK Validate FK against Parent, else errorDelete row No validation needed

Child Table

Insert row No validation neededUpdate PK Any FK dependencies, get errorDelete row Any FK dependencies, get error

Parent Table

Adding Table Constraints and Attributes Page 11-41

Page 424: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Fixing Referential Integrity Problems Alteraing a table to add foreign key constraints will appear to be a successful operation even if unreferenced values are found. The rows containing the unreferenced values will be written to an error table, however the operation will appear to be successful. It is thus important to check for the existence of the error table following the alter table command. There are multiple ways to 'clean up' these kinds of inconsistencies, as demonstrated on the facing page.

Adding Table Constraints and Attributes Page 11-42

Page 425: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Fixing Referential Integrity ProblemsAssume there is a department manager 1099.

Assume that 1099 is not an entry in the employee table.

DELETE FROM deptWHERE manager_employee_number IN

(SELECT manager_employee_number FROM dept_0);

DELETE FROM deptWHERE manager_employee_number IN

(SELECT manager_employee_number FROM dept_0);

Fix Option 3 - Delete the problem department(s)

UPDATE dept SET manager_employee_number = NULLWHERE manager_employee_number IN(SELECT manager_employee_number FROM dept_0);

UPDATE dept SET manager_employee_number = NULLWHERE manager_employee_number IN(SELECT manager_employee_number FROM dept_0);

Fix Option 2 - Assign a null manager

UPDATE dept SET manager_employee_number = 1018WHERE manager_employee_number = 1099;

UPDATE dept SET manager_employee_number = 1018WHERE manager_employee_number = 1099;

Fix Option 1 - Assign a different manager

• ALTER TABLE is successful• Table dept_0 is created with

the single row for 1099• User is responsible for

fixing referencing table data

• ALTER TABLE is successful• Table dept_0 is created with

the single row for 1099• User is responsible for

fixing referencing table data

ResultsALTER TABLE dept ADD CONSTRAINT mgr_ref

FOREIGN KEY (manager_employee_number)REFERENCES employee (employee_number);

ALTER TABLE dept ADD CONSTRAINT mgr_refFOREIGN KEY (manager_employee_number)REFERENCES employee (employee_number);

Submit

Adding Table Constraints and Attributes Page 11-43

Page 426: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Drop Inconsistent References When a table is determined to have potential consistency problems it is marked as incon-sistent. This marking disallows all maintenance activity against the table. If it is desired to perform maintenance on the table, the table may be marked useable by dropping the inconsistent references flag, however this should be done only in extremely rare circumstances. Once the table is marked useable normal maintenance activity may proceed. It becomes the user's responsibility to insure the validity of the referencing information until such time as the Foreign Key constraint is reinstated. The REFERENCES privilege is required to perform the DROP INCONSISTENT REFERENCES command. It has no effect on existing data in the table. DROP INCONSISTENT REFERENCES is used to delete all inconsistent references defined on the table. It is commonly used after a restore, when an ALTER TABLE DROP FOREIGN KEY might not work. This clause is a Teradata extension to the ANSI SQL-99 standard. While a table is marked as inconsistent, no updates, inserts, or deletes are permitted. The table is fully usable only when the inconsistencies are resolved. This restriction is true for both hard and soft referential integrity. It is possible that the user either intends to or must revert to a definition of a table which results in an inconsistent reference on that table. The Archive and Restore operations are the most common cause of such inconsistencies. After the child table is archived, the parent table can be dropped. When the child table is restored, the parent table no longer exists. The normal ALTER TABLE DROP FOREIGN KEY statement does not work, because the parent table references cannot be resolved. You can use the DROP INCONSISTENT REFERENCES option to remove these inconsistent references from a table. The syntax is: ALTER TABLE database_name.table_name DROP INCONSISTENT REFERENCES You must have DROP privileges on the target table of the statement to perform this option, which removes all inconsistent internal indexes used to establish references.

Adding Table Constraints and Attributes Page 11-44

Page 427: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Drop Inconsistent References

A parent table may not be dropped while there are existing child table dependenciesA parent table may not be dropped while there are existing child table dependencies

Assume you want to update a row of Child table

Warning: Proceed with caution!

ALTER TABLE child_tbl DROP INCONSISTENT REFERENCES;ALTER TABLE child_tbl DROP INCONSISTENT REFERENCES;

FK Constraints are now removed from the table

UPDATE child_tbl SET fk1 = 1005;UPDATE child_tbl SET fk1 = 1005;

Update child row

Make child table accessible

Referential Integrity is user responsibility until a new FK constraint is added to table.Referential Integrity is user responsibility until a new FK constraint is added to table.

Example Archive and drop child tableArchive and drop parent tableRestore child tableWithout a parent, child table is marked as inconsistent(I.e. no adds, updates, deletes or table alterations allowed)

Parent RestoredChild

FK constraints may be reinstated by an ALTER TABLE command

Adding Table Constraints and Attributes Page 11-45

Page 428: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Circular References Circular references occur when two tables are each both parent and child to each other. It is possible to have circular references in tables, however the sequence of operations performed becomes critical, and the referencing columns must allow nulls.

Adding Table Constraints and Attributes Page 11-46

Page 429: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Circular References

PK1 FK1

Table A Table B

FK2 PK2

x xy y

INSERT INTO A (PK1 , FK2) VALUES (x, NULL);INSERT INTO B (PK2 , FK1) VALUES (y, x);UPDATE A SET FK2 = y;

INSERT INTO A (PK1 , FK2) VALUES (x, NULL);INSERT INTO B (PK2 , FK1) VALUES (y, x);UPDATE A SET FK2 = y;

To Insert rows

DELETE A WHERE PK1 = x; (error, referencing column)UPDATE B SET FK1 = NULL;DELETE A WHERE PK1 = x; (OK)

DELETE A WHERE PK1 = x; (error, referencing column)UPDATE B SET FK1 = NULL;DELETE A WHERE PK1 = x; (OK)

To Delete rows

Note: FK1 and FK2 must allow nulls for these operations to workNote: FK1 and FK2 must allow nulls for these operations to work

Adding Table Constraints and Attributes Page 11-47

Page 430: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Macros to Preserve Integrity Macros may be used for either of the following purposes:

• To insure data integrity - that the data conforms to certain range or value restrictions. • To insure referential integrity - that the data being input as foreign keys have legal

primary key values elsewhere in the database. These are legacy approaches to integrity, as newer ANSI standard techniques exist to accomplish the same end using table constraints. This is seen on the next slide of this module.

Adding Table Constraints and Attributes Page 11-48

Page 431: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Macros to Preserve Integrity

• Employee must be over 21 years of age.

• Employee must be assigned a valid department number.

• Employee must be assigned a valid job code.

CREATE MACRO new_employee(emp INTEGER, mgr INTEGER ,dept INTEGER,job INTEGER, lastname CHAR (20),firstname VARCHAR(30),hired DATE , birth DATE ,salary DECIMAL (10, 2))AS (ROLLBACK WORK 'Invalid Hire'

WHERE (:hired - :birth) / 365 < 21;ROLLBACK WORK 'Invalid Department'

WHERE :dept NOT IN (SELECT department_numberFROM departmentWHERE department_number = :dept);

ROLLBACK WORK 'Invalid Job Code' WHERE :job NOT IN (SELECT job_code

FROM jobWHERE job_code = :job);

INSERT INTO employee (employee_number,manager_employee_number,department_number,job_code,last_name,first_name,hire_date,birthdate,salary_amount )

VALUES ( :emp, :mgr, :dept, :job, :lastname, :firstname, :hired, :birth,:salary );) ;

CREATE MACRO new_employee(emp INTEGER, mgr INTEGER ,dept INTEGER,job INTEGER, lastname CHAR (20),firstname VARCHAR(30),hired DATE , birth DATE ,salary DECIMAL (10, 2))AS (ROLLBACK WORK 'Invalid Hire'

WHERE (:hired - :birth) / 365 < 21;ROLLBACK WORK 'Invalid Department'

WHERE :dept NOT IN (SELECT department_numberFROM departmentWHERE department_number = :dept);

ROLLBACK WORK 'Invalid Job Code' WHERE :job NOT IN (SELECT job_code

FROM jobWHERE job_code = :job);

INSERT INTO employee (employee_number,manager_employee_number,department_number,job_code,last_name,first_name,hire_date,birthdate,salary_amount )

VALUES ( :emp, :mgr, :dept, :job, :lastname, :firstname, :hired, :birth,:salary );) ;

Create a macro which inserts a new employee row with the following restrictions:

Adding Table Constraints and Attributes Page 11-49

Page 432: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Constraints to Preserve Integrity Table constraints may be put in place as part of the table definition. Data and referential integrity are controlled at the Data Dictionary level, as part of the table definition. No row may be inserted into this table without these constraint conditions being applied. This contrasts with the use a macro for integrity enforcement seen on the previous slide, wherein integrity is controlled only when data is inserted via the macro. Insertions done without the macro will not have the constraints applied. NOTE: In this definition of the Employee table, the department number column is defined as a SMALLINT instead of INTEGER as was the case previously. This is due to the fact that a REFERENCE constraint is created referencing the primary key department_number in the Department table where it is defined as a SMALLINT. Referenced and referencing columns must always have the same data type.

Adding Table Constraints and Attributes Page 11-50

Page 433: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Constraints to Preserve Integrity

CREATE TABLE employee(employee_number INTEGER,manager_employee_number INTEGER,department_number SMALLINT,job_code INTEGER,last_name CHAR(20),first_name VARCHAR(30),birthdate DATE,hire_date DATE,salary_amount DECIMAL (10,2),CHECK ((hire_date - birthdate) /365 > = 21),FOREIGN KEY (department_number)

REFERENCES department (department_number),FOREIGN KEY (job_code)

REFERENCES job (job_code))UNIQUE PRIMARY INDEX (employee_number);

CREATE TABLE employee(employee_number INTEGER,manager_employee_number INTEGER,department_number SMALLINT,job_code INTEGER,last_name CHAR(20),first_name VARCHAR(30),birthdate DATE,hire_date DATE,salary_amount DECIMAL (10,2),CHECK ((hire_date - birthdate) /365 > = 21),FOREIGN KEY (department_number)

REFERENCES department (department_number),FOREIGN KEY (job_code)

REFERENCES job (job_code))UNIQUE PRIMARY INDEX (employee_number);

Create an employee table with constraints to insure that each employee:

• Is at least 21 years old when hired.• Has a valid department number.• Has a valid job code.

Adding Table Constraints and Attributes Page 11-51

Page 434: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise The facing page contains review questions.

Adding Table Constraints and Attributes Page 11-52

Page 435: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise

1. The COMPRESS attribute will permit compression of both ________ and _________ .

2. The two benefits associated with column compression are _____________ costs and system __________.

3. How many values can be compressed for a single column? ________

4. Constraints involving multiple columns must be defined at the _______ _______.

5. True or False: In order to issue a HELP CONSTRAINT command, a constraint must be named. ________

6. True or False: When defining a REFERENCES constraint at the table level, the FOREIGN KEY keywords must be used. _________

7. True or False: All PRIMARY KEY and FOREIGN KEY constraints are implemented as indexes. __________

8. True or False: A UNIQUE constraint can be implemented as a UPI or a USI.________

9. True or False: A FOREIGN KEY constraint must reference a column or columns that are defined using a PRIMARY KEY or a UNIQUE constraint _________.

10. True or False: When an ALTER TABLE command is used to add a referencing constraint to a table, the command is successful only if there are no inconsistencies in the existing table data. _________

Adding Table Constraints and Attributes Page 11-53

Page 436: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2) The facing page describes the tasks for your lab exercise.

Adding Table Constraints and Attributes Page 11-54

Page 437: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)Note: For the following labs, you will be creating and modifying your own tables. It will be useful

to set your default database to your own username and to qualify external references to other databases. (DATABASE userid; where userid is your assigned student id.)

1.) CREATE an employee table in your own database identical to the employee table in the Customer_Service database. Use the 'SHOW TABLE Customer_Service .employee' command to assist you in creating the new table in your own database. Call the new table emp_new. Populate the table using INSERT SELECT using rows from the Customer_Service.employee table.

2a.) Create a department table identical to the department table in the Customer_Service database. Use the same approach as in Lab 1, however, assign a primary key constraint on the department_number column instead of specifying an index. Call the primary key constraint 'primary_1' and call the new table 'dept_new'. Also, remove the index specified for department_name. Do a SHOW TABLE on 'dept_new' to see how the constraint is implemented.

2b.) Alter dept_new to add a uniqueness constraint on the department_name column. Name the constraint 'unique_nm'. Do a SHOW TABLE to see how it is implemented.

2c.) Alter dept_new to add a check constraint to insure that the department_number is greater than 99. Name the constraint 'hundred_plus'. Do a SHOW TABLE to see how it is implemented.

2d.) Alter dept_new to add a references constraint on manager_employee_number. Name the constraint 'mgr_ref' and make it reference the employee_number column of your emp_new table. Do a SHOW TABLE to see how it is implemented.

3.) Do a HELP CONSTRAINT on each of the named constraints from Lab 2.

Adding Table Constraints and Attributes Page 11-55

Page 438: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2) The facing page describes the tasks for your lab exercise.

Adding Table Constraints and Attributes Page 11-56

Page 439: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

4.) Use INSERT SELECT to populate the dept_new table with rows from the department table in Customer_Service. Does it succeed or fail? Why?

5.) Add a WHERE clause to the INSERT SELECT in the preceding lab which eliminates the row causing the failure.

6.) Attempt to add the following rows to the dept_new table.

Dept # dept name budget manager99 ‘new dept’ 900000 1021400 ‘education’ 900000 1021400 ‘new dept’ 900000 1099400 ‘new dept’ 900000 1021

Which succeed, which fail, and why?

7.) Drop any tables you have created during this lab exercise.

Adding Table Constraints and Attributes Page 11-57

Page 440: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (Optional) The facing page describes additional lab exercises.

Adding Table Constraints and Attributes Page 11-58

Page 441: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (Optional)

9a.) Alter your employee_phone table to compress the values 'office' and 'residence' in the comment column. What steps are necessary to make this change? After the modification is complete, do a SHOW TABLE to validate the table definition.

9b.) Resubmit the query to select all rows which have 'office' in the comment column and where the extension is not null. Compare the results to Lab 4 and verify that the table contents have not changed.

10.) Drop any tables you have created during this lab exercise.

Optional Labs

8.) Do a SHOW TABLE on the 'employee_phone' table in the Customer_Service database. Modify the source code by adding null compression on the 'comment' column. Submit the modified source to create this table in your own database.

Populate the table with the data from the source table. Select all rows from the table which have 'office' in the comment column and where the extension is not null.

Adding Table Constraints and Attributes Page 11-59

Page 442: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Adding Table Constraints and Attributes Page 11-60

Page 443: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 12

Module 12Volatile and Global Temporary Tables

After completing this module, you should be able to:

• Create and populate both types of temporary tables.

• Decide which type of temporary table is appropriate for a given need.

• Use temporary tables to enhance application performance.

Volatile and Global Temporary Tables Page 12-1

Page 444: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Volatile and Global Temporary Tables Page 12-2

Page 445: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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 ...........................................................................................................................................12-50

Volatile and Global Temporary Tables Page 12-3

Page 446: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Why Interim Tables? Interim tables have been a staple of SQL programming for a long time. They are extremely useful for solving problems which require “temporary”’ results or which require multiple steps. They are primarily useful in denormalizing tables to make query times faster than would otherwise be the case, particularly when multiple table joins are needed. In the facing-page example, an interim table is built to summarize all transactions by account number. This will make for easy reporting or comparisons of aggregated amounts.

Volatile and Global Temporary Tables Page 12-4

Page 447: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Why Interim Tables?

To allow SQL to perform operations that:• Might not be possible against a normalized table, or• May require multiple SQL statements

For denormalizations such as:• Summary tables• Repeating groups

For intermediate results which will be needed:• Frequently, or• On an on-going basis

CREATE TABLE Daily_Net_Trans(Account_Number INTEGER,Total_Trans_Amount DECIMAL(14,2))

UNIQUE PRIMARY INDEX (Account_Number);

CREATE TABLE Daily_Net_Trans(Account_Number INTEGER,Total_Trans_Amount DECIMAL(14,2))

UNIQUE PRIMARY INDEX (Account_Number);

INSERT INTO Daily_Net_TransSELECT Account_Number

,SUM(Trans_Amount)FROM TransGROUP BY Account_Number;

INSERT INTO Daily_Net_TransSELECT Account_Number

,SUM(Trans_Amount)FROM TransGROUP BY Account_Number;

Create an interim table: Populate it:

Volatile and Global Temporary Tables Page 12-5

Page 448: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Accessing an Interim Table As can be seen from the facing-page example, interim tables have many advantages. They greatly simplify the SQL code needed to produce aggregated results. Because aggregations are done, query time is much faster. In this specific case we have the possible advantage of doing unique primary index accesses against the account number, meaning fewer full table scans. Interim tables have disadvantages as well. They require extra coding to create, populate and drop since they are considered normal, permanent tables by the file system which require perm space. Each time a table is created or dropped, many accesses must be made to the Data Dictionary to account for the new table and all access rights to it.

Volatile and Global Temporary Tables Page 12-6

Page 449: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Accessing an Interim Table

Acct_Num Total_Trans_Amt20035223 -3280.0020024048 -470.0020036699 -870.0020045853 -6910.00

SELECT *FROM Daily_Net_TransWHERE Account_Number IN

(20035223, 20024048, 20036699, 20045853);

SELECT *FROM Daily_Net_TransWHERE Account_Number IN

(20035223, 20024048, 20036699, 20045853);

• Simpler SQL• Doesn’t have to do aggregation• May access Accounts based on Primary Index value• Fast Response

• Simpler SQL• Doesn’t have to do aggregation• May access Accounts based on Primary Index value• Fast Response

Good news about interim tables

• Separate steps to create and populate table• Requires extra perm space for temp table• Table must be dropped when no longer needed• Data Dictionary access necessary for creation and dropping

• Separate steps to create and populate table• Requires extra perm space for temp table• Table must be dropped when no longer needed• Data Dictionary access necessary for creation and dropping

Bad news about interim tables

Volatile and Global Temporary Tables Page 12-7

Page 450: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Temp Table Choices In addition to creating short-term tables in permanent space, there are three other ways to create tables whose duration is limited:

• Derived tables are always local to a specific query, as they are built with code within the query. The rows of the derived table are stored in spool and discarded when the query finishes. The data dictionary has no knowledge of derived tables.

• Volatile Temporary tables are local to a session rather than a specific query, which

means that the table may be used repeatedly within a session. Once the session ends, the volatile table is automatically discarded if it has not already been manually discarded. The Data Dictionary has no knowledge of volatile tables.

• Global Temporary tables are local to a session just as are volatile tables. Unlike

volatile tables, global temporary tables are known by the data dictionary where a permanent definition is kept. Global temporary tables are materialized within a session, and then discarded when the session ends.

In the following module, we will be looking at the three types of temporary tables, how their implementations differ and when to use each.

Volatile and Global Temporary Tables Page 12-8

Page 451: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Temp Table Choices

Derived Tables• Local to the query• Incorporated into SQL query syntax• Spool rows discarded when query finishes• No Data Dictionary involvement

Volatile Temporary Tables• Local to a session• Uses CREATE VOLATILE TABLE syntax• Discarded automatically at session end• No Data Dictionary involvement

Global Temporary Tables• Local to a session• Uses CREATE GLOBAL TEMPORARY TABLE syntax• Materialized instance of table discarded at session end• Creates and keeps table definition in Data Dictionary

Derived Tables• Local to the query• Incorporated into SQL query syntax• Spool rows discarded when query finishes• No Data Dictionary involvement

Volatile Temporary Tables• Local to a session• Uses CREATE VOLATILE TABLE syntax• Discarded automatically at session end• No Data Dictionary involvement

Global Temporary Tables• Local to a session• Uses CREATE GLOBAL TEMPORARY TABLE syntax• Materialized instance of table discarded at session end• Creates and keeps table definition in Data Dictionary

Volatile and Global Temporary Tables Page 12-9

Page 452: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Derived Tables Revisited The creation of the derived table is local to the query. A query may have multiple derived tables. These tables may be joined or manipulated much as any other table would be. We learned in a previous module that the OLAP functions of SQL do not support standard aggregation functions. This fact makes the OLAP functions excellent candidates for the use with derived tables, in particular when the requirement is to perform a statistical function on an aggregation. We see in the facing-page example that to find the top three selling items across all stores, we must first aggregate the sales by product-id using a derived table. Once we have this aggregation done in spool, we may apply the RANK function to answer the question. Derived tables are useful, but only exist for the duration of the query. They are not a practical solution if the result is to be used in many follow-on queries. In this case, other types of temporary tables will be more appropriate.

Volatile and Global Temporary Tables Page 12-10

Page 453: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Derived Tables Revisited

SELECT prodid,sumsales, RANK( ) OVER (ORDER BY sumsales DESC)

AS "Ranking"FROM (SELECT prodid, SUM(sales) FROM salestbl

GROUP BY 1) AS temp(prodid, sumsales)QUALIFY Ranking <= 3;

SELECT prodid,sumsales, RANK( ) OVER (ORDER BY sumsales DESC)

AS "Ranking"FROM (SELECT prodid, SUM(sales) FROM salestbl

GROUP BY 1) AS temp(prodid, sumsales)QUALIFY Ranking <= 3;

Get top three selling items across all stores:

prodid Sumsales Ranking A 170000.00 1 F 150000.00 2 C 115000.00 3

prodid Sumsales Ranking A 170000.00 1 F 150000.00 2 C 115000.00 3

• The table is required for this query but no others.• The query will be run only one time with this data.

• The table is required for this query but no others.• The query will be run only one time with this data.

Derived tables are a good choice if:

Example from a previous moduleExample from a previous module

Derived table name is “temp”.Derived column names are “prodid” and “sumsales”.Table is created in spool using the inner SELECT.SELECT statement is always in parenthesis following FROM.

Derived table name is “temp”.Derived column names are “prodid” and “sumsales”.Table is created in spool using the inner SELECT.SELECT statement is always in parenthesis following FROM.

Volatile and Global Temporary Tables Page 12-11

Page 454: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Volatile Temporary Tables Volatile tables have much in common with derived tables. They are materialized in spool and are unknown to the data dictionary. Unlike derived tables, volatile tables may be used repeatedly throughout a session. They may be dropped at any time manually or automatically at the session end. Volatile tables require their own CREATE syntax. The table definition is kept in cache and not permanently written to disk. Volatile tables do not survive a system restart. The LOG option indicates the desire for standard transaction logging of “before images” into the transient journal. The ON COMMIT DELETE ROWS option specifies that at the end of a transaction, the table rows should be deleted. While this might seem a bit unusual, it is the default required by the ANSI standard. It may be appropriate in situations where a table is materialized only to produce an aggregation and the table rows are not needed beyond that purpose. (Typically this would occur in a multi-statement transaction.) The ON COMMIT PRESERVE ROWS option provides the more normal situation where the table rows are kept following the end of the transaction.

Volatile and Global Temporary Tables Page 12-12

Page 455: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Volatile Temporary Tables

• Materialized in spool• No Data Dictionary access or

transaction locks• Table definition kept in cache• Designed for optimal performance

• Materialized in spool• No Data Dictionary access or

transaction locks• Table definition kept in cache• Designed for optimal performance

Similar to derived tables:

• Is local to the session, not the query • Can be used with multiple queries in

the session• Dropped manually anytime or

automatically at session end• Requires CREATE VOLATILE

TABLE statement

• Is local to the session, not the query • Can be used with multiple queries in

the session• Dropped manually anytime or

automatically at session end• Requires CREATE VOLATILE

TABLE statement

Different from derived tables:

CREATE VOLATILE TABLE vt_deptsal, LOG(deptno SMALLINT,avgsal DEC(9,2),maxsal DEC(9,2),minsal DEC(9,2),sumsal DEC(9,2),empcnt SMALLINT)

ON COMMIT PRESERVE ROWS;

CREATE VOLATILE TABLE vt_deptsal, LOG(deptno SMALLINT,avgsal DEC(9,2),maxsal DEC(9,2),minsal DEC(9,2),sumsal DEC(9,2),empcnt SMALLINT)

ON COMMIT PRESERVE ROWS;

• LOG indicates that a transaction journal is maintained.

• NO LOG allows for better performance.

• PRESERVE ROWS indicates keep table rows at TXN end.

• DELETE ROWS indicates delete all table rows at TXN end.

• Volatile tables do not survive a system restart.

• LOG indicates that a transaction journal is maintained.

• NO LOG allows for better performance.

• PRESERVE ROWS indicates keep table rows at TXN end.

• DELETE ROWS indicates delete all table rows at TXN end.

• Volatile tables do not survive a system restart.

Example:CREATE VOLATILE TABLE Considerations:

Volatile and Global Temporary Tables Page 12-13

Page 456: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Volatile Table Restrictions Volatile tables must have names that are unique within the user’s working database. Even though volatile tables are not known to the data dictionary, if names duplicating dictionary names were allowed, the system would not understand where to locate the requested named object because it could be found in two places. Up to 1000 volatile tables are allowed on a single session. They must all have unique names. They also must be qualified by the userid of the session, either explicitly or implicitly. A volatile table cannot belong to a database or a user; it can only belong to a user’s session. While FALLBACK is an electable option, its value is limited for volatile tables. Because they cannot survive a system restart, making a table FALLBACK will not keep a table available following a restart. The only reason to make a volatile table FALLBACK would be to allow creation of the table in the event of a down AMP. None of the following options are permitted with volatile tables:

• Permanent Journaling • Referential Integrity • CHECK constraints • Column compression • Column default values • Column titles • Named indexes

Volatile and Global Temporary Tables Page 12-14

Page 457: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Volatile Table Restrictions

• Up to 1000 volatile tables are allowed on a single session.• Each must have a unique name.• Volatile tables are always qualified by the session’s userid.

• Up to 1000 volatile tables are allowed on a single session.• Each must have a unique name.• Volatile tables are always qualified by the session’s userid.

• Each session can use the same VT name (local to session).

• VT name cannot duplicate existing object name for this user

Perm or Temp table namesView namesMacro namesTrigger names, etc.

• Each session can use the same VT name (local to session).

• VT name cannot duplicate existing object name for this user

Perm or Temp table namesView namesMacro namesTrigger names, etc.

Multiple Sessions:

• Electable but not often useful for VTs.• VTs don’t survive a system reset.• Electable but not often useful for VTs.• VTs don’t survive a system reset.

FALLBACK:

• Permanent Journaling• Referential Integrity• CHECK constraints• Column compression• Column default values• Column titles• Named indexes

• Permanent Journaling• Referential Integrity• CHECK constraints• Column compression• Column default values• Column titles• Named indexes

Options not permitted:

Restrictions:

Examples:

CREATE VOLATILE TABLE username.table1 CREATE VOLATILE TABLE table1 CREATE VOLATILE TABLE databasename.table1

CREATE VOLATILE TABLE username.table1 CREATE VOLATILE TABLE table1 CREATE VOLATILE TABLE databasename.table1

(Explicit)(Implicit)(Error)

Volatile and Global Temporary Tables Page 12-15

Page 458: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Volatile Tables (1 of 2) Volatile tables are useful for doing aggregations and other denormalizations of permanent tables. In the facing-page example, salary information as well as employee counts are aggregated for each department and stored in the volatile table. The table allows for easy comparisons against the aggregates, as in this example where we are trying to find the individuals whose salary is the minimum in each department. The following view is used in the query on the facing page:

CREATE VIEW emp (emp ,mgr ,dept ,job ,last ,first ,hire ,birth ,sal) AS SELECT employee_number ,manager_employee_number ,department_number ,job_code ,last_name ,first_name ,hire_date ,birthdate ,salary_amount FROM CUSTOMER_SERVICE.employee;

Volatile and Global Temporary Tables Page 12-16

Page 459: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Volatile Tables (1 of 2)

INSERT INTO vt_deptsal SELECT dept ,AVG(sal) ,MAX(sal) ,MIN(sal)

,SUM(sal) ,COUNT(emp)FROM emp GROUP BY 1;

INSERT INTO vt_deptsal SELECT dept ,AVG(sal) ,MAX(sal) ,MIN(sal)

,SUM(sal) ,COUNT(emp)FROM emp GROUP BY 1;

Show all employees who make the minimum salary in their department:

SELECT emp, last, dept, salFROM emp INNER JOIN vt_deptsalON dept = deptnoWHERE sal = minsalORDER BY 3;

SELECT emp, last, dept, salFROM emp INNER JOIN vt_deptsalON dept = deptnoWHERE sal = minsalORDER BY 3;

emp last dept sal 801 Trainer 100 100000.00

1025 Short 201 34700.00 1008 Kanieski 301 29250.00 1016 Rogers 302 56500.00 1013 Phillips 401 24500.00 1014 Crane 402 24500.00 1009 Lombardo 403 31000.00 1023 Rabbit 501 26500.00

emp last dept sal 801 Trainer 100 100000.00

1025 Short 201 34700.00 1008 Kanieski 301 29250.00 1016 Rogers 302 56500.00 1013 Phillips 401 24500.00 1014 Crane 402 24500.00 1009 Lombardo 403 31000.00 1023 Rabbit 501 26500.00

CREATE VOLATILE TABLE vt_deptsal, LOG(deptno SMALLINT,avgsal DEC(9,2),maxsal DEC(9,2),minsal DEC(9,2),sumsal DEC(9,2),empcnt SMALLINT)

ON COMMIT PRESERVE ROWS;

CREATE VOLATILE TABLE vt_deptsal, LOG(deptno SMALLINT,avgsal DEC(9,2),maxsal DEC(9,2),minsal DEC(9,2),sumsal DEC(9,2),empcnt SMALLINT)

ON COMMIT PRESERVE ROWS;

Create a volatile table.

Populate the volatile table with computed aggregates.

Volatile and Global Temporary Tables Page 12-17

Page 460: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Volatile Tables (2 of 2) Because the same temporary table used in the previous example enables us to see which employees have a salary greater than their departmental average, it is also useful for this query. This fact makes it a good candidate for a volatile table. If a derived table were used, the amounts would have to be aggregated a second time for this query.

Volatile and Global Temporary Tables Page 12-18

Page 461: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Volatile Tables (2 of 2)

Show all employees whose salary is more than their department average:

SELECT emp, dept, sal, avgsalFROM emp INNER JOIN vt_deptsalON dept = deptnoWHERE sal > avgsalORDER BY 2;

SELECT emp, dept, sal, avgsalFROM emp INNER JOIN vt_deptsalON dept = deptnoWHERE sal > avgsalORDER BY 2;

emp dept sal avgsal 1021 201 38750.00 36725.00 1019 301 57700.00 38800.00 1010 401 46000.00 35082.14 1004 401 36300.00 35082.14 1002 401 43100.00 35082.14 1003 401 37850.00 35082.14 1011 402 52500.00 38500.00 1007 403 49700.00 38833.33 1020 403 39500.00 38833.33 1024 403 43700.00 38833.33 1015 501 53625.00 50031.25 1017 501 66000.00 50031.25 1018 501 54000.00 50031.25

emp dept sal avgsal 1021 201 38750.00 36725.00 1019 301 57700.00 38800.00 1010 401 46000.00 35082.14 1004 401 36300.00 35082.14 1002 401 43100.00 35082.14 1003 401 37850.00 35082.14 1011 402 52500.00 38500.00 1007 403 49700.00 38833.33 1020 403 39500.00 38833.33 1024 403 43700.00 38833.33 1015 501 53625.00 50031.25 1017 501 66000.00 50031.25 1018 501 54000.00 50031.25

All aggregates are already computed and available.VT is a small, easily scanned table.At session end, table will be dropped.

Volatile and Global Temporary Tables Page 12-19

Page 462: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Getting Help on Volatile Tables Help is easily available for volatile tables using the HELP VOLATILE command. The information returned is as follows: HELP VOLATILE TABLE; Session Id 3685 (Internal session id) Table Name vt_deptsal (External table name) Table Id 10C001000000 (Internal table id) Protection N (Y or N for FALLBACK) Creator Name PED (Userid of creator) Commit Option P (P or D for PRESERVE or DELETE rows) Transaction Log Y (Y or N for LOG option) The conventional SHOW TABLE command also may be used to see the definition of a volatile table. The HELP VOLATILE TABLE command will return all volatile tables defined for this session. Note that HELP DATABASE, which shows all database objects, does not show volatile tables.

Volatile and Global Temporary Tables Page 12-20

Page 463: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Getting Help on Volatile Tables

Session Id 3685 Table Name vt_deptsalTable Id 10C001000000 Protection NCreator Name PED Commit Option P Transaction Log Y

Session Id 3685 Table Name vt_deptsalTable Id 10C001000000 Protection NCreator Name PED Commit Option P Transaction Log Y

SHOW TABLE vt_deptsal;SHOW TABLE vt_deptsal;

Note: HELP DATABASE command does not show VT’s.Note: HELP DATABASE command does not show VT’s.

.SET FOLDLINE ON

.SET SIDETITLES ON

HELP VOLATILE TABLE;

.SET FOLDLINE ON

.SET SIDETITLES ON

HELP VOLATILE TABLE;

CREATE SET VOLATILE TABLE PED1.vt_deptsal,NO FALLBACK,LOG(deptno SMALLINT,avgsal DECIMAL(9,2),maxsal DECIMAL(9,2),minsal DECIMAL(9,2),sumsal DECIMAL(9,2),empcnt SMALLINT)

PRIMARY INDEX ( deptno ) ON COMMIT PRESERVE ROWS;

CREATE SET VOLATILE TABLE PED1.vt_deptsal,NO FALLBACK,LOG(deptno SMALLINT,avgsal DECIMAL(9,2),maxsal DECIMAL(9,2),minsal DECIMAL(9,2),sumsal DECIMAL(9,2),empcnt SMALLINT)

PRIMARY INDEX ( deptno ) ON COMMIT PRESERVE ROWS;

(Returns info on all VT’s in the session)

Volatile and Global Temporary Tables Page 12-21

Page 464: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Limitations on Volatile Tables DDL commands involving dictionary access are not applicable to volatile tables. Dropping or deleting a database or user does not remove volatile tables. Volatile tables also may not use Access Logging, may not be renamed and may not be loaded with the utilities Multiload or Fastload. Volatile tables may be referenced in both views and macros. If two users have logged onto two separate sessions, they may create identical volatile tables which even share the same name. Each instance of the table is local to its own session. If each user selected the contents of the named volatile table, each would see the results of their own volatile table. Volatile tables may be manually dropped by using a conventional DROP TABLE command.

Volatile and Global Temporary Tables Page 12-22

Page 465: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Limitations on Volatile Tables

• COLLECT/DROP/HELP STATISTICS• CREATE/DROP INDEX• ALTER TABLE• GRANT/REVOKE privileges• DELETE DATABASE/USER (does not drop VT’s)

• COLLECT/DROP/HELP STATISTICS• CREATE/DROP INDEX• ALTER TABLE• GRANT/REVOKE privileges• DELETE DATABASE/USER (does not drop VT’s)

The following commands are not applicable to VT’s:

• Use ACCESS LOGGING.• Be RENAMEd.• Be loaded with Multiload

or Fastload utilities.

• Use ACCESS LOGGING.• Be RENAMEd.• Be loaded with Multiload

or Fastload utilities.

VT’s may not:

CREATE MACRO vt1 AS (SELECT * FROM vt_deptsal;);CREATE MACRO vt1 AS (SELECT * FROM vt_deptsal;);Session AEXEC vt1

Session BEXEC vt1

Each session has its own materialized instance of vt_deptsal.Each session may return different results.Each session has its own materialized instance of vt_deptsal.Each session may return different results.

DROP TABLE vt_deptsal;DROP TABLE vt_deptsal;

Example

• Be accessed via views.• Be used in macros.• Be manually dropped

before the session end.

• Be accessed via views.• Be used in macros.• Be manually dropped

before the session end.

VT’s may:

Volatile and Global Temporary Tables Page 12-23

Page 466: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Volatile Table Quiz

Volatile and Global Temporary Tables Page 12-24

Page 467: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Volatile Table Quiz

User A

Session 1 Session 2 Success/Failure/Reason

CVT T1 CVT T1 _________________________

CPT T2 _________________________

CVT T2 _________________________

CVT T3 _________________________

CPT T3 _________________________

INSERT T3 _________________________

CVT T3 _________________________

INSERT T3 ________________________

DROP T3 _________________________

SELECT * FROM T3 _________________________

In the following exercise, User A has two parallel sessions. Assuming the given sequence of events, which will succeed, which will fail, and why?

(Note: CVT = Create Volatile Table; CPT = Create Permanent Table.)

Volatile and Global Temporary Tables Page 12-25

Page 468: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Global Temporary Tables Global Temporary Tables, unlike volatile and derived tables, have a definition stored in the Data Dictionary. The table itself is materialized by the first SQL DML statement which accesses the table, typically an INSERT SELECT or an INSERT. Like volatile tables, global temporary tables are local to a session. The materialized instance of the table is not shareable with other sessions. The table instance may be dropped explicitly or it will be automatically dropped at the end of the session. The definition remains in the dictionary for future materialized instances of the table. The base definition may be dropped with an explicit DROP command. The only privilege required by the user is the DML privilege necessary to materialize the table. Once materialized, no privileges are checked. A special type of space called “temporary space” is used for global temporary tables. Like perm space, temporary space is sustained during a system restart. Global temporary tables are thus able to survive a system restart. Up to 32 materialized instances of a global temporary table may exist for a given session. Note: Henceforth, volatile temporary tables will be referred to simply as “volatile tables”. Also, global temporary tables will be referred to simply as “temporary tables”.

Volatile and Global Temporary Tables Page 12-26

Page 469: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Global Temporary Tables

Are created using CREATE GLOBAL TEMPORARY command.Require a base definition which is stored in the DD.Are materialized by first SQL DML statement to access table.

Are created using CREATE GLOBAL TEMPORARY command.Require a base definition which is stored in the DD.Are materialized by first SQL DML statement to access table.

Global Temporary Tables:

Base definition is permanent and kept in DD.Requires DML privileges necessary to materialize the table.Space is charged against an allocation of “temporary space”.User can materialize up to 32 global tables per session.Tables can survive a system restart.

Base definition is permanent and kept in DD.Requires DML privileges necessary to materialize the table.Space is charged against an allocation of “temporary space”.User can materialize up to 32 global tables per session.Tables can survive a system restart.

Global Temporary Tables are different from Volatile Tables:

Each instance of global temp table is local to a session.Materialized tables are dropped automatically at session end.Have LOG and ON COMMIT PRESERVE/DELETE options.Materialized table contents aren’t sharable with other sessions.

Each instance of global temp table is local to a session.Materialized tables are dropped automatically at session end.Have LOG and ON COMMIT PRESERVE/DELETE options.Materialized table contents aren’t sharable with other sessions.

Global Temporary Tables are similar to Volatile Tables:

Volatile temporary tables are referred to as “Volatile Tables”.Global temporary tables are referred to as “Temporary Tables”.Volatile temporary tables are referred to as “Volatile Tables”.Global temporary tables are referred to as “Temporary Tables”.

Note:

Volatile and Global Temporary Tables Page 12-27

Page 470: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Temporary Tables Temporary tables are created using the CREATE GLOBAL TEMPORARY TABLE command. This stores the base definition of the table in the data dictionary. Like volatile tables, the defaults are to LOG transactions and ON COMMIT DELETE ROWS. Temporary tables may be altered by the ALTER command to change any attributes of the table, similar to perm tables. Once the table is accessed by a DML command, such as the INSERT SELECT seen on the facing-page, the table is considered materialized and a row is entered into a dictionary view called DBC.Temptables. Deleting all rows from a temporary table does not de-materialize the table. The instance of the table must be dropped or the session must be ended for the materialized table to be discarded.

Volatile and Global Temporary Tables Page 12-28

Page 471: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Temporary Tables

CREATE GLOBAL TEMPORARY TABLE gt_deptsal(deptno SMALLINT,avgsal DEC(9,2),maxsal DEC(9,2),minsal DEC(9,2),sumsal DEC(9,2),empcnt SMALLINT);

CREATE GLOBAL TEMPORARY TABLE gt_deptsal(deptno SMALLINT,avgsal DEC(9,2),maxsal DEC(9,2),minsal DEC(9,2),sumsal DEC(9,2),empcnt SMALLINT);

ALTER TABLE gt_deptsal, ON COMMIT PRESERVE ROWS;

ALTER TABLE gt_deptsal, ON COMMIT PRESERVE ROWS;

INSERT INTO gt_deptsalSELECT dept ,AVG(sal) ,MAX(sal) ,MIN(sal)

,SUM(sal) ,COUNT(emp)FROM empGROUP BY 1;

INSERT INTO gt_deptsalSELECT dept ,AVG(sal) ,MAX(sal) ,MIN(sal)

,SUM(sal) ,COUNT(emp)FROM empGROUP BY 1;

DELETE FROM gt_deptsal; DELETE FROM gt_deptsal;

• Base table definition is stored in DD

• Default is ON COMMIT DELETE ROWS

• Base table definition is stored in DD

• Default is ON COMMIT DELETE ROWS

ALTER TABLE can be done to change defaultsALTER TABLE can be done to change defaults

• Table is now materialized

• Row is inserted in DBC.Temptables

• Table is now materialized

• Row is inserted in DBC.Temptables

Table remains materialized until it is droppedTable remains materialized until it is dropped

Volatile and Global Temporary Tables Page 12-29

Page 472: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Space Allocations A new category of space called “temporary space” is used for instances of temporary tables. An allocation of temporary space must be made for any user who will materialize a temporary table. The total of all perm and temporary space allocations in the system must equal the amount of space available for tables system-wide. Unlike spool space, temporary space remains allocated through a system reset, enabling temporary tables to survive such a reset. Unlike perm space, temporary space is deallocated at the end of the session, or sooner if the instance of the table is dropped. Spool space is used for realizations of derived and volatile tables.

Volatile and Global Temporary Tables Page 12-30

Page 473: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Space Allocations

CREATE USER john AS PERM = 5000000,PASSWORD = lucky,SPOOL = 5000000,TEMPORARY = 2000000;

CREATE USER john AS PERM = 5000000,PASSWORD = lucky,SPOOL = 5000000,TEMPORARY = 2000000;

PERM TEMP SPOOL DD/D

Perm tables TemporaryInstances

Volatile InstancesDerived tables

Global BaseDefinitions

• Temporary space, like perm space, survives a system reset. • Like spool space, it is deallocated at the session end.• If unspecified, default is set to max of immediate owner.

• Temporary space, like perm space, survives a system reset. • Like spool space, it is deallocated at the session end.• If unspecified, default is set to max of immediate owner.

CREATE SET TABLE dbc.temptables ,FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL (HostNo SMALLINT FORMAT '---,--9' NOT NULL,SessionNo INTEGER FORMAT '--,---,---,--9' NOT NULL,TableId BYTE(6) NOT NULL,BaseDbId BYTE(4) NOT NULL,BaseTableId BYTE(6) NOT NULL,AccountDbId BYTE(4) NOT NULL,StatisticsCnt SMALLINT FORMAT '---,--9' NOT NULL)

PRIMARY INDEX ( HostNo ,SessionNo );

CREATE SET TABLE dbc.temptables ,FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL (HostNo SMALLINT FORMAT '---,--9' NOT NULL,SessionNo INTEGER FORMAT '--,---,---,--9' NOT NULL,TableId BYTE(6) NOT NULL,BaseDbId BYTE(4) NOT NULL,BaseTableId BYTE(6) NOT NULL,AccountDbId BYTE(4) NOT NULL,StatisticsCnt SMALLINT FORMAT '---,--9' NOT NULL)

PRIMARY INDEX ( HostNo ,SessionNo );

Dictionary table DBC.Temptables carries one row for each materialized instance of any temporary table

Dictionary table DBC.Temptables carries one row for each materialized instance of any temporary table

Volatile and Global Temporary Tables Page 12-31

Page 474: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Getting Help on Temporary Tables Obtaining HELP on temporary tables is no different than on perm tables. HELP DATABASE will show temporary tables as objects in the database just like perm tables, views, macros or triggers. HELP TABLE will provide the column names and data types. SHOW TABLE shows the currently active definition of the temporary table.

Volatile and Global Temporary Tables Page 12-32

Page 475: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Getting Help on Temporary Tables

HELP DATABASE ped;HELP DATABASE ped;

Table/View/Macro Kind Commentemployee T ? empsamp T ? exceed_10_pcent T ? gt_deptsal T ?

Table/View/Macro Kind Commentemployee T ? empsamp T ? exceed_10_pcent T ? gt_deptsal T ?

Column Name Type Commentdeptno I2 ? avgsal D ? maxsal D ? minsal D ? sumsal D ? empcnt I2 ?

Column Name Type Commentdeptno I2 ? avgsal D ? maxsal D ? minsal D ? sumsal D ? empcnt I2 ?

HELP TABLE gt_deptsal;HELP TABLE gt_deptsal;

CREATE SET GLOBAL TEMPORARY TABLE PED.gt_deptsal,NO FALLBACK , LOG

(deptno SMALLINT,avgsal DECIMAL(9,2),maxsal DECIMAL(9,2),minsal DECIMAL(9,2),sumsal DECIMAL(9,2),empcnt SMALLINT)

PRIMARY INDEX ( deptno ) ON COMMIT PRESERVE ROWS;

CREATE SET GLOBAL TEMPORARY TABLE PED.gt_deptsal,NO FALLBACK , LOG

(deptno SMALLINT,avgsal DECIMAL(9,2),maxsal DECIMAL(9,2),minsal DECIMAL(9,2),sumsal DECIMAL(9,2),empcnt SMALLINT)

PRIMARY INDEX ( deptno ) ON COMMIT PRESERVE ROWS;

SHOW TABLE gt_deptsal;SHOW TABLE gt_deptsal;

Volatile and Global Temporary Tables Page 12-33

Page 476: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Temporary Tables Temporary tables, like volatile tables, are used for denormalization and aggregation purposes. As before with volatile tables, we can create a temporary table with aggregated information on a department basis. We then can use this table in conjunction with others to produce reports like the ones shown in the facing examples.

Volatile and Global Temporary Tables Page 12-34

Page 477: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Temporary Tables

SELECT emp, last, dept, salFROM emp INNER JOIN gt_deptsalON dept = deptnoWHERE sal = maxsalORDER BY 3;

SELECT emp, last, dept, salFROM emp INNER JOIN gt_deptsalON dept = deptnoWHERE sal = maxsalORDER BY 3;

Show all employees who make the maximum salary in their deptemp last dept sal801 Trainer 100 100000.00 1021 Morrissey 201 38750.00 1019 Kubic 301 57700.00 1016 Rogers 302 56500.00 1010 Rogers 401 46000.00 1011 Daly 402 52500.00 1007 Villegas 403 49700.00 1017 Runyon 501 66000.00

emp last dept sal801 Trainer 100 100000.00 1021 Morrissey 201 38750.00 1019 Kubic 301 57700.00 1016 Rogers 302 56500.00 1010 Rogers 401 46000.00 1011 Daly 402 52500.00 1007 Villegas 403 49700.00 1017 Runyon 501 66000.00

Show employees who are in a department of less than 3 people:SELECT emp, last, dept, empcntFROM emp INNER JOIN gt_deptsalON dept = deptnoWHERE empcnt < 3;

SELECT emp, last, dept, empcntFROM emp INNER JOIN gt_deptsalON dept = deptnoWHERE empcnt < 3;

emp last dept empcnt 801 Trainer 100 1 1014 Crane 402 2 1025 Short 201 2 1011 Daly 402 2 1021 Morrissey 201 2 1016 Rogers 302 1

emp last dept empcnt 801 Trainer 100 1 1014 Crane 402 2 1025 Short 201 2 1011 Daly 402 2 1021 Morrissey 201 2 1016 Rogers 302 1

Volatile and Global Temporary Tables Page 12-35

Page 478: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Temporary Tables and DDL (1 of 2) In using DDL in conjunction with temporary tables, there are a few simple rules to keep in mind which will help in determining the appropriate command. Rules for use of TEMPORARY and ALL keywords:

• TEMPORARY: Use of this keyword means the command applies to a materialized instance of the temporary table.

• ALL: Use of this keyword means the command applies to both the base table definition

and also all materialized instances.

• Neither: Use of neither keyword in conjunction with a temporary table means the reference is to the base table definition.

• Both: Use of both keywords is syntactically not allowed

Note that it is not possible to delete a base table definition if either of the following are true:

a.) Any materialized instance of the table will continue to exist afterwards. b.) Any materialized instance is involved in a currently active transaction.

Volatile and Global Temporary Tables Page 12-36

Page 479: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Temporary Tables and DDL (1 of 2)

TEMPORARY keyword in command denotes table instance only(otherwise applies to both base definition and instance)

TEMPORARY keyword used for (global) temporary tables onlyTEMPORARY and ALL are mutually exclusive

TEMPORARY keyword in command denotes table instance only(otherwise applies to both base definition and instance)

TEMPORARY keyword used for (global) temporary tables onlyTEMPORARY and ALL are mutually exclusive

TEMPORARY and ALL keywords

Deleting Databases/Users:Deletes all objects in database, including temporary tables.Fails if any instances of temporary table are materialized.Deletes all objects in database, including temporary tables.Fails if any instances of temporary table are materialized.DELETE DATABASE db1;DELETE DATABASE db1;

Deletes all objects, including materialized temporary tables.Deletes all objects, including materialized temporary tables.DELETE DATABASE db1 ALL;DELETE DATABASE db1 ALL;

Drops base and all instances.Fails if any instance is in an active transaction.Drops base and all instances.Fails if any instance is in an active transaction.DROP TABLE gt_deptsal ALL;DROP TABLE gt_deptsal ALL;

Dropping tables:

Showing table definitions:Shows definition of base table.Shows definition of materialized instance.Shows definition of base table.Shows definition of materialized instance.

SHOW TABLE gt_deptsal;SHOW TEMPORARY TABLE gt_deptsal;SHOW TABLE gt_deptsal;SHOW TEMPORARY TABLE gt_deptsal;

DROP TEMPORARY TABLE gt_deptsalDROP TEMPORARY TABLE gt_deptsal Drops local instance of table only.Drops local instance of table only.

DROP TABLE gt_deptsal;DROP TABLE gt_deptsal; Drops base definition and local instance if present.Fails is there are other instances of the table in the system.Drops base definition and local instance if present.Fails is there are other instances of the table in the system.

Volatile and Global Temporary Tables Page 12-37

Page 480: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Temporary Tables and DDL (2 of 2) Altering tables Temporary tables may be altered like perm tables, however, the base definition may not be changed if any instance of the table exists. The ALTER command applies only to the base definition of the temporary table. Views and Macros It is possible to materialize an instance of a table by executing a macro which executes DML against the temporary table. Even a SELECT against a table which is yet unpopulated has the effect of materializing what is essentially an empty table. Granting/Revoking Privileges Privileges apply to the base table only. The only rights needed by the user are the necessary DML privileges to materialize the table. After the table has been materialized, no further access rights are checked. HELP on indexes Because an instance of a table may have additional secondary indexes added, a HELP TEMPORARY INDEX command is available to show such indexes. HELP on statistics Because an instance of a table may have additional statistics defined for collection, a HELP TEMPORARY STATISTICS command is available to show statistics on the table instance.

Volatile and Global Temporary Tables Page 12-38

Page 481: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Temporary Tables and DDL (2 of 2)

Altering tables:• Also used to alter LOG option.• Changes base definition of table.• Fails if there are any materialized instances.

• Also used to alter LOG option.• Changes base definition of table.• Fails if there are any materialized instances.

ALTER TABLE gt_deptsal, ON COMMIT PRESERVE ROWS;

ALTER TABLE gt_deptsal, ON COMMIT PRESERVE ROWS;

Views and macros:• Macros and views must reference base table

definition.• Execution of macro or view can materialize table.

• Macros and views must reference base table definition.

• Execution of macro or view can materialize table.CREATE MACRO gt1 AS

(SELECT * FROM gt_deptsal;);CREATE MACRO gt1 AS

(SELECT * FROM gt_deptsal;);

Granting/Revoking privileges:• Applies to base definition only.• Appropriate DML rights needed to materialize table.• DML rights needed on base or on containing database.• Once table is materialized, no rights are checked.

• Applies to base definition only.• Appropriate DML rights needed to materialize table.• DML rights needed on base or on containing database.• Once table is materialized, no rights are checked.

GRANT INSERT, SELECT ON gt_deptsal TO user1;

GRANT INSERT, SELECT ON gt_deptsal TO user1;

HELP on indexes:• Shows indexes defined on materialized table.• Without TEMPORARY, shows indexes on base.• Shows indexes defined on materialized table.• Without TEMPORARY, shows indexes on base.HELP TEMPORARY INDEX gt_deptsal;HELP TEMPORARY INDEX gt_deptsal;

HELP on statistics:• Shows statistics defined on materialized table.• Without TEMPORARY, shows statistics

defined on base.

• Shows statistics defined on materialized table.• Without TEMPORARY, shows statistics

defined on base.HELP TEMPORARY STATISTICS gt_deptsal;HELP TEMPORARY STATISTICS gt_deptsal;

Volatile and Global Temporary Tables Page 12-39

Page 482: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Secondary Indexes Secondary indexes are added to a table to improve performance for certain queries which make use of the columns which define the index. These indexes may be added to and dropped from temporary tables much as they would be with perm tables. A difference with temporary tables is the need to understand whether the index is being defined for the base table (in which case it will be realized for all materialized instances) or whether it is being defined solely for the one instance. The distinction is controlled by use of the keyword TEMPORARY. If an index is defined using this keyword, the index will be created for the instance only. When the keyword is absent, the index is built for the base definition. There are a few rules about building secondary indexes on temporary table. They are:

1.) An index for a temporary table may never be named. 2.) An index for a base table may not be added if any instances exist. 3.) An index for an instance may not be added if it already is defined on the base. 4.) Once defined for the base, all materialized instances will have this index.

CREATE GLOBAL TEMPORARY TABLE gt_deptsal(deptno SMALLINT ,avgsal DEC(9,2) ,maxsal DEC(9,2) ,minsal DEC(9,2) ,sumsal DEC(9,2) ,empcnt SMALLINT);

The following table is used in the facing page examples:

Volatile and Global Temporary Tables Page 12-40

Page 483: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Secondary IndexesSecondary indexes may be added to a temporary table.They may be added either to the base or to an instance.

CREATE INDEX (empcnt) ON gt_deptsal;

CREATE INDEX (empcnt) ON gt_deptsal;

CREATE SET GLOBAL TEMPORARY TABLE PED.gt_deptsal,NO FALLBACK , LOG

( deptno SMALLINT, avgsal DECIMAL(9,2),maxsal DECIMAL(9,2), minsal DECIMAL(9,2), sumsal DECIMAL(9,2), empcnt SMALLINT)

PRIMARY INDEX ( deptno ) INDEX ( empcnt ) ON COMMIT PRESERVE ROWS;

CREATE SET GLOBAL TEMPORARY TABLE PED.gt_deptsal,NO FALLBACK , LOG

( deptno SMALLINT, avgsal DECIMAL(9,2),maxsal DECIMAL(9,2), minsal DECIMAL(9,2), sumsal DECIMAL(9,2), empcnt SMALLINT)

PRIMARY INDEX ( deptno ) INDEX ( empcnt ) ON COMMIT PRESERVE ROWS;

SHOW TABLE gt_deptsal;SHOW TABLE gt_deptsal;

• Creates index on materialized instance.• Fails if index already defined as part of base table.• Creates index on materialized instance.• Fails if index already defined as part of base table.

CREATE SET GLOBAL TEMPORARY TABLE PED.gt_deptsal,NO FALLBACK , LOG

( deptno SMALLINT, avgsal DECIMAL(9,2),maxsal DECIMAL(9,2), minsal DECIMAL(9,2), sumsal DECIMAL(9,2), empcnt SMALLINT)

PRIMARY INDEX ( deptno ) INDEX ( empcnt ) ON COMMIT PRESERVE ROWS;

CREATE SET GLOBAL TEMPORARY TABLE PED.gt_deptsal,NO FALLBACK , LOG

( deptno SMALLINT, avgsal DECIMAL(9,2),maxsal DECIMAL(9,2), minsal DECIMAL(9,2), sumsal DECIMAL(9,2), empcnt SMALLINT)

PRIMARY INDEX ( deptno ) INDEX ( empcnt ) ON COMMIT PRESERVE ROWS;

• Creates index for column ‘empcnt’ on base table definition.• Fails if an instance of the table already exists.• Subsequent instances of table will have this index.• Can never use a “named” index with temporary tables.

• Creates index for column ‘empcnt’ on base table definition.• Fails if an instance of the table already exists.• Subsequent instances of table will have this index.• Can never use a “named” index with temporary tables.

CREATE INDEX (empcnt) ON TEMPORARY gt_deptsal;

CREATE INDEX (empcnt) ON TEMPORARY gt_deptsal;

SHOW TEMPORARY TABLE gt_deptsal;

SHOW TEMPORARY TABLE gt_deptsal;

Volatile and Global Temporary Tables Page 12-41

Page 484: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Dropping Secondary Indexes Secondary indexes may be dropped from either a base table or an instance. The keyword “TEMPORARY” controls where the index is to be dropped from. It is not possible to drop an index from a base table while an instance of the table is materialized.

Volatile and Global Temporary Tables Page 12-42

Page 485: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Dropping Secondary IndexesSecondary indexes may be dropped from a temporary table.They may be dropped either from the base or from an instance.

Drops index on materialized instance.Drops index on materialized instance.

HELP TEMPORARY INDEX gt_deptsal;

DROP INDEX (empcnt) ON gt_deptsal;DROP INDEX ( ) ON gt_deptsal;

HELP INDEX gt_deptsal; HELP INDEX gt_deptsal;

Note: Base table keeps index definition.

DROP INDEX (empcnt) ON TEMPORARY gt_deptsal;

Primary or Unique?

empcnt

Note: Base table keeps index definition.

Drops index on base table definition.Fails if an instance of the table exists.Drops index on base table definition.Fails if an instance of the table exists.

Secondary? Column Names N P deptno

Primary or Unique? Secondary? Column Names N P deptno

Primary or Unique? Secondary? Column NamesN P deptno N S empcnt

Primary or Unique? Secondary? Column NamesN P deptno N S empcnt

Volatile and Global Temporary Tables Page 12-43

Page 486: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Collecting Statistics Collecting Statistics may be done on either a base table or on a materialized instance. There are three forms of the COLLECT STATISTICS command in temporary table processing:

COLLECT STATISTICS ON gt_deptsal INDEX (deptno); Defines a base table index for which statistics are to be collected

COLLECT STATISTICS ON gt_deptsal COLUMN avgsal; Defines a base table column for which statistics are to be collected

COLLECT STATISTICS ON gt_deptsal;

Actually collects statistics on all defined columns and indexes (Note, this command is not meaningful for a base table as there are no rows to collect from). By using the keyword TEMPORARY, we may again control whether these commands apply to the base table or to an instance of a table.

COLLECT STATISTICS ON TEMPORARY gt_deptsal INDEX (deptno); Defines an index on the instance for which statistics are to be collected

COLLECT STATISTICS ON TEMPORARY gt_deptsal COLUMN avgsal;

Defines a column on the instance for which statistics are to be collected COLLECT STATISTICS ON TEMPORARY gt_deptsal;

Collects statistics on the instance for all defined columns and indexes The following table is used in the facing page examples:

CREATE GLOBAL TEMPORARY TABLE gt_deptsal (deptno SMALLINT ,avgsal DEC(9,2) ,maxsal DEC(9,2) ,minsal DEC(9,2) ,sumsal DEC(9,2) ,empcnt SMALLINT);

Volatile and Global Temporary Tables Page 12-44

Page 487: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Collecting Statistics

May be done on a base table or a materialized instance.Done on a base table - defines which columns will be collected.Done on an instance - actually collects and stores statistics.

May be done on a base table or a materialized instance.Done on a base table - defines which columns will be collected.Done on an instance - actually collects and stores statistics.

COLLECT STATISTICS ON gt_deptsal index (deptno);COLLECT STATISTICS ON gt_deptsal index (deptno);

COLLECT STATISTICS ON gt_deptsal column avgsal;COLLECT STATISTICS ON gt_deptsal column avgsal;

HELP STATISTICS gt_deptsal; HELP STATISTICS gt_deptsal; Date Time Unique Vals Col Names 98/09/11 13:38:37 0 deptno 98/09/11 13:39:01 0 avgsal

Date Time Unique Vals Col Names 98/09/11 13:38:37 0 deptno 98/09/11 13:39:01 0 avgsal

INSERT INTO gt_deptsal SELECT dept ,AVG(sal) ,MAX(sal) ,MIN(sal),SUM(sal) ,COUNT(emp)FROM emp GROUP BY 1;

INSERT INTO gt_deptsal SELECT dept ,AVG(sal) ,MAX(sal) ,MIN(sal),SUM(sal) ,COUNT(emp)FROM emp GROUP BY 1;

COLLECT STATISTICS ON gt_deptsal; COLLECT STATISTICS ON gt_deptsal;

COLLECT STATISTICS ON TEMPORARY gt_deptsal;COLLECT STATISTICS ON TEMPORARY gt_deptsal;

HELP TEMPORARY STATISTICS gt_deptsal; HELP TEMPORARY STATISTICS gt_deptsal; Date Time Unique Vals Col Names 98/09/11 13:46:08 8 deptno 98/09/11 13:46:08 8 avgsal

Date Time Unique Vals Col Names 98/09/11 13:46:08 8 deptno 98/09/11 13:46:08 8 avgsal

Defines index to be collected.

Defines index to be collected.

Defines column to be collected.Defines column to be collected.

Fails; not allowed when table is materialized.There are no statistics to collect on base table.Fails; not allowed when table is materialized.There are no statistics to collect on base table.

Collects statistics on the instance.Collects statistics on the instance.

Volatile and Global Temporary Tables Page 12-45

Page 488: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Dropping Statistics Dropping Statistics may be done on either a base table or on a materialized instance. There are three forms of the DROP STATISTICS command in temporary table processing:

DROP STATISTICS ON gt_deptsal INDEX (deptno); Defines a base table index for which statistics are to be dropped

DROP STATISTICS ON gt_deptsal COLUMN avgsal;

Defines a base table column for which statistics are to be dropped DROP STATISTICS ON gt_deptsal;

Actually drops statistics on all defined columns and indexes (Note, this command is not meaningful for a base table as there are no collected statistics to drop). By using the keyword TEMPORARY, we may again control whether these commands apply to the base table or to an instance of a table:

DROP STATISTICS ON TEMPORARY gt_deptsal INDEX (deptno); Defines an index on the instance for which statistics are to be dropped

DROP STATISTICS ON TEMPORARY gt_deptsal COLUMN avgsal; Defines a column on the instance for which statistics are to be dropped

DROP STATISTICS ON TEMPORARY gt_deptsal; Drops statistics on the instance for all defined columns and indexes The following table is used in the facing page examples:

CREATE GLOBAL TEMPORARY TABLE gt_deptsal (deptno SMALLINT ,avgsal DEC(9,2) ,maxsal DEC(9,2) ,minsal DEC(9,2) ,sumsal DEC(9,2) ,empcnt SMALLINT);

Volatile and Global Temporary Tables Page 12-46

Page 489: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Dropping Statistics

May be done on a base table or a materialized instance.Done on a base table; drops column(s) from base definition.Done on an instance; drops statistics from the instance.

May be done on a base table or a materialized instance.Done on a base table; drops column(s) from base definition.Done on an instance; drops statistics from the instance.

HELP TEMPORARY STATISTICS gt_deptsal; HELP TEMPORARY STATISTICS gt_deptsal; Date Time Unique Vals Col Names98/09/11 13:46:08 8 deptnoDate Time Unique Vals Col Names98/09/11 13:46:08 8 deptno

HELP STATISTICS gt_deptsal; HELP STATISTICS gt_deptsal; Date Time Unique Vals Col Names98/09/11 13:38:37 0 deptno98/09/11 13:39:01 0 avgsal

Date Time Unique Vals Col Names98/09/11 13:38:37 0 deptno98/09/11 13:39:01 0 avgsal

DROP STATISTICS ON gt_deptsal;DROP STATISTICS ON gt_deptsal; Fails; not allowed when table is materialized.Fails; not allowed when table is materialized.

DROP STATISTICS ON TEMPORARY gt_deptsal column avgsal;DROP STATISTICS ON TEMPORARY gt_deptsal column avgsal; Drops stats on cols avgsal for table instance.Drops stats on cols avgsal for table instance.

DROP STATISTICS ON TEMPORARY gt_deptsal;DROP STATISTICS ON TEMPORARY gt_deptsal; Drop all statistics for table instance. Drop all statistics for table instance.

HELP TEMPORARY STATISTICS gt_deptsal;HELP TEMPORARY STATISTICS gt_deptsal; No statistics returned.All statistics are dropped from instance.No statistics returned.All statistics are dropped from instance.

COLLECT STATISTICS ON TEMPORARY gt_deptsal;COLLECT STATISTICS ON TEMPORARY gt_deptsal; Fails; no statistics defined for instance.Fails; no statistics defined for instance.

Volatile and Global Temporary Tables Page 12-47

Page 490: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercises

Volatile and Global Temporary Tables Page 12-48

Page 491: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercises

Which of the three choices below would be most suitable for each of the numbered hypothetical situations:

DT - Derived tableVT - Volatile tableGT - Global Temp table

1.) “When end-of-month queries are run this weekend, make sure that the scripts are set up to populate the necessary temporary tables without having to create them.”

2.) “We’ve just been handed an ad-hoc request for information which will requireus to run a bunch of queries against a summary of last years sales.”

3.) “The boss says he doesn’t care if it’s extra work. He wants that information at the end of this week and if he likes it he’s going to want it every week.”

4.) “This query requires me to denormalize information in three different tables and then join the results. Fortunately, I only need to do it this one time.”

5.) “Get me the top ten products by revenue and the top ten by profitabilty do an outer join on them.”

6.) “We need to agree with the marketing people on a single table layout so that our numbers can be compared with theirs at week’s end.”

Volatile and Global Temporary Tables Page 12-49

Page 492: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

Volatile and Global Temporary Tables Page 12-50

Page 493: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

1.) Create a global temporary table called gt_deptsal, which preserves rows on commit And which does not use the LOG option. The table should follow the specifications as follows:

deptno SMALLINT avgsal DEC(9,2)maxsal DEC(9,2)minsal DEC(9,2)sumsal DEC(9,2)empcnt SMALLINT)

2.) Populate the table by insert selecting from the ‘emp’ view in the CS_Views database to create the appropriate aggregates.

3.) Create a volatile table as follows:CREATE VOLATILE TABLE SQL00.vt_emp_job_dept, NO LOG

(empno INT,deptname CHAR(15),job_desc CHAR(15))UNIQUE PRIMARY INDEX (empno) ON COMMIT PRESERVE ROWS;

4.) Populate this denormalized table by insert selecting from an outer join of the job table and the emp view. Make sure there is a row for each employee in the table.

5.) Show the department name and the job description of the highest paid person in each department. Do not show employee id or salary. Use both the temporary and the volatile tables to solve this. Order results by department name.

6.) Show the departments in the top 50% of average salary with head counts of less than six. Show department name, average salary and head count.

Volatile and Global Temporary Tables Page 12-51

Page 494: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Volatile and Global Temporary Tables Page 12-52

Page 495: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 13

Module 13Table Creation from Existing Tables

After completing this module, you should be able to:

• Create empty tables based on existing table definitions.

• Create populated tables based on existing table definitions and data content.

• Create tables from multiple table sources.

Table Creation from Existing Tables Page 13-1

Page 496: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table Creation from Existing Tables Page 13-2

Page 497: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Table Creation from Existing Tables Page 13-3

Page 498: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Empty Tables Using Existing Definitions The creation of tables from existing table definitions is facilitated by the use of the CREATE TABLE … AS syntax. To create a table using existing table definitions but without copying the data, use the WITH NO DATA syntax. To perform this operation, the user will be required to have both the CREATE TABLE privilege in the destination database/user space, and the SELECT privilege against the source tables. Example This example creates a new table called 'dept1' with the same definition as the table 'department'.

CREATE TABLE dept1 AS department WITH NO DATA; Doing a SHOW TABLE on either table will produce the same table definition:

SHOW TABLE dept1; CREATE SET TABLE PED.dept1 ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( department_number SMALLINT, department_name CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL, budget_amount DECIMAL(10,2), manager_employee_number INTEGER)

UNIQUE PRIMARY INDEX ( department_number ) ;

Table Creation from Existing Tables Page 13-4

Page 499: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Empty Tables Using Existing Definitions

CREATE TABLE NewTableName AS ExistingTableName WITH {NO} DATA;CREATE TABLE NewTableName AS ExistingTableName WITH {NO} DATA;

Create Table in target database/user spaceSelect on source tableCreate Table in target database/user spaceSelect on source table

CREATE TABLE dept1 AS department WITH NO DATA;CREATE TABLE dept1 AS department WITH NO DATA;

Privileges Required:

Syntax:

Create a new table called 'dept1' with the same definition as the table 'department'.

Creates a table with the same definition as department but without copying data rows.Creates a table with the same definition as department but without copying data rows.

Table Creation from Existing Tables Page 13-5

Page 500: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Copied Attributes Most standard column attributes are copied:

• Column names • Data types • Default values • NOT NULL constraints • CHECK constraints • UNIQUE constraints • PRIMARY KEY constraints

Most table level attributes are copied:

• Fallback options • Journal options (permanent tables only) • All indexes (except defined join indexes)

Some attributes are not copied:

• References constraints • Triggers (which reference source table)

Note: Attributes may be overridden as part of CREATE TABLE...AS statement:

Table Creation from Existing Tables Page 13-6

Page 501: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Copied Attributes

• Column names • Data types • Default values • NOT NULL constraints • CHECK constraints • UNIQUE constraints • PRIMARY KEY constraints

• Column names • Data types • Default values • NOT NULL constraints • CHECK constraints • UNIQUE constraints • PRIMARY KEY constraints

• REFERENCES (Foreign Key) constraints • Triggers (which reference source table) • REFERENCES (Foreign Key) constraints • Triggers (which reference source table)

Most standard column attributes are copied:

Some attributes are not copied:

Table Creation from Existing Tables Page 13-7

Page 502: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Overriding Copied Attributes Attributes may be overridden as part of CREATE TABLE... AS statement:

SHOW TABLE department; CREATE SET TABLE department ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( department_number SMALLINT, department_name CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL, budget_amount DECIMAL(10,2), manager_employee_number INTEGER)

UNIQUE PRIMARY INDEX ( department_number ); Example:This example makes the target table ‘dept1’ fallback, and adds a secondary index.

CREATE TABLE dept1, FALLBACK AS department WITH NO DATA UNIQUE INDEX (department_name); SHOW TABLE dept1; CREATE SET TABLE PED1.dept1 ,FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( department_number SMALLINT, department_name CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL, budget_amount DECIMAL(10,2), manager_employee_number INTEGER)

PRIMARY INDEX ( department_number ) UNIQUE INDEX ( department_name );

Note: The inclusion of the secondary index, causes the primary index to default to the first column as a NUPI. When an existing table (not a subquery) is specified in the AS clause and indexes are specified in the Index Definition clause, then source table indexes are not copied to the target table.

Table Creation from Existing Tables Page 13-8

Page 503: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Overriding Copied Attributes

CREATE TABLE dept1 , FALLBACK AS department WITH NO DATAUNIQUE INDEX (department_name);

CREATE TABLE dept1 , FALLBACK AS department WITH NO DATAUNIQUE INDEX (department_name);

Example:

Attributes may be overridden as part of a CREATE TABLE...AS statementAttributes may be overridden as part of a CREATE TABLE...AS statement

CREATE SET TABLE PED1.dept1 ,FALLBACK ,

UNIQUE INDEX ( department_name );

NO BEFORE JOURNAL, NO AFTER JOURNAL ( department_number SMALLINT, department_name CHAR(30) CHARACTER SET

LATIN NOT CASESPECIFIC NOT NULL, budget_amount DECIMAL(10,2), manager_employee_number INTEGER) PRIMARY INDEX ( department_number )

CREATE SET TABLE PED1.dept1 ,NO BEFORE JOURNAL, NO AFTER JOURNAL ( department_number SMALLINT, department_name CHAR(30) CHARACTER SET

LATIN NOT CASESPECIFIC NOT NULL, budget_amount DECIMAL(10,2), manager_employee_number INTEGER) PRIMARY INDEX ( department_number )

SHOW TABLE dept1;

FALLBACK ,

UNIQUE INDEX ( department_name );

SHOW TABLE dept1;

The inclusion of the secondary index causes the primary index to default to the first defined column as a NUPI.The inclusion of the secondary index causes the primary index to default to the first defined column as a NUPI.

Note:

Table Creation from Existing Tables Page 13-9

Page 504: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Tables Using Subqueries When the subquery form of CREATE AS is used:

• Table attributes (such as FALLBACK) are not copied from the source table. • Table attributes are copied from standard system defaults (e.g., NO FALLBACK) unless

otherwise specified. • Secondary indexes, if present, are not copied from the source table. • The first column specified (employee_number) is created as a NUPI unless otherwise

specified. There are some limitations on the use of subqueries for table creation:

• The ORDER BY clause is not allowed. • All columns or expressions must have an assigned or defaulted name.

Use of subqueries allows any of the following features:

• Join expressions (including outer joins) • OLAP functions • Embedded subqueries

Table Creation from Existing Tables Page 13-10

Page 505: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Tables Using Subqueries

CREATE SET TABLE emp1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( employee_number INTEGER,

department_number INTEGER, salary_amount DECIMAL(10,2) NOT NULL)

PRIMARY INDEX ( employee_number );

CREATE SET TABLE emp1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( employee_number INTEGER,

department_number INTEGER, salary_amount DECIMAL(10,2) NOT NULL)

PRIMARY INDEX ( employee_number );

CREATE TABLE emp1 AS (SELECT employee_number

,department_number ,salary_amount

FROM employee) WITH NO DATA;

CREATE TABLE emp1 AS (SELECT employee_number

,department_number ,salary_amount

FROM employee) WITH NO DATA;

Example:

Join expressionsAggregates OLAP functionsEmbedded subqueries

Join expressionsAggregates OLAP functionsEmbedded subqueries

Subqueries can include:

Can limit column and row selection for the target table. Does not carry table attributes such as FALLBACK.Does not carry index definitions.

Can limit column and row selection for the target table. Does not carry table attributes such as FALLBACK.Does not carry index definitions.

SHOW TABLE emp1;SHOW TABLE emp1;

• Table attributes (FALLBACK) are not copied from source table.

• Secondary indexes (if exist) are not copied from source table.

• First column listed becomes a NUPI unless otherwise specified.

• Table attributes (FALLBACK) are not copied from source table.

• Secondary indexes (if exist) are not copied from source table.

• First column listed becomes a NUPI unless otherwise specified.

Use of Subqueries in table creation

ORDER BY clauseUnnamed columnsORDER BY clauseUnnamed columns

Subqueries cannot include:

When creating tables using subqueries:

Table Creation from Existing Tables Page 13-11

Page 506: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Renaming Columns Columns may be renamed using the AS clause (the Teradata NAMED extension may also be used). Example This example changes the column names of the subset of columns used for the target table.

CREATE TABLE emp1 AS (SELECT employee_number AS emp

,department_number AS dept ,salary_amount AS sal

FROM employee) WITH NO DATA; SHOW TABLE emp1; CREATE SET TABLE PED1.emp1 ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( emp INTEGER, ,dept INTEGER ,sal DECIMAL(10,2))

PRIMARY INDEX ( emp ); An alternate coding approach, to achieve the same result is as follows:

CREATE TABLE emp1(emp, dept, sal) AS (SELECT employee_number

,department_number ,salary_amount

FROM employee) WITH NO DATA;

Table Creation from Existing Tables Page 13-12

Page 507: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Renaming Columns

Two techniques are available for renaming columns.Two techniques are available for renaming columns.

CREATE SET TABLE SQL00.emp1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL (emp INTEGER, ,dept INTEGER,sal DECIMAL(10,2))

PRIMARY INDEX ( emp );

CREATE SET TABLE SQL00.emp1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL (emp INTEGER, ,dept INTEGER,sal DECIMAL(10,2))

PRIMARY INDEX ( emp );

SHOW TABLE emp1;SHOW TABLE emp1;

Technique #2CREATE TABLE emp1 AS (SELECT employee_number AS emp

AS dept AS sal

,department_number ,salary_amount

FROM employee) WITH NO DATA;

Technique #1CREATE TABLE emp1 AS (SELECT employee_number ,department_number ,salary_amount

FROM employee) WITH NO DATA;

(emp, dept, sal)

Table Creation from Existing Tables Page 13-13

Page 508: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Changing Column Attributes Column attributes may be altered for the target table. Example 1 This example changes the column names of both columns and the data type of one column.

CREATE TABLE dept1 AS (SELECT department_number AS dept ,CAST(budget_amount AS INTEGER) AS budget FROM department) WITH NO DATA;

SHOW TABLE dept1; CREATE SET TABLE PED1.dept1 ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( dept SMALLINT, budget INTEGER)

PRIMARY INDEX ( dept ); Example 2 This example changes the column names, attributes and the data types of the columns.

CREATE TABLE dept1 (dept DEFAULT 0 UNIQUE NOT NULL, budget CHECK (budget > 0) ) AS (SELECT CAST(department_number AS INTEGER)

,CAST(budget_amount AS INTEGER) FROM department) WITH NO DATA;

SHOW TABLE dept1; CREATE SET TABLE PED1.dept1 ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( dept INTEGER NOT NULL DEFAULT 0 , budget INTEGER CHECK ( budget > 0 ))

UNIQUE PRIMARY INDEX ( dept ); Note: Data type changes must be indicated within the subquery SELECT statement, not the parameter list.

Table Creation from Existing Tables Page 13-14

Page 509: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Changing Column Attributes

NOTE: Data type changes must be indicated within thesubquery SELECT statement, not the parameter list.

NOTE: Data type changes must be indicated within thesubquery SELECT statement, not the parameter list.

Example 1: Changes the column names and the data type of one column.

CREATE TABLE dept1 AS (SELECT department_number AS dept ,CAST(budget_amount AS INTEGER) AS budget

FROM department) WITH NO DATA;

CREATE TABLE dept1 AS (SELECT department_number AS dept ,CAST(budget_amount AS INTEGER) AS budget

FROM department) WITH NO DATA;

Example 2: Changes the column names, attributes and the data types of both columns.

CREATE TABLE dept1 (dept DEFAULT 0 UNIQUE NOT NULL, budget CHECK (budget > 0) ) AS

(SELECT CAST(department_number AS INTEGER) ,CAST(budget_amount AS INTEGER)

FROM department) WITH NO DATA;

CREATE TABLE dept1 (dept DEFAULT 0 UNIQUE NOT NULL, budget CHECK (budget > 0) ) AS

(SELECT CAST(department_number AS INTEGER) ,CAST(budget_amount AS INTEGER)

FROM department) WITH NO DATA;

Column attributes may be altered for the target table.Column attributes may be altered for the target table.

Table Creation from Existing Tables Page 13-15

Page 510: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Populated Tables Using Existing Tables The CREATE TABLE...AS syntax is used to create a table definition from one or more existing table definitions. The WITH DATA option is used to copy data from source table(s) to target table. Example This example creates a new table called 'dept1' with the same definition as the table 'department' and containing the same data.

CREATE TABLE dept1 AS department WITH DATA; We can now verify the contents of 'dept1':

SELECT department_number AS dept_num ,department_name AS dept_name ,budget_amount AS budget ,manager_employee_number AS mgr FROM dept1 ORDER BY 1;

dept_num dept_name budget mgr -------------- ------------- ---------- --------- 100 president 400000.00 801 201 technical operations 293800.00 1025 301 research and development 465600.00 1019 302 product planning 226000.00 1016 401 customer support 982300.00 1003 402 software support 308000.00 1011 403 education 932000.00 1005 501 marketing sales 308000.00 1017 600 None ? 1099

Table Creation from Existing Tables Page 13-16

Page 511: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Populated Tables Using Existing Tables

Use the CREATE TABLE…AS and the WITH DATAoption to create populated tables from existing tables.

dept_num dept_name budget mgr100 president 400000.00 801 201 technical operations 293800.00 1025 301 research and development 465600.00 1019 302 product planning 226000.00 1016 401 customer support 982300.00 1003 402 software support 308000.00 1011 403 education 932000.00 1005 501 marketing sales 308000.00 1017 600 None ? 1099

CREATE TABLE dept1 AS department WITH DATA;CREATE TABLE dept1 AS department WITH DATA;Example

SELECT department_number AS dept_num ,department_name AS dept_name ,budget_amount AS budget ,manager_employee_number AS mgr

FROM dept1 ORDER BY 1;

SELECT department_number AS dept_num ,department_name AS dept_name ,budget_amount AS budget ,manager_employee_number AS mgr

FROM dept1 ORDER BY 1;

Verify

Table Creation from Existing Tables Page 13-17

Page 512: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Joins in a Subquery Joins may be used in a subquery to copy data from more than one table. Example: Create a table showing the department number, the department name and the name of the department manager. In order to show the manager's name, a join to the employee table is needed.

CREATE TABLE dept3 AS (SELECT d.department_number

, d.department_name , e.last_name AS mgr_name

FROM department d INNER JOIN employee e ON e.employee_number = d.manager_employee_number ) WITH DATA;

SHOW TABLE dept3; CREATE SET TABLE PED.dept3 ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( department_number SMALLINT, department_name CHAR(30) CHARACTER SET LATIN

NOT CASESPECIFIC NOT NULL, mgr_name CHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC) PRIMARY INDEX ( department_number ); SELECT * FROM dept3 ORDER BY 1;

deptartment_number deptartment_name Mgr_name ---------------- ----------------------- ------- 100 president Trainer 201 technical operations Short 301 research and development Kubic 302 product planning Rogers 401 customer support Trader 402 software support Daly 403 education Ryan 501 marketing sales Runyan

Table Creation from Existing Tables Page 13-18

Page 513: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Joins In a Subquery

CREATE SET TABLE PED.dept3 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( department_number SMALLINT, department_name CHAR(30)

CHARACTER SET LATINNOT CASESPECIFICNOT NULL,

mgr_name CHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC)

PRIMARY INDEX ( department_number );

SHOW TABLE dept3;SHOW TABLE dept3;

Note: In order to show the manager's name, a join to the employee table is needed.

Note: In order to show the manager's name, a join to the employee table is needed.

Joins may be used in a subquery to copy data from more than one table.Joins may be used in a subquery to copy data from more than one table.

Create a table showing the department number, the department name and the name of the department manager.

CREATE TABLE dept3 AS (SELECT d.department_number

,d.department_name ,e.last_name AS mgr_name

FROM department d INNER JOIN employee e ON e.employee_number =

d.manager_employee_number ) WITH DATA;

CREATE TABLE dept3 AS (SELECT d.department_number

,d.department_name ,e.last_name AS mgr_name

FROM department d INNER JOIN employee e ON e.employee_number =

d.manager_employee_number ) WITH DATA;

Table Creation from Existing Tables Page 13-19

Page 514: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Calculations and Expressions Calculations and expressions may be used for the columns of the target table. Example Create and populate a table which shows an employee's number, last name, hire date, birth date and a calculated column showing the employee's age at time of hire called 'hire_age'. The data type of the calculated column is determined by the result of the expression. In this case, a data type of INTERVAL YEAR(2) is assigned as seen in the following: Right page solution: SHOW TABLE emp2; CREATE SET TABLE emp2 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL, CHECKSUM = DEFAULT ( emp INTEGER, last CHAR(20) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL, hire DATE FORMAT 'YY/MM/DD' NOT NULL, birth DATE FORMAT 'YY/MM/DD' NOT NULL, hire_age INTERVAL YEAR(2)) PRIMARY INDEX ( emp ); Alternate solution:

CREATE TABLE emp2a(emp,last,hire,birth,hire_age) AS (SELECT employee_number

,last_name ,hire_date ,birthdate ,(hire_date - birthdate)/365.25

FROM employee) WITH DATA;

In this case, a data type of DECIMAL(15,2) is assigned as seen in the following: SHOW TABLE emp2a; CREATE SET TABLE PED1.emp2a ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( emp INTEGER, last CHAR(20) CHARACTER SET LATIN

NOT CASESPECIFIC NOT NULL, hire DATE FORMAT 'YY/MM/DD' NOT NULL, birth DATE FORMAT 'YY/MM/DD' NOT NULL, hire_age DECIMAL(15,2))

PRIMARY INDEX ( emp );

Table Creation from Existing Tables Page 13-20

Page 515: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using Calculations and Expressions

emp last hire birth hire_age 801 Trainer 73/03/01 45/08/11 27

1001 Hoover 76/06/18 50/01/14 261002 Brown 76/07/31 44/08/09 311003 Trader 76/07/31 47/06/19 291004 Johnson 76/10/15 46/04/23 301005 Ryan 76/10/15 55/09/10 211006 Stein 76/10/15 53/10/15 231007 Villegas 77/01/02 37/01/31 391008 Kanieski 77/02/01 58/05/17 181009 Lombardo 77/02/01 45/11/15 311010 Rogers 77/03/01 35/04/23 411011 Daly 77/03/15 49/12/11 271012 Hopkins 77/03/15 42/02/18 351013 Phillips 77/04/01 63/08/10 131014 Crane 78/01/15 60/07/04 17

emp last hire birth hire_age 801 Trainer 73/03/01 45/08/11 27

1001 Hoover 76/06/18 50/01/14 261002 Brown 76/07/31 44/08/09 311003 Trader 76/07/31 47/06/19 291004 Johnson 76/10/15 46/04/23 301005 Ryan 76/10/15 55/09/10 211006 Stein 76/10/15 53/10/15 231007 Villegas 77/01/02 37/01/31 391008 Kanieski 77/02/01 58/05/17 181009 Lombardo 77/02/01 45/11/15 311010 Rogers 77/03/01 35/04/23 411011 Daly 77/03/15 49/12/11 271012 Hopkins 77/03/15 42/02/18 351013 Phillips 77/04/01 63/08/10 131014 Crane 78/01/15 60/07/04 17

SELECT * FROM emp2 WHERE emp < 1015 ORDER BY 1;SELECT * FROM emp2 WHERE emp < 1015 ORDER BY 1;

Calculations and expressions may be used for the columns of the target table.Calculations and expressions may be used for the columns of the target table.

CREATE TABLE emp2(emp, last, hire, birth, hire_age) AS

(SELECT employee_number ,last_name ,hire_date ,birthdate,(hire_date - birthdate) YEAR

FROM employee) WITH DATA;

CREATE TABLE emp2(emp, last, hire, birth, hire_age) AS

(SELECT employee_number ,last_name ,hire_date ,birthdate,(hire_date - birthdate) YEAR

FROM employee) WITH DATA;

Create and populate a table which shows an employee's number, last name, hire date, birth date and the employee's age at time of hire.

Table Creation from Existing Tables Page 13-21

Page 516: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Overriding Data Types Data types may be overridden at table creation time.

Table Creation from Existing Tables Page 13-22

Page 517: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CREATE TABLE emp3 (emp,last,hire,birth,hire_age) AS (SELECT employee_number, last_name, hire_date, birthdate

,CAST((hire_date - birthdate)/365.25 AS INTEGER) FROM employee) WITH DATA;

CREATE TABLE emp3 (emp,last,hire,birth,hire_age) AS (SELECT employee_number, last_name, hire_date, birthdate

,CAST((hire_date - birthdate)/365.25 AS INTEGER) FROM employee) WITH DATA;

Overriding Data Types

Default data types for calculated columns can be overridden in the SELECT subqueryDefault data types for calculated columns can be overridden in the SELECT subquery

SELECT * FROM emp3 WHERE emp < 1015 ORDER BY 1;SELECT * FROM emp3 WHERE emp < 1015 ORDER BY 1;

emp last hire birth hire_age 801 Trainer 73/03/01 45/08/11 27

1001 Hoover 76/06/18 50/01/14 261002 Bro wn 76/07/31 44/08/09 311003 Trader 76/07/31 47/06/19 291004 John son 76/10/15 46/04/23 301005 Ryan 76/10/15 55/09/10 211006 Stein 76/10/15 53/10/15 231007 Villegas 77/01/02 37/01/31 391008 Kanieski 77/02/01 58/05/17 181009 Lombardo 77/02/01 45/11/15 311010 Rogers 77/03/01 35/04/23 411011 Daly 77/03/15 49/12/11 271012 Hopkins 77/03/15 42/02/18 351013 Phillips 77/04/01 63/08/10 131014 Crane 78/01/15 60/07/04 17

emp last hire birth hire_age 801 Trainer 73/03/01 45/08/11 27

1001 Hoover 76/06/18 50/01/14 261002 Bro wn 76/07/31 44/08/09 311003 Trader 76/07/31 47/06/19 291004 John son 76/10/15 46/04/23 301005 Ryan 76/10/15 55/09/10 211006 Stein 76/10/15 53/10/15 231007 Villegas 77/01/02 37/01/31 391008 Kanieski 77/02/01 58/05/17 181009 Lombardo 77/02/01 45/11/15 311010 Rogers 77/03/01 35/04/23 411011 Daly 77/03/15 49/12/11 271012 Hopkins 77/03/15 42/02/18 351013 Phillips 77/04/01 63/08/10 131014 Crane 78/01/15 60/07/04 17

Table Creation from Existing Tables Page 13-23

Page 518: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Setting Default Titles Normally, the column title is the same as the column-name. The Teradata extension 'TITLE' may be used to change the default title. Example Create and populate a table showing the monthly salary of each employee in department 401. Alternative Solution (Non-ANSI standard)

CREATE TABLE monthly_sal_401 (emp, sal TITLE 'Monthly Salary') AS (SELECT employee_number ,salary_amount/12 FROM employee

WHERE department_number = 401) WITH DATA; SHOW TABLE monthly_sal_401; CREATE SET TABLE PED1.monthly_sal_401 ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( emp INTEGER, sal DECIMAL(10,2) TITLE 'Monthly Salary')

PRIMARY INDEX ( emp ); Note: The AS phrase causes a renaming of the column. Double-quoting the column-name allows for non-standard characters in the column-name. By default, the column name becomes the title.

SELECT * FROM monthly_sal_401; emp Monthly Salary ---- -------------- 1003 3154.17 1004 3025.00 1010 3833.33 1001 2127.08 1002 3591.67 1013 2041.67 1022 2691.67

Table Creation from Existing Tables Page 13-24

Page 519: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Setting Default Titles

CREATE TABLE monthly_sal_401 AS (SELECT employee_number AS emp,salary_amount/12 AS "Monthly//Salary"

"Monthly//Salary" D

FROM employee WHERE department_number = 401) WITH DATA;

CREATE SET TABLE PED1.monthly_sal_401,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL (

emp INTEGER, ECIMAL(10,2))

PRIMARY INDEX ( emp );

CREATE SET TABLE PED1.monthly_sal_401,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL (

emp INTEGER, DECIMAL(10,2))

PRIMARY INDEX ( emp );

SHOW TABLE monthly_sal_401;

"Monthly//Salary"

SHOW TABLE monthly_sal_401;

Note: Double-quoting the column-name allows non-standard characters in the column-name.

Note: Double-quoting the column-name allows non-standard characters in the column-name.

MonthlyEmp Salary1003 3154.17 1004 3025.00 1010 3833.33 1001 2127.08 1002 3591.67 1013 2041.67 1022 2691.67

MonthlyEmp Salary1003 3154.17 1004 3025.00 1010 3833.33 1001 2127.08 1002 3591.67 1013 2041.67 1022 2691.67

SELECT * FROM monthly_sal_401;SELECT * FROM monthly_sal_401;

• Title stacking only occurs in BTEQ.• With Teradata SQL Assistant, the

title will appear as 'Monthly Salary'.

• Title stacking only occurs in BTEQ.• With Teradata SQL Assistant, the

title will appear as 'Monthly Salary'.

Table Creation from Existing Tables Page 13-25

Page 520: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Renaming Columns Using the AS Clause Since the default column title is the actual column-name, the AS phrase can be used to change the column-name which flows on to become the new column title. Alternative Solution (Non-ANSI standard)

CREATE TABLE monthly_sal_401 AS (SELECT employee_number AS emp

,salary_amount/12 (NAMED monthly_salary) FROM employee WHERE department_number = 401)

WITH DATA; SHOW TABLE monthly_sal_401; CREATE SET TABLE PED1.monthly_sal_401 ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( emp INTEGER, monthly_salary DECIMAL(10,2))

PRIMARY INDEX ( emp );

Table Creation from Existing Tables Page 13-26

Page 521: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Renaming Columns Using the AS Clause

Use the AS clause to rename a column.Use the AS clause to rename a column.

CREATE TABLE monthly_sal_401 AS (SELECT employee_number AS emp

,salary_amount/12 AS monthly_salary

monthly_salary

FROM employee WHERE department_number = 401) WITH DATA;

CREATE SET TABLE PED1.monthly_sal_401 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL (

emp INTEGER, DECIMAL(10,2))

PRIMARY INDEX ( emp );

CREATE SET TABLE PED1.monthly_sal_401 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL (

emp INTEGER, DECIMAL(10,2))

PRIMARY INDEX ( emp );

SHOW TABLE monthly_sal_401;SHOW TABLE monthly_sal_401;

monthly_salary

Table Creation from Existing Tables Page 13-27

Page 522: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Adding Unique and Primary Key Constraints When adding UNIQUE or PRIMARY KEY constraints, it is necessary to also specify NOT NULL, regardless of whether or not the original column is defined as NOT NULL.

CREATE TABLE dept1 (deptno UNIQUE NOT NULL ,deptname PRIMARY KEY NOT NULL ,budget ,manager) AS department WITH NO DATA; SHOW TABLE dept1; CREATE SET TABLE PED1.dept1 ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( deptno SMALLINT NOT NULL, deptname CHAR(30) CHARACTER SET LATIN

NOT CASESPECIFIC NOT NULL, budget DECIMAL(10,2), manager INTEGER)

UNIQUE PRIMARY INDEX ( deptname ) UNIQUE INDEX ( deptno );

Table Creation from Existing Tables Page 13-28

Page 523: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Adding UNIQUE and PRIMARY KEY Constraints

When adding PRIMARY KEY or UNIQUE constraints, it is necessary to also specify the NOT NULL attribute

When adding PRIMARY KEY or UNIQUE constraints, it is necessary to also specify the NOT NULL attribute

CREATE TABLE dept1 (deptno UNIQUE NOT NULL

PRIMARY KEY NOT NULL,deptname,budget ,manager)

AS department WITH NO DATA;

CREATE SET TABLE PED1.dept1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( deptno SMALLINT NOT NULL, deptname CHAR(30) CHARACTER SET LATIN

NOT CASESPECIFIC NOT NULL, budget DECIMAL(10,2), manager INTEGER) UNIQUE PRIMARY INDEX ( deptname ) UNIQUE INDEX ( deptno );

CREATE SET TABLE PED1.dept1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( deptno SMALLINT NOT NULL, deptname CHAR(30) CHARACTER SET LATIN

NOT CASESPECIFIC NOT NULL, budget DECIMAL(10,2), manager INTEGER) UNIQUE PRIMARY INDEX ( deptname ) UNIQUE INDEX ( deptno );

SHOW TABLE dept1;SHOW TABLE dept1;

Table Creation from Existing Tables Page 13-29

Page 524: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Volatile and Temporary Tables Volatile and global temporary tables may be created using CREATE AS WITH NO DATA. Volatile tables may be created using CREATE WITH DATA. Global temporary tables may not be created using CREATE WITH DATA. Global temporary tables must be populated with a separate DML request. Example Create a global temporary table which copies the department table.

CREATE GLOBAL TEMPORARY TABLE dept1 AS (SELECT * FROM department) WITH NO DATA;

SHOW TABLE dept1; CREATE SET GLOBAL TEMPORARY TABLE PED.dept1 ,NO FALLBACK ,

LOG (department_number SMALLINT, department_name CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL, budget_amount DECIMAL(10,2), manager_employee_number INTEGER) PRIMARY INDEX ( department_number ) ON COMMIT DELETE ROWS;

Note: The default for temporary tables is ON COMMIT DELETE. This means that the first DML request to populate the table will be undone when the request is completed. This is the ANSI standard default for temporary tables. Prior to populating the tables, alter the table to change the default. Alter the temporary table to preserve rows.

ALTER TABLE dept1 ON COMMIT PRESERVE ROWS; Then, populate the table.

INSERT INTO dept1 SELECT * FROM department; Reminder: Temporary tables may not be populated using WITH DATA syntax because you are creating a template of the table, not the real table. Temporary tables must be populated via a separate request using INSERT or INSERT SELECT.

Table Creation from Existing Tables Page 13-30

Page 525: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Volatile and Temporary Tables

CREATE GLOBAL TEMPORARY TABLE dept1 AS (SELECT * FROM department) WITH NO DATA;CREATE GLOBAL TEMPORARY TABLE dept1 AS (SELECT * FROM department) WITH NO DATA;

Temporary table created without data.Temporary table created without data.

CREATE GLOBAL TEMPORARY TABLE dept1 AS (SELECT * FROM department) WITH DATA;CREATE GLOBAL TEMPORARY TABLE dept1 AS (SELECT * FROM department) WITH DATA;

Error - WITH DATA not permitted for temporary tables.Error - WITH DATA not permitted for temporary tables.

CREATE VOLATILE TABLE dept1 AS (SELECT * FROM department) WITH DATA;CREATE VOLATILE TABLE dept1 AS (SELECT * FROM department) WITH DATA;

Successful, however data is deleted after being created since default is ON COMMIT DELETE ROWS.

Successful, however data is deleted after being created since default is ON COMMIT DELETE ROWS.

CREATE VOLATILE TABLE dept1 AS (SELECT * FROM department) WITH NO DATA;CREATE VOLATILE TABLE dept1 AS (SELECT * FROM department) WITH NO DATA;

Volatile table is created without data.Volatile table is created without data.

INSERT INTO dept1 SELECT * FROM department; INSERT INTO dept1 SELECT * FROM department; Temporary tables must be populated separately.Temporary tables must be populated separately.

CREATE VOLATILE TABLE dept1 AS (SELECT * FROM department) WITH DATAON COMMIT PRESERVE ROWS;

CREATE VOLATILE TABLE dept1 AS (SELECT * FROM department) WITH DATAON COMMIT PRESERVE ROWS;

Volatile table is created with data.Volatile table is created with data.

Table Creation from Existing Tables Page 13-31

Page 526: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using DEFAULT Values

SELECT department_number, budget_amount FROM department; department_number budget_amount ----------------- ------------- 501 308000.00 301 465600.00 201 293800.00 600 ? 100 400000.00 402 308000.00 403 932000.00 302 226000.00 401 982300.00

Default values may be specified as part of a column definition. They may be specified when creating a table using either WITH DATA or WITH NO DATA. Example Create a table showing a department number and budget amount using CREATE WITH NO DATA. Budget amounts must be entered (NOT NULL) or are defaulted to zero (DEFAULT 0).

CREATE TABLE dept1 (deptno, budget NOT NULL DEFAULT 0) AS (SELECT department_number ,budget_amount FROM department) WITH NO DATA;

SHOW TABLE dept1; CREATE SET TABLE PED1.dept1 ,NO FALLBACK ,

NO BEFORE JOURNAL, NO AFTER JOURNAL

( deptno SMALLINT, budget DECIMAL(10,2) NOT NULL DEFAULT 0.00 )

PRIMARY INDEX ( deptno );

Table Creation from Existing Tables Page 13-32

Page 527: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using DEFAULT Values

Default values may be specified as part of a column definition when using CREATE … AS.Default values may be specified as part of a column definition when using CREATE … AS.

CREATE SET TABLE sql00.dept1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( deptno SMALLINT, budget DECIMAL(10,2) NOT NULL DEFAULT 0.00 )

PRIMARY INDEX ( deptno );

CREATE SET TABLE sql00.dept1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( deptno SMALLINT, budget DECIMAL(10,2) NOT NULL DEFAULT 0.00 )

PRIMARY INDEX ( deptno );

SHOW TABLE dept1; SHOW TABLE dept1;

Default values and null attributes may be specified either for WITH DATA or WITH NO DATA table creation.Default values and null attributes may be specified either for WITH DATA or WITH NO DATA table creation.

CREATE TABLE dept1(deptno,budget NOT NULL DEFAULT 0) AS

(SELECT department_number,budget_amount

FROM department) WITH NO DATA;

CREATE TABLE dept1(deptno,budget NOT NULL DEFAULT 0) AS

(SELECT department_number,budget_amount

FROM department) WITH NO DATA;

Table Creation from Existing Tables Page 13-33

Page 528: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Populating NOT NULL Columns (Table definition repeated for convenience)

CREATE SET TABLE sql00.dept1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL

(deptno SMALLINT, budget DECIMAL(10,2) NOT NULL DEFAULT 0.00 )

PRIMARY INDEX ( deptno ); The following table data in the department table is repeated for convenience. Note the existence of a null in the budget column.

SELECT department_number, budget_amount FROM department; department_number budget_amount --------------------------- --------------------- 501 308000.00 301 465600.00 201 293800.00 600 ? 100 400000.00 402 308000.00 403 932000.00 302 226000.00 401 982300.00

Special attention may be needed when attempting to populate columns defined as NOT NULL.

INSERT INTO dept1 SELECT department_number, budget_amount FROM department; *** Failure 3604 Cannot place a null value in a NOT NULL field.

This problem may be corrected by use of the COALESCE function, which can be used to convert a null budget amount to a zero.

INSERT INTO dept1 SELECT department_number, COALESCE(budget_amount,0) FROM department;

Table Creation from Existing Tables Page 13-34

Page 529: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Populating NOT NULL Columns

Population of NOT NULL columns may require special attentionPopulation of NOT NULL columns may require special attention

INSERT INTO dept1 SELECT department_number

, COALESCE(budget_amount , 0)FROM department;ZZZZ

deptno budget 501 308000.00 301 465600.00 201 293800.00 600 .00 100 400000.00 402 308000.00 403 932000.00 302 226000.00 401 982300.00

deptno budget 501 308000.00 301 465600.00 201 293800.00 600 .00 100 400000.00 402 308000.00 403 932000.00 302 226000.00 401 982300.00

*** Failure 3604 Cannot place a null value in a NOT NULL field.

*** Failure 3604 Cannot place a null value in a NOT NULL field.

SELECT * FROM dept1;ZSELECT * FROM dept1;Z

Problem:

Solution:

INSERT INTO dept1 SELECT department_number

,budget_amount FROM department;

INSERT INTO dept1 SELECT department_number

,budget_amount FROM department;

Table Creation from Existing Tables Page 13-35

Page 530: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Populating DEFAULT Columns Accomplish the same task using CREATE AS to both create and populate the table. Convert the budget amount to a DEC(10,2) data type.

CREATE TABLE dept1 (deptno, budget NOT NULL DEFAULT 0) AS (SELECT department_number ,budget_amount (DEC(10,2))) FROM department) WITH DATA; *** Failure 3604 Cannot place a null value in a NOT NULL field.

This failure occurs because the attempted insert of the null budget amount causes a violation of the NOT NULL attribute. The default value of zero is only applicable to future rows, thus is does not override the null. To correct this problem, add the COALESCE function.

CREATE TABLE dept1 (deptno, budget NOT NULL DEFAULT 0) AS (SELECT department_number ,COALESCE(budget_amount,0) (DEC(10,2)) FROM department) WITH DATA; SHOW TABLE dept1;

CREATE SET TABLE PED1.dept1 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( deptno SMALLINT, budget DECIMAL(10,2) NOT NULL DEFAULT 0.00 ) PRIMARY INDEX ( deptno );

Table Creation from Existing Tables Page 13-36

Page 531: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Populating DEFAULT Columns

Population of NOT NULL/DEFAULT columns may require special attentionPopulation of NOT NULL/DEFAULT columns may require special attention

CREATE TABLE dept1 (deptno,budget NOT NULL DEFAULT 0)

AS (SELECT department_number,COALESCE(budget_amount,0)

DEFAULT 0

(DEC(10,2))FROM department) WITH DATA;

CREATE TABLE dept1 (deptno,budget NOT NULL )

AS (SELECT department_number,budget_amount (DEC(10,2)))

FROM department) WITH DATA;

deptno budget501 308000.00 301 465600.00 201 293800.00 600 .00 100 400000.00 402 308000.00 : :

deptno budget501 308000.00 301 465600.00 201 293800.00 600 .00 100 400000.00 402 308000.00 : :

*** Failure 3604 Cannot place a null value in a NOT NULL field.

*** Failure 3604 Cannot place a null value in a NOT NULL field.

SELECT * FROM dept1; SELECT * FROM dept1;

Problem:

Solution:

Table Creation from Existing Tables Page 13-37

Page 532: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

Table Creation from Existing Tables Page 13-38

Page 533: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

1.) Create and populate a table based on the 'employee' table containing rows for the employees in department 403 only, and consisting of the following three columns.

• Employee number - renamed as 'empno', converted to a SMALLINT and with a UNIQUE constraint • Employee last name - renamed as 'lastnm', converted to a CHAR(10) • Employee weekly salary - renamed as 'weekly_sal'

After successful table creation, SHOW the table and SELECT all rows, ordering by employee number.

2.) Create and populate a table which shows the highest salary in each department. Rename the department number as 'dept' and the maximum salary as 'maxsal'. Also, specify a default stacked title of 'Max//Sal'. This may be accomplished with a single statement which selects from the 'employee' table. SHOW the table and SELECT all rows to verify your results.

3.) Create and populate a table called 'emp_age' which contains the following columns:

Employee number (named 'empno')Department name (named 'deptname')

Employee's age named 'age' and computed as '(DATE - birthdate) YEAR'

To create this table, a join between the 'employee' and 'department' table will be needed.

Upon successful creation, SHOW the table and SELECT all employees whose age is greater than 50.

Order the results by employee number.

Table Creation from Existing Tables Page 13-39

Page 534: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Table Creation from Existing Tables Page 13-40

Page 535: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 14

Module 14Extended Grouping Functions

After completing this module, you should be able to:

• Identify appropriate applications of the ROLLUP function.

• Identify appropriate applications of the CUBE function.

• Identify appropriate applications of the GROUPING SETS function.

Extended Grouping Functions Page 14-1

Page 536: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extended Grouping Functions Page 14-2

Page 537: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Extended Grouping Functions Page 14-3

Page 538: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extended Grouping Functions Extended Grouping Functions expand the ability to perform in-database analysis, eliminating the need to extract and move data elsewhere for processing. This feature includes three analytical functions: ROLLUP, which allows the creation of a hierarchy of results along a single dimension such as time (e.g. year, month, week) or geography (country, state, province). CUBE, which supports multidimensional processing such as product by time by geography. GROUPING SETS, which allows aggregations in single or multiple dimensions without requiring a full ROLLUP or CUBE to be processed. The GROUP BY clause in SQL has been extended to permit CUBE, ROLLUP and GROUPING SET options. An additional GROUPING function is used in conjunction with any of the GROUP BY options to identify when a group represents a grand total versus a null set. This function will be demonstrated later in this module. Each of these functions conforms to ANSI standards.

Extended Grouping Functions Page 14-4

Page 539: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Extended Grouping Functions

Extended Grouping Functions expand the ability to perform in-database analysis, eliminating the need to extract and move data elsewhere for processing.

This feature includes three analytical functions:

ROLLUPCreates a hierarchy of results along a single dimension such as time (e.g. year, month, week) or geography (country, state, province).

CUBESupports multidimensional processing such as product by time by geography.

GROUPING SETSAllows aggregations in single or multiple dimensions without requiring a full ROLLUP or CUBE to be processed.

All three are extensions of the GROUP BY clause.

The GROUPING function is used in conjunction with any of these functions to identify when a group represents a grand total versus a null set.

Each of these functions conforms to ANSI standards.Each of these functions conforms to ANSI standards.

Extended Grouping Functions Page 14-5

Page 540: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Aggregation Review In the following example, we examine the total salaries of employees using the single level of department. We can use the approach seen on the right page, or alternatively we can use the Sum Window function, seen in a previous module, to do this. SUM Window Function Approach: SELECT DISTINCT department_number ,SUM(salary_amount) OVER (PARTITION BY department_number) FROM employee WHERE department_number < 402; department_number Group Sum(salary_amount) ------------------------ --------------------------------- 100 100000.00 201 73450.00 301 116400.00 302 56500.00 401 245575.00 NOTE: DISTINCT is needed to prevent duplicate rows of output, since a row is generated for each employee.

Extended Grouping Functions Page 14-6

Page 541: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Aggregation Review

Using the SUM function, produce a total of salaries by department for department numbers less than 402.

SELECT department_number,SUM(salary_amount)

FROM employee WHERE department_number < 402GROUP BY 1ORDER BY 1;

SELECT department_number,SUM(salary_amount)

FROM employee WHERE department_number < 402GROUP BY 1ORDER BY 1;

department_number Sum(salary_amount)100 100000.00201 73450.00301 116400.00302 56500.00401 245575.00

department_number Sum(salary_amount)100 100000.00201 73450.00301 116400.00302 56500.00401 245575.00

Extended Grouping Functions Page 14-7

Page 542: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The ROLLUP Function: One Level The ROLLUP function is used when aggregation is desired across all levels of a hierarchy within a single dimension. For example, within the dimension of geography, you might have three levels of hierarchy - city, county and state. You may wish to produce a sum of revenue amounts across all three levels. In other words, you wish to aggregate at the lowest level (city) and 'rollup' those aggregations to the next level (county) and finally rollup to the top level (state). You might also wish to provide a grand total across all three levels. All of these are easily done with the ROLLUP feature. Note the difference in the result set compared to the Sum Window example. There is an additional row with a '?' department number. This row represents the rollup of all department salaries which is the top (and only) level of the hierarchy in this case. The '?' does not represent a null, rather it represents the 'total' of all department salaries.

Extended Grouping Functions Page 14-8

Page 543: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The ROLLUP Function: One Level

The ROLLUP function is used when aggregation is desired across all levels of a hierarchy within a single dimension.

We may produce a result similar to the SUM Window using the ROLLUP function. In this case the rollup is very simple as it involves only the department level or, a single level of hierarchy.

Example

Using the ROLLUP function, produce salary totals by department.

SELECT department_number,SUM(salary_amount)FROM employeeWHERE department_number < 402GROUP BY ROLLUP (department_number)ORDER BY 1;

dept_num Sum(salary_amount)? 591925.00100 100000.00201 73450.00301 116400.00302 56500.00401 245575.00

dept_num Sum(salary_amount)? 591925.00100 100000.00201 73450.00301 116400.00302 56500.00401 245575.00

The '?' does not represent a null, it represents the 'total' of all department salaries.

Extended Grouping Functions Page 14-9

Page 544: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ROLLUP Function: Two Levels We will now add another level to this example. We will add the level of department manager. Since a manager can have multiple departments, the hierarchy has manager at the top with departments subordinate to it. This type of multi-level hierarchy represents a more traditional use of the ROLLUP function. In fact, there are three separate aggregations taking place in this query:

• Totals for each dept/manager combo (black). This is represented as the set '(manager,department )' and is the bottom of the hierarchy.

• Totals for each manager across all departments (red). This is represented as the set '(manager )' which is the middle of the hierarchy.

• A total for all depts across all managers (blue). This is represented as the set '( )' and is the top of the hierarchy.

• (We will see the set '( )' notation again later when we look at the GROUPING SETS function.)

• The order of the output data is controlled by the ORDER BY clause, otherwise it is randomized.

Extended Grouping Functions Page 14-10

Page 545: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ROLLUP Function: Two Levels

Use ROLLUP to produce a hierarchy of total salaries by department within manager.

• Totals for each dept/manager combo (black).

• Totals for each manager across all departments (red or ).

• A total for all depts across all managers (blue or ).

• Totals for each dept/manager combo (black).

• A total for all depts across all managers (blue or ).

SELECT manager_employee_number AS Mgr,department_number AS Dept,SUM(salary_amount) AS SumSal

FROM employeeWHERE department_number < 402

ORDER BY 1,2;

• Totals for each manager across all departments (red or ).

GROUP BY ROLLUP (manager_employee_number,department_number)

SELECT manager_employee_number AS Mgr,department_number AS Dept,SUM(salary_amount) AS SumSal

FROM employeeWHERE department_number < 402

ORDER BY 1,2;(all mgrs, all depts, all emps)

(mgr 801, dept 100, 1 emp)(mgr 801, dept 201, 1 emp)(mgr 801, dept 301, 1 emp)(mgr 801, dept 302, 1 emp)(mgr 801, dept 401, 1 emp)

(mgr 1003, dept 401, 6 emps)

(mgr 1019, dept 301, 2 emps)

(mgr 1025, dept 201, 1 emp)

Mgr

GROUP BY ROLLUP (manager_employee_number,department_number)

(mgr 801, all depts, 5 emps)

(mgr 1003, all depts, 6 emps)

(mgr 1019, all depts, 2 emps)

(mgr 1025, all depts, 1 emp)

Dept SumSal? ? 591925.00

801 100 100000.00801 201 34700.00801 301 57700.00801 302 56500.00801 401 37850.00

1003 401 207725.00

1019 301 58700.00

1025 201 38750.00

801 ? 286750.00

1003 ? 207725.00

1019 ? 58700.00

1025 ? 38750.00

Mgr Dept SumSal? ? 591925.00

801 100 100000.00801 201 34700.00801 301 57700.00801 302 56500.00801 401 37850.00

1003 401 207725.00

1019 301 58700.00

1025 201 38750.00

801 ? 286750.00

1003 ? 207725.00

1019 ? 58700.00

1025 ? 38750.00

Extended Grouping Functions Page 14-11

Page 546: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Reversing the ROLLUP Sequence We can also consider doing the roll up in the other direction as well. If we are interested in how managers roll up into departments, we can simply invert the sequence in the ROLLUP clause. Again, there are three separate aggregations taking place in the preceding query. Totals for each dept/manager combo (black). This is represented as the set '(manager,department )' and is the bottom of the hierarchy. This part of the report has not changed from the previous example. Totals for each department (red). This is represented as the set '(department )' which is the middle of the hierarchy. These rows are new to this report. A total for all depts across all departments (blue). This is represented as the set '( )' and is the top of the hierarchy. This row of output remains unchanged.

Extended Grouping Functions Page 14-12

Page 547: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Reversing the ROLLUP SequenceUse ROLLUP to produce a hierarchy of total salaries by manager within department.SELECT department_number AS Dept

,manager_employee_number AS Mgr,SUM(salary_amount) AS SumSal

FROM employeeWHERE department_number < 402GROUP BY ROLLUP (department_number

manager_employee_number)ORDER BY 1,2;

SELECT department_number AS Dept,manager_employee_number AS Mgr,SUM(salary_amount) AS SumSal

FROM employeeWHERE department_number < 402

ORDER BY 1,2;

Dept

GROUP BY ROLLUP (department_numbermanager_employee_number)

Mgr SumSal? ? 591925.00

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

Dept Mgr SumSal? ? 591925.00

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

(all depts all managers)

(dept 100,manager 801,all emps,1)

(dept 201,manager 801,all emps,1)(dept 201,manager 1025,all emps,1)

(dept 301,manager 801,all emps,1)(dept 301,manager 1019,all emps,2)

(dept 302, manager 801,all emps, 1)

(dept 401, manager 801, all emps, 1)(dept 401,manager 1003, all emps,6)

• Totals for each dept/manager combo (black).

• A total for all depts across all managers (blue or ).

100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

(dept 100 all managers)

(dept 201, all managers)

(dept 301, all managers)

(dept 302, all managers,all emps, 1)

(dept 401 all managers, all emps, 2)

• Totals for each manager across all departments (red or ).

• Totals for each dept/manager combo (black).

• A total for all depts across all managers (blue or ).

• Totals for each manager across all departments (red or ).

Extended Grouping Functions Page 14-13

Page 548: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ROLLUP with NULL As previously indicated, the row with the '?' represents the grand total of all salaries across all departments. But what if there was a department number with a null? How would we distinguish between it and a 'total' row? Add an employee with a null department and a null job code:

INSERT INTO employee VALUES (1050,801,NULL,NULL,'LaCoste','Jason',780415,480816,60000.00);

Now, let's rerun the query.

SELECT department_number ,SUM(salary_amount) FROM employee GROUP BY ROLLUP (department_number) ORDER BY 1; department_number Sum(salary_amount) -------------------------- ----------------------------- ? 1162050.00 ? 60000.00 100 100000.00 201 73450.00 301 116400.00 302 56500.00 401 245575.00 402 77000.00 403 233000.00 501 200125.00

While it may seem obvious that the row with the larger sum will represent the 'total' row, a function exists which permits the report to be more easily read.

Extended Grouping Functions Page 14-14

Page 549: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

ROLLUP with NULL

The '?' represents a total when output from a ROLLUP function.

But what if there was a department number with a null?

How would we distinguish between it and a 'total' row?

Add an employee with a null department and a null job code:

Now, let's rerun the query.

While it may seem obvious that the ‘?’ row with the larger sum will represent the 'total' row, a function exists which permits the report to be more easily read.

While it may seem obvious that the ‘?’ row with the larger sum will represent the 'total' row, a function exists which permits the report to be more easily read.

dept_num Sum(salary_amount)? 1162050.00? 60000.00

100 100000.00201 73450.00301 116400.00302 56500.00401 245575.00402 77000.00403 233000.00501 200125.00

dept_num Sum(salary_amount)? 1162050.00? 60000.00

100 100000.00201 73450.00301 116400.00302 56500.00401 245575.00402 77000.00403 233000.00501 200125.00

SELECT department_number,SUM(salary_amount)FROM employeeGROUP BY ROLLUP (department_number)ORDER BY 1;

SELECT department_number,SUM(salary_amount)FROM employee

ORDER BY 1;

INSERT INTO employee VALUES(1050,801,NULL,NULL,'LaCoste','Jason',780415,480816,60000.00);INSERT INTO employee VALUES(1050,801,NULL,NULL,'LaCoste','Jason',780415,480816,60000.00);

GROUP BY ROLLUP (department_number)

Extended Grouping Functions Page 14-15

Page 550: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The GROUPING Function The GROUPING function is applied to a column and determines whether the value being returned represents a grouping aggregate result or simply a data value (including nulls). When a grouping aggregate is being returned, the value returned by the function is one, otherwise it is zero. GROUPING is ANSI SQL-99-compliant. A null in the result row of a query containing a ROLLUP can mean one of two things:

• The actual data for the column is null. • The null represents an aggregation for all values for this column.

Use GROUPING to distinguish between rows with nulls in the actual data from rows generated from grouping aggregate results. GROUPING returns either a one or a zero depending on the circumstances previously shown:

• The actual data for the column is null - (in this case, a zero is returned by GROUPING). • The null represents an aggregation for all values for this column - (in this case, a one is

returned). Now, let's revisit the previous query again. This time, we will add the GROUPING function to distinguish when an actual null is being returned.

Extended Grouping Functions Page 14-16

Page 551: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The GROUPING Function

Using the ROLLUP function, produce a total of all salaries by department.

SELECT CASE GROUPING (department_number)WHEN 1 THEN 'Total'

ELSE department_number END

AS Deptno ,SUM(salary_amount)FROM employeeGROUP BY ROLLUP (department_number)ORDER BY 1;

SELECT CASE GROUPING

AS Deptno ,SUM(salary_amount)FROM employeeGROUP BY ROLLUP (department_number)ORDER BY 1;

Deptno(department_number)WHEN 1 THEN 'Total'

ELSE department_number END

Sum(salary_amount)? 60000.00

100 100000.00201 73450.00301 116400.00302 56500.00401 245575.00402 77000.00403 233000.00501 200125.00

Total 1162050.00

Deptno Sum(salary_amount)? 60000.00

100 100000.00201 73450.00301 116400.00302 56500.00401 245575.00402 77000.00403 233000.00501 200125.00

Total 1162050.00

NOTE: This result set makes it clear which is a total and which is a null. The AS clause is used to prevent a 'CASE Expression' title on the report heading for the department number column.

NOTE: This result set makes it clear which is a total and which is a null. The AS clause is used to prevent a 'CASE Expression' title on the report heading for the department number column.

GROUPING distinguishes rows with nulls from rows with aggregates. GROUPING returns:

• A zero if the actual data for the column is null.• A one if the null represents an aggregation for all values for this column.

Extended Grouping Functions Page 14-17

Page 552: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

GROUPING, CASE and COALESCE We will now consider a different relationship, namely the relationship between departments and job codes. This is a classic one-to-many relationship which is easily understood as a hierarchy. Each department may have multiple job codes, but a job code cannot exist in more than one department. Using the ROLLUP function, we can produce a total of all salaries by job code within a department. Given that we have a null department number and a null job code, it is very difficult to read this report and know which are the nulls and which are aggregate groupings. We can now see the grouping aggregates more clearly. We can also add one more bit of refinement to this report using the COALESCE function, to eliminate all '?'s from the report. Note that the coalesced results appear last in the sort sequence due to being alpha character strings instead of numerics.

Extended Grouping Functions Page 14-18

Page 553: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

GROUPING, CASE and COALESCEUse ROLLUP to produce a total of all salaries by job code within a department.

Given a null department number and a null job code, it can be very difficult to read this report and know which are the nulls and which are aggregate groupings.

Use the COALESCE function, to eliminate all '?'s from the report.

SELECT CASE GROUPING (department_number)WHEN 1

THEN 'Total Depts'ELSE (COALESCE (department_number,'Null Dept')

(COALESCE (job_code,'Null Job Code')

) \END

AS Deptno,CASE GROUPING (job_code)

WHEN 1 THEN 'All Job Codes' ELSE ) END

AS job_code,SUM(salary_amount)

FROM employeeGROUP BY ROLLUP (department_number,job_code)ORDER BY 1,2;

SELECT CASE GROUPING (department_number)WHEN 1

THEN 'Total Depts'ELSE ) \END

AS Deptno,CASE GROUPING (job_code)

WHEN 1 THEN 'All Job Codes' ELSE ) END

AS job_code,SUM(salary_amount)

FROM employeeGROUP BY ROLLUP (department_number,job_code)ORDER BY 1,2;

Deptno

(COALESCE (department_number,'Null Dept')

(COALESCE (job_code,'Null Job Code')

job_code Sum(sal_amt) 403 431100 31200.00403 432101 201800.00403 All Job Codes 233000.00501 511100 66000.00501 512101 134125.00501 All Job Codes 00125.00

Null Dept All Job Codes 60000.00Null Dept Null Job Code 60000.00

Total Depts All Job Codes 1162050.00

Deptno job_code Sum(sal_amt) 403 431100 31200.00403 432101 201800.00403 All Job Codes 233000.00501 511100 66000.00501 512101 134125.00501 All Job Codes 00125.00

Null Dept All Job Codes 60000.00Null Dept Null Job Code 60000.00

Total Depts All Job Codes 1162050.00

(Partial Output)

Extended Grouping Functions Page 14-19

Page 554: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CUBE/ROLLUP Comparison The CUBE feature is used when multiple dimensions of some entity require aggregation across all dimensions and across all combinations of the dimensions. For example, you may have a customer revenue table and you may be interested in the dimensions of age range, income level and education level. You may wish show revenue amounts based on each of these dimensions, but also in all combinations of these dimensions as well. In other words you might wish to aggregate by all permutations of these three dimensions - i.e., (age, income), (age, education), (income, education), (age), (income), (education), (age,income,education). You might also wish to provide a grand total across all three dimensions. All of these are easily done with the CUBE feature. You will recall, the output for the ROLLUP function breaks down as follows:

GROUP BY ROLLUP (department_number,manager_employee_number) (manager, department) - each dept/manager combo (department) - department totals( ) - grand total

The CUBE function can be thought of as a superset of the ROLLUP functions. It returns the same result set as ROLLUP, but it also provides additional information. Instead producing a 'hierarchy' of results, CUBE looks at all permutations of the specified dimensions. In this case, all permutations of these two dimensions (manager and department) would be represented by the following four grouping sets:

• (manager, department) - each dept/manager combo • (department) - department totals • (manager) - manager totals • ( ) - grand total

Thus, the (manager) set is added to the ROLLUP output.

Extended Grouping Functions Page 14-20

Page 555: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

CUBE/ROLLUP Comparison

In this case, all permutations of these two dimensions (manager and department) would be represented by the following four grouping sets:

• (manager, department) - each dept/manager combo• (department) -

• (department) -

department totals• (manager) - manager totals• ( ) - grand total

Thus, the (manager) set is added to the ROLLUP output.

The output for the ROLLUP function consists of:• (manager, department) - each dept/manager combo

department totals• ( ) - grand total

Think of the CUBE function as a superset of the ROLLUP functions. It returns the same result set as ROLLUP, and provides additional information.

Instead producing a 'hierarchy' of results, CUBE looks at all permutations of the specified dimensions.

Think of the CUBE function as a superset of the ROLLUP functions. It returns the same result set as ROLLUP, and provides additional information.

Instead producing a 'hierarchy' of results, CUBE looks at all permutations of the specified dimensions.

GROUP BY ROLLUP (department_number,manager_employee_numberGROUP BY ROLLUP (department_number,manager_employee_number

GROUP BY CUBE (department_number,manager_employee_number)GROUP BY CUBE (department_number,manager_employee_number)

Extended Grouping Functions Page 14-21

Page 556: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The CUBE Function Notice that four additional rows (green) are included in the output,compared to the ROLLUP output. These four rows represent the totals by manager. This report shows all possible permutations of the two specified dimensions.

Extended Grouping Functions Page 14-22

Page 557: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The CUBE Function

ExampleProduce a 'cube' report of salaries across the department and manager dimensions.

Deptno Mngr SumSal? ? 591925.00? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

SELECT department_number AS Deptno ,manager_employee_number AS Mngr,SUM(salary_amount) AS SumSal

FROM employeeWHERE department_number < 402

ORDER BY 1,2;GROUP BY CUBE (department_number ,manager_employee_number)

Deptno Mngr SumSal? ? 591925.00? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

Notice that four additional rows are included in the output. 100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

SELECT department_number AS Deptno ,manager_employee_number AS Mngr,SUM(salary_amount) AS SumSal

FROM employeeWHERE department_number < 402

ORDER BY 1,2;

(all depts, all managers)(all depts, one manager)

(one dept, all managers)(one dept, one manager)(one dept, all managers)

GROUP BY CUBE (department_number ,manager_employee_number)

Extended Grouping Functions Page 14-23

Page 558: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Reversing CUBE Parameters What happens when we flip the sequence of the CUBE parameters? Note, there is no difference. The output is exactly the same. Because the CUBE function looks at all permutations of the specified parameters, order of the arguments doesn't matter - unlike ROLLUP where it does matter.

Extended Grouping Functions Page 14-24

Page 559: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Reversing CUBE Parameters

SELECT department_number AS Deptno ,manager_employee_number AS Mngr,SUM(salary_amount) AS SumSal

FROM employee WHERE department_number < 402GROUP BY CUBE (manager_employee_number ,department_number)ORDER BY 1,2;

SELECT department_number AS Deptno ,manager_employee_number AS Mngr,SUM(salary_amount) AS SumSal

FROM employee WHERE department_number < 402

ORDER BY 1,2;

What happens when we flip the sequence of CUBE parameters?

Deptno

GROUP BY CUBE (manager_employee_number ,department_number)

Mngr SumSal? ? 591925.00? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

Deptno Mngr SumSal(all depts, all managers)(all depts, one manager)

(one dept, all managers)(one dept, one manager)(one dept, all managers)

? ? 591925.00? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

• There is no difference. The output is exactly the same.

• Because the CUBE function looks at all permutations of the specified parameters, order of the arguments doesn't matter.

• This is not the case with ROLLUP, where it does matter.

100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

Extended Grouping Functions Page 14-25

Page 560: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Adding GROUPING Function to CUBE As seen previously, we can use the GROUPING and COALESCE functions to make the report more readable.

Extended Grouping Functions Page 14-26

Page 561: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Adding GROUPING Function to CUBE

SELECT CASE GROUPING (department_number)WHEN 1 THEN 'All Depts'ELSE (COALESCE (department_number,'Null Dept') )END

CASE GROUPING (manager_employee_number)WHEN 1 THEN 'All Managers'ELSE manager_employee_number END

AS Deptno,

AS Mngr,SUM(salary_amount) AS SumSal

FROM employeeWHERE department_number < 402GROUP BY CUBE (manager_employee_number,department_number)ORDER BY 1,2;

SELECT

AS Deptno,

AS Mngr,SUM(salary_amount) AS SumSal

FROM employeeWHERE department_number < 402GROUP BY CUBE (manager_employee_number,department_number)ORDER BY 1,2;

GROUPING and COALESCE functions can make a CUBE report more readable.

Make a 'cube' report of employee salaries across department and manager dimensions.

Deptno CASE GROUPING (department_number)WHEN 1 THEN 'All Depts'ELSE (COALESCE (department_number,'Null Dept') )END

CASE GROUPING (manager_employee_number)WHEN 1 THEN 'All Managers'ELSE manager_employee_number END

Mngr SumSal-100 801 100000.00100 All Managers 100000.00201 801 34700.00201 1025 38750.00201 All Managers 73450.00301 801 57700.00301 1019 58700.00301 All Managers 116400.00302 801 56500.00302 All Managers 56500.00401 801 37850.00401 1003 207725.00401 All Managers 245575.00All Depts 801 286750.00All Depts 1003 207725.00All Depts 1019 58700.00All Depts 1025 38750.00All Depts All Managers 591925.00

Deptno Mngr SumSal-100 801 100000.00100 All Managers 100000.00201 801 34700.00201 1025 38750.00201 All Managers 73450.00301 801 57700.00301 1019 58700.00301 All Managers 116400.00302 801 56500.00302 All Managers 56500.00401 801 37850.00401 1003 207725.00401 All Managers 245575.00All Depts 801 286750.00All Depts 1003 207725.00All Depts 1019 58700.00All Depts 1025 38750.00All Depts All Managers 591925.00

Extended Grouping Functions Page 14-27

Page 562: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Multi-Dimensional Cubes As more dimensions are added to the cube, more permutations and thus more rows will result. In the example seen here, a third dimension (job_code) is added to the cube. The following eight grouping sets are thus generated:

(department,manager, job_code) Each unique combo of dept/mgr/job (department, manager) Each unique combo of dept/mgr (department, job_code) Each unique combo of dept/job (manager, job_code) Each unique combo of mgr/job (department) All depts (manager) All mgrs (job_code) All jobs ( ) Grand Total

We can infer from this, the rule of CUBE permutations: If n is the number of dimensions in a cube, then 2**n represents the number of permutations generated: 2 dimensions - 4 permutations 3 dimensions - 8 permutations 4 dimensions - 16 permutations Example Produce a 'cube' report showing employee salaries across the three dimensions of department, manager and job code for department numbers less than 302.

SELECT CASE GROUPING (department_number) WHEN 1 THEN 'All Depts' ELSE (COALESCE (department_number,'Null Dept') ) END AS Deptno ,CASE GROUPING (manager_employee_number) WHEN 1 THEN 'All Managers' ELSE manager_employee_number END AS Mngr ,CASE GROUPING (job_code) WHEN 1 THEN 'All Job Codes' ELSE (COALESCE (job_code,'Null Job Code') ) END AS Job ,SUM(salary_amount) AS SumSal FROM employee WHERE department_number < 302 GROUP BY CUBE (manager_employee_number,department_number, job_code) ORDER BY 1,2;

As can be seen from the example, adding dimensions to a cube can generate many additional rows and can make for a complex report.

Extended Grouping Functions Page 14-28

Page 563: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Multi-Dimensional Cube

If n is the number of dimensions in a cube, then 2**n is the number of permutations generated:

2 dimensions - 4 permutations3 dimensions - 8 permutations4 dimensions - 16 permutations

Produce a 'cube' report showing employee salaries across the three dimensions of department, manager and job code for department numbers less than 302.

Deptno Mngr Job SumSal100 801 111100 100000.00100 801 All Job Codes 100000.00100 All Managers All Job Codes 100000.00100 All Managers 111100 100000.00

201 1025 222101 38750.00201 All Managers 211100 34700.00

201 All Managers 222101 38750.00

301 1019 312102 29250.00301 All Managers 312102 29250.00301 All Managers 311100 57700.00

301 All Managers 312101 29450.00

All Depts All Managers All Job Codes 289850.00

201 801 211100 34700.00201 801 All Job Codes 34700.00201 1025 All Job Codes 38750.00

201 All Managers All Job Codes 73450.00

301 801 All Job Codes 57700.00301 801 311100 57700.00301 1019 All Job Codes 58700.00301 1019 312101 29450.00

301 All Managers All Job Codes 116400.00

All Depts 801 All Job Codes 192400.00All Depts 801 111100 100000.00All Depts 801 211100 34700.00All Depts 801 311100 57700.00All Depts 1019 312101 29450.00All Depts 1019 All Job Codes 58700.00All Depts 1019 312102 29250.00All Depts 1025 All Job Codes 38750.00All Depts 1025 222101 38750.00

All Depts All Managers 311100 57700.00All Depts All Managers 211100 34700.00All Depts All Managers 111100 100000.00All Depts All Managers 312101 29450.00All Depts All Managers 312102 29250.00All Depts All Managers 222101 38750.00

Deptno Mngr Job SumSal100 801 111100 100000.00100 801 All Job Codes 100000.00100 All Managers All Job Codes 100000.00

201 801 211100 34700.00201 801 All Job Codes 34700.00201 1025 All Job Codes 38750.00201 1025 222101 38750.00

201 All Managers All Job Codes 73450.00

301 801 All Job Codes 57700.00301 801 311100 57700.00301 1019 All Job Codes 58700.00301 1019 312101 29450.00301 1019 312102 29250.00

301 All Managers All Job Codes 116400.00

All Depts 801 All Job Codes 192400.00All Depts 801 111100 100000.00All Depts 801 211100 34700.00All Depts 801 311100 57700.00All Depts 1019 312101 29450.00All Depts 1019 All Job Codes 58700.00All Depts 1019 312102 29250.00All Depts 1025 All Job Codes 38750.00All Depts 1025 222101 38750.00

All Depts All Managers 311100 57700.00All Depts All Managers 211100 34700.00All Depts All Managers 111100 100000.00All Depts All Managers 312101 29450.00All Depts All Managers 312102 29250.00All Depts All Managers 222101 38750.00

Output

100 All Managers 111100 100000.00

201 All Managers 211100 34700.00

201 All Managers 222101 38750.00

301 All Managers 312102 29250.00301 All Managers 311100 57700.00

301 All Managers 312101 29450.00

All Depts All Managers All Job Codes 289850.00

Extended Grouping Functions Page 14-29

Page 564: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

GROUPING SETS/ROLLUP Comparison The GROUPING SETS function permits you to customize your definition of groups for which 'group aggregations' should occur. When either a single or multi-dimensional aggregation is desired and the CUBE or ROLLUP features do not provide the exact combinations that you require, the GROUPING SETS functiton can be used to customize the desired result. With GROUPING SETS, each group or group combo must be identified explicitly, unlike ROLLUP and CUBE which form the grouping sets for you. With the GROUPING SETS feature, we can explicitly define the groups we desire to be represented in the output. First, let's briefly revisit the ROLLUP function for comparison purposes. You will recall, the output for ROLLUP breaks down as follows:

• Totals for each dept/manager combo. This is represented as the set '(manager,department )' and is the bottom of the hierarchy.

• Totals for each department. This is represented as the set '(department )' which is the middle of the hierarchy.

• A total across all departments. This is represented as the set '( )' and is the top of the hierarchy.

Extended Grouping Functions Page 14-30

Page 565: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

GROUPING SETS/ROLLUP Comparison

The GROUPING SETS function permits you to customize your definition of which 'group aggregations' should occur.

With GROUPING SETS, each group or group combo must be identifiedexplicitly, unlike ROLLUP and CUBE which form the grouping sets for you.

First, let's briefly revisit the ROLLUP function for comparison purposes.

You will recall, the output for

ROLLUP(department_number, manager_employee_number)

Breaks down as follows: • Totals for each dept/manager combo.

This is represented as the set '(manager,department )' and is the bottom of the hierarchy.

• Totals for each department.This is represented as the set '(department )' which is the middle of the hierarchy.

• A total across all departments (or grand total).This is represented as the set '( )' and is the top of the hierarchy.

Extended Grouping Functions Page 14-31

Page 566: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The GROUPING SETS Function With the GROUPING SETS feature, we can explicitly define the groups we desire to be represented in the output. In this example, the two grouping sets are (department) and (manager), each of which is explicitly specified in the GROUPING SETS clause.

Extended Grouping Functions Page 14-32

Page 567: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The GROUPING SETS Function

Example: Use GROUPING SETS to produce a report showing employee salaries aggregated by manager and also by department for department numbers less than 402.

SELECT department_number AS deptnum,manager_employee_number AS manager,SUM(salary_amount) AS SumsalFROM employeeWHERE department_number < 402GROUP BY GROUPING SETS (department_number

,manager_employee_number)ORDER BY 1,2;

SELECT department_number AS deptnum,manager_employee_number AS manager,SUM(salary_amount) AS SumsalFROM employeeWHERE department_number < 402

ORDER BY 1,2;

deptnum

GROUP BY GROUPING SETS (department_number,manager_employee_number)

manager Sumsal? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00

100 ? 100000.00201 ? 73450.00301 ? 116400.00302 ? 56500.00401 ? 245575.00

deptnum manager Sumsal? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00

red

100 ? 100000.00201 ? 73450.00301 ? 116400.00302 ? 56500.00401 ? 245575.00

The report shows:4 manager totals (grouped over all

departments) -5 dept totals (grouped over all

managers) - blue

The two grouping sets are (department) and (manager), each of which is explicitly specified in the GROUPING SETS clause.

The two grouping sets are (department) and (manager), each of which is explicitly specified in the GROUPING SETS clause.

Extended Grouping Functions Page 14-33

Page 568: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Adding Grand Totals Suppose we wish to add to the previous report an additional line of output representing a grand total of all departments over all managers. We can explicitly add the empty set ( ), which will add the grand total to the report. In the output seen here, all three items are treated as individual grouping sets - (department), (manager), and ( ) providing:

• 4 manager totals (grouped over all departments) - red • 5 dept totals (grouped over all managers) - blue • 1 grand total row (grouped over all managers and departments) - black

Extended Grouping Functions Page 14-34

Page 569: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Adding Grand Totals

Example: Use GROUPING SETS to make a report of employee salaries aggregated by manager and department. In addition, show the grand total of all salaries.

SELECT department_number AS deptnum,manager_employee_number AS manager,SUM(salary_amount) AS Sumsal

FROM employeeWHERE department_number < 402GROUP BY GROUPING SETS (

department_number,manager_employee_number, ( ))

ORDER BY 1,2;

SELECT department_number AS deptnum,manager_employee_number AS manager,SUM(salary_amount) AS Sumsal

FROM employeeWHERE department_number < 402

ORDER BY 1,2;

deptnum

GROUP BY GROUPING SETS ( department_number,manager_employee_number, ( ))

manager Sumsal? ? 591925.00

100 ? 100000.00201 ? 73450.00301 ? 116400.00302 ? 56500.00401 ? 245575.00

? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00

deptnum manager Sumsal? ? 591925.00

100 ? 100000.00201 ? 73450.00301 ? 116400.00302 ? 56500.00401 ? 245575.00

All three items are treated as individual grouping sets(department), , and ( ) providing:

• 4 manager totals (grouped over all departments) -• 5 dept totals (grouped over all managers) - blue• 1 grand total row (grouped over all managers and departments) - black

? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00

(manager)

red

red

blue

black

Extended Grouping Functions Page 14-35

Page 570: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Combining GROUPING SETS (1 of 2) Consider now, a slight variation of the preceding query. If you inner-parenthesize the combination of department and manager, we are requesting only two grouping sets:

• Each department/manager combo • A grand total

Extended Grouping Functions Page 14-36

Page 571: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Combining GROUPING SETS (1 of 2)

Consider now, a slight variation of the preceding query. If you inner-parenthesize the combination of department and manager, the query would be as follows:

SELECT department_number AS deptnum,manager_employee_number AS manager,SUM(salary_amount) AS Sumsal

FROM employeeWHERE department_number < 402GROUP BY GROUPING SETS ( department_number,manager_employee_number), ( ))(ORDER BY 1,2;

SELECT department_number AS deptnum,manager_employee_number AS manager,SUM(salary_amount) AS Sumsal

FROM employeeWHERE department_number < 402

(ORDER BY 1,2; depnum

GROUP BY GROUPING SETS ( department_number,manager_employee_number), ( ))

manager Sumsal? ? 591925.00

100 801 100000.00201 801 34700.00201 1025 38750.00301 801 57700.00301 1019 58700.00302 801 56500.00401 801 37850.00401 1003 207725.00

depnum manager Sumsal? ? 591925.00

With this syntax, we are requesting only two grouping sets:• Each department/manager combo -• A grand total – blue (top row only)

100 801 100000.00201 801 34700.00201 1025 38750.00301 801 57700.00301 1019 58700.00302 801 56500.00401 801 37850.00401 1003 207725.00

red

Extended Grouping Functions Page 14-37

Page 572: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Combining GROUPING SETS (2 of 2) If we now add one last parameter (department_number) to the grouping set, the resulting query is seen next. Note that the sequence of parameters in the GROUPING SETS is not important. The following output groups are seen. Each department/manager combo - red A grand total - blue Totals by department - black Notice that this is equivalent to the output from a ROLLUP expression. Thus the expression:

• GROUP BY GROUPING SETS ((department_number,manager_employee_number), (),department_number) is functionally equivalent to the expression

• GROUP BY ROLLUP (department_number, manager_employee_number) Either syntax produces the following three aggregations: • Each department/manager combo • A grand total • Totals by department

Extended Grouping Functions Page 14-38

Page 573: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Combining GROUPING SETS (2 of 2)Add one last parameter (department_number) to the grouping setNote that the sequence of parameters in the GROUPING SETS is not important.

This is functionally equivalent to:

GROUP BY ROLLUP (department_number, manager_employee_number)

SELECT department_number AS deptnum,manager_employee_number AS manager,SUM(salary_amount) AS Sumsal

FROM employeeWHERE department_number < 402GROUP BY GROUPING SETS ((department_number,manager_employee_number), (),department_number)

ORDER BY 1,2;

SELECT department_number AS deptnum,manager_employee_number AS manager,SUM(salary_amount) AS Sumsal

FROM employeeWHERE department_number < 402

ORDER BY 1,2;

depnum

GROUP BY GROUPING SETS ((department_number,manager_employee_number), (),department_number)

manager Sumsal? ? 591925.00

100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

depnum manager Sumsal? ? 591925.00

100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

• A grand total - blue• Each department/manager combo -• Totals by department - black

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

red • A grand total - blue• Each department/manager combo -• Totals by department - black

red

Extended Grouping Functions Page 14-39

Page 574: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The Multiplier Effect A simple way to think of multiple grouping sets is to treat them as a multiplication problem. In this case there are two statements, each with a single parameter. The rule of multiplication is: GROUPING SETS(A), GROUPING SETS(B) = GROUPING SETS (A,B) So,

GROUPING SETS(department_number), GROUPING SETS(manager_employee_number) = GROUPING SETS (department_number, manager_employee_number)

Suppose we add the empty set ( ) to the second GROUPING SETS clause.

GROUP BY GROUPING SETS (department_number), GROUPING SETS ((manager_employee_number),())

Using the multiplier effect, the rules are as follows:

• Multiply each element of one clause by each element of the other. • Treat the empty set ( ) as the number 1 for multiplication purposes.

Example

GROUPING SETS(A), GROUPING SETS(B, () ) = GROUPING SETS ( (A,B), (A) )

because (A)(B) = (A,B) and (A)( ) = (A) So,

GROUPING SETS (department_number), GROUPING SETS ((manager_employee_number), () )

is equivalent to

GROUPING SETS ((department_number, manager_employee_number), (department_number ))

Got that?

Extended Grouping Functions Page 14-40

Page 575: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

The Multiplier Effect

A simple way to think of multiple grouping sets is as a multiplication problem.

GROUPING SETS(A), GROUPING SETS(B) = GROUPING SETS (A,B)So, GROUPING SETS(dept), GROUPING SETS(mgr) = GROUPING SETS (dept,mgr)

If we add the empty set ( ) to the second GROUPING SETS clause as seen here:GROUP BY GROUPING SETS (dept),GROUPING SETS ((mgr),( ))

Using the multiplier effect, the rules are:• Multiply each element of one clause

by each element of the other. • Treat the empty set ( ) as the number

1 for multiplication purposes.

Using the multiplier effect, the rules are:• Multiply each element of one clause

by each element of the other. • Treat the empty set ( ) as the number

1 for multiplication purposes.

Example

GROUPING SETS(A), GROUPING SETS(B, () ) = GROUPING SETS ( (A,B), (A) ) because (A)(B) = (A,B) and (A)( ) = (A)So, the problems resolves as follows:GROUPING SETS (dept), GROUPING SETS ((mgr), () ) is equivalent toGROUPING SETS ((dept,mgr),(dept ))

Extended Grouping Functions Page 14-41

Page 576: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Multiple GROUPING SETS Clauses (1 of 2) In the previous example, we saw the following GROUP BY clause:

GROUP BY GROUPING SETS (department_number), GROUPING SETS ((manager_employee_number),())

With this syntax, we saw that we are requesting these two grouping sets: • Each department/manager combo • Totals by department

Suppose we add the empty set ( ) to both GROUPING SETS clauses.

GROUP BY GROUPING SETS (department_number, () ), GROUPING SETS ((manager_employee_number), () )

Using the multiplier effect, cross-multiplying both sets we get:

GROUPING SETS(A, () ), GROUPING SETS(B, () ) = GROUPING SETS ( (A,B), (A ), (B), () )

because (A)(B) = (A,B) and (A)( ) = (A) and (B)( ) = (B) and ( )( ) = ( ) So,

GROUPING SETS (department_number, () ), GROUPING SETS ((manager_employee_number),() )

is equivalent to

GROUPING SETS ((department_number,manager_employee_number), (department_number),(manager_employee_number, () )

Extended Grouping Functions Page 14-42

Page 577: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Multiple GROUPING SETS Clauses (1 of 2)

Suppose we add the empty set ( ) to both GROUPING SETS clauses.GROUP BY GROUPING SETS (dept, ()

()

GROUP BY GROUPING SETS (department_number,( )),GROUPING SETS (manager_employee_number,( ))

),GROUPING SETS (mgr, )Using the multiplier effect, cross-multiplying both sets we get:GROUPING SETS(A, () ), GROUPING SETS(B, () ) = GROUPING SETS ( (A,B), (A ), (B), () )because (A)(B) = (A,B)

and (A)( ) = (A)and (B)( ) = (B)and ( )( ) = ( )

So, GROUPING SETS (dept, () ), GROUPING SETS (mgr,() ) is equivalent toGROUPING SETS ((dept,mgr), (dept),(mgr), () )

Get all salary aggregation permutations for department and manager, using GROUPING SETS.

Example SELECT department_number,manager_employee_number,SUM(salary_amount)FROM employeeWHERE department_number < 402

ORDER BY 1,2;

SELECT department_number,manager_employee_number,SUM(salary_amount)FROM employeeWHERE department_number < 402

ORDER BY 1,2;

Which four grouping sets are produced by this example? Answer is on next page.

GROUP BY GROUPING SETS (department_number,( )),GROUPING SETS (manager_employee_number,( ))

Which four grouping sets are produced by this example? Answer is on next page.

Extended Grouping Functions Page 14-43

Page 578: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Multiple GROUPING SETS Clauses (2 of 2) With this syntax

GROUP BY GROUPING SETS (department_number,( )), GROUPING SETS (manager_employee_number,( ))

we are requesting four grouping sets:

• Each department/manager combo • Totals by department • Totals by manager • Grand total

In other words, all possibilities are provided. This is functionally the equivalent of a CUBE. So,

GROUP BY GROUPING SETS (department_number,( )), GROUPING SETS (manager_employee_number,( ))

is equivalent to

GROUP BY CUBE (department_number,manager_employee_number)

Extended Grouping Functions Page 14-44

Page 579: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Multiple GROUPING SETS Clauses (2 of 2)

GROUP BY GROUPING SETS (dept,( )),GROUPING SETS (mgr,( ))

This construct produces four grouping sets:• Each department/manager combo• Totals by department • Totals by manager• Grand total

In other words, all possibilities are provided. This is functionally the equivalent of a CUBE. So,GROUP BY GROUPING SETS (dept,( )),

GROUPING SETS (mgr,( ))is equivalent toGROUP BY CUBE (dept,mgr)

GROUP BY GROUPING SETS (dept,( )),GROUPING SETS (mgr,( ))

This construct produces four grouping sets:• Each department/manager combo• Totals by department

• Grand totalIn other words, all possibilities are provided. This is functionally the equivalent of a CUBE. So,GROUP BY GROUPING SETS (dept,( )),

GROUPING SETS (mgr,( ))is equivalent toGROUP BY CUBE (dept,mgr)

There is at least one other equivalent syntax approach which can also produce this same result for a two-dimensional cube. Consider the following:GROUP BY GROUPING SETS ((dept,mgr),dept, mgr,())In this case, all four grouping options are explicitly expressedin a single GROUPING SETS clause. This will produce the same result as the two preceding options.

• Totals by manager

There is at least one other equivalent syntax approach which can also produce this same result for a two-dimensional cube. Consider the following:GROUP BY GROUPING SETS ((dept,mgr),dept, mgr,())In this case, all four grouping options are explicitly expressedin a single GROUPING SETS clause. This will produce the same result as the two preceding options.

Explicit Approach

Multiplier Effect and CUBE

Extended Grouping Functions Page 14-45

Page 580: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Three Approaches All three approaches seen on the facing page will produce the same result. All four grouping sets are produced:

• Each department/manager combo • Totals by department • Totals by manager • Grand total

Three Approaches

• The ‘explicit’ approach: GROUP BY GROUPING SETS ((department_number,manager_employee_number), department_number, manager_employee_number,())

• The ‘multiplier’ approach: GROUP BY GROUPING SETS (department_number,( )), GROUPING SETS (manager_employee_number,( ))

• The CUBE approach: GROUP BY CUBE (department_number,manager_employee_number

Extended Grouping Functions Page 14-46

Page 581: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

orGROUP BY GROUPING SETS

(department_number,( )),GROUPING SETS

(manager_employee_number,( ))orGROUP BY CUBE (department_number

,manager_employee_number)

orGROUP BY GROUPING SETS

(department_number,( )),GROUPING SETS

(manager_employee_number,( ))orGROUP BY CUBE (department_number

,manager_employee_number)

Multiplier

Cube

Three ApproachesSELECT department_number AS deptnum

,manager_employee_number AS manager,SUM(salary_amount) AS Sumsal

FROM employee WHERE department_number < 402GROUP BY GROUPING SETS ((department_number,manager_employee_number),department_number, manager_employee_number,())ORDER BY 1,2;

SELECT department_number AS deptnum,manager_employee_number AS manager,SUM(salary_amount) AS Sumsal

FROM employee WHERE department_number < 402

ORDER BY 1,2;

ExplicitGROUP BY GROUPING SETS ((department_number,manager_employee_number),department_number, manager_employee_number,())

Deptno Mngr SumSal? ? 591925.00? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

Deptno Mngr SumSal? ? 591925.00? 801 286750.00? 1003 207725.00? 1019 58700.00? 1025 38750.00

100 801 100000.00

201 801 34700.00201 1025 38750.00

301 801 57700.00301 1019 58700.00

302 801 56500.00

401 801 37850.00401 1003 207725.00

(all depts, all managers)(all depts, one manager)

(one dept, all managers)(one dept, one manager)(one dept, all managers)

(all depts, all managers)(all depts, one manager)(one dept, all managers)(one dept, one manager)

100 ? 100000.00

201 ? 73450.00

301 ? 116400.00

302 ? 56500.00

401 ? 245575.00

Extended Grouping Functions Page 14-47

Page 582: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Grouping Exercise Answer the questions on the facing page.

Extended Grouping Functions Page 14-48

Page 583: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Grouping Exercise

Consider the following query with three GROUPING SETS.

SELECT department_number,manager_employee_number,SUM(salary_amount)FROM employeeWHERE department_number < 402GROUP BY GROUPING SETS (department_number),GROUPING SETS (manager_employee_number),GROUPING SETS (())ORDER BY 1,2;

What result sets are produced by this construct?

Recreate the query using only a single GROUPING SETS clause.

Extended Grouping Functions Page 14-49

Page 584: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)

Extended Grouping Functions Page 14-50

Page 585: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)1a.) Create the equivalent

of this query using GROUPING SETS syntax. Verify theoutput is identical.

*** Query completed. 17 rows found. 3 columns returned. department_number job_code Sum(salary_amount)

? ? 591925.00100 ? 100000.00100 111100 100000.00201 ? 73450.00201 211100 34700.00201 222101 38750.00301 ? 116400.00301 311100 57700.00301 312101 29450.00301 312102 29250.00302 ? 56500.00302 321100 56500.00401 ? 245575.00401 411100 37850.00401 412101 107825.00401 412102 56800.00401 413201 43100.00

*** Query completed. 17 rows found. 3 columns returned. department_number job_code Sum(salary_amount)

? ? 591925.00100 ? 100000.00100 111100 100000.00201 ? 73450.00201 211100 34700.00201 222101 38750.00301 ? 116400.00301 311100 57700.00301 312101 29450.00301 312102 29250.00302 ? 56500.00302 321100 56500.00401 ? 245575.00401 411100 37850.00401 412101 107825.00401 412102 56800.00401 413201 43100.00

1b.) Using the GROUPING and COALESCE functions, remove all ‘?’s in this report.

SELECT department_number,,job_code,SUM(salary_amount)FROM employeeWHERE department_number < 402GROUP BY ROLLUP (department_number, job_code)ORDER BY 1,2;

Extended Grouping Functions Page 14-51

Page 586: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

Extended Grouping Functions Page 14-52

Page 587: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)2a.) Create the equivalent of this query

using GROUPING SETS syntax. Verify the output is identical.

*** Query completed. 16 rows found. 3 columns returned.

department_number job_code Sum(salary_amount)? ? 289850.00? 111100 100000.00? 211100 34700.00? 222101 38750.00? 311100 57700.00? 312101 29450.00? 312102 29250.00100 ? 100000.00100 111100 100000.00201 ? 73450.00201 211100 34700.00201 222101 38750.00301 ? 116400.00301 311100 57700.00301 312101 29450.00301 312102 29250.00

*** Query completed. 16 rows found. 3 columns returned.

department_number job_code Sum(salary_amount)? ? 289850.00? 111100 100000.00? 211100 34700.00? 222101 38750.00? 311100 57700.00? 312101 29450.00? 312102 29250.00100 ? 100000.00100 111100 100000.00201 ? 73450.00201 211100 34700.00201 222101 38750.00301 ? 116400.00301 311100 57700.00301 312101 29450.00301 312102 29250.00

3.) What is the equivalent of this GROUP BY clause? Express it in a single GROUPING SETS clause.

2b.) Using the GROUPING and COALESCE functions, remove all ‘?’s in this report.

SELECT department_number,job_code,SUM(salary_amount)

FROM employeeWHERE department_number < 302GROUP BY CUBE (department_number

,job_code)ORDER BY 1,2;

SELECT department_number,job_code,SUM(salary_amount)

FROM employeeWHERE department_number < 302GROUP BY CUBE (department_number

,job_code)ORDER BY 1,2;

GROUP BY GROUPING SETS (department_number ),GROUPING SETS (job_code, manager_employee_number),GROUPING SETS ( )

GROUP BY GROUPING SETS (department_number ),GROUPING SETS (job_code, manager_employee_number),GROUPING SETS ( )

Extended Grouping Functions Page 14-53

Page 588: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Extended Grouping Functions Page 14-54

Page 589: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 15

Module 15MERGE INTO Statement

After completing this module, you should be able to:

• Describe the benefits of the MERGE INTO statement.

• Compare and contrast the MERGE INTO and the UPDATE commands.

• Create an Upsert query using the MERGE INTO statement.

• Distinguish between the transaction protocols of ANSI and Teradata mode.

MERGE INTO Statement Page 15-1

Page 590: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

MERGE INTO Statement Page 15-2

Page 591: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

MERGE INTO Statement Page 15-3

Page 592: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Transaction Semantics: BTET Mode A transaction is a unit of work performed against one or more tables of a database. It can consist of one or more data-changing statements. By definition, a transaction must commit all of its associated changes or they must all be rolled back, meaning all changes are returned to the original pre-transaction state. A transaction is an all-or-nothing proposition i.e., it either succeeds in its entirety, or it is entirely rolled back. Knowing that transactions cannot partially complete provides assurance of data integrity. Teradata Mode Transactions

.SET SESSION TRANSACTION BTET Teradata mode also is referred to as BTET mode, which stands for BEGIN TRANSACTION / END TRANSACTION. In BTET mode, all individual requests are treated as single implicit transactions. If you need to combine requests into a single transaction, use the BEGIN and END TRANSACTION delimiters. Locks are accumulated following a BT until an ET is issued at which time all held locks are released. In Teradata mode, a rollback of the transaction will occur if any of the following events occur: ROLLBACK WORK explicit rollback of active transaction Session Abort rollback of active transaction SQL statement failure rollback of active transaction

MERGE INTO Statement Page 15-4

Page 593: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Transaction Semantics: BTET Mode

.LOGONINSERT row1; (txn #1)INSERT row2; (txn #2).LOGOFF

.LOGONINSERT row1; (txn #1)INSERT row2; (txn #2).LOGOFF

.LOGONBT; (txn #1)INSERT row1;INSERT row2;ET;.LOGOFF

.LOGONBT; (txn #1)INSERT row1;INSERT row2;ET;.LOGOFF

• Two SQL statements.• Two requests.• Two transactions.• Failure of one doesn’t affect the other.

• Four SQL statements.• Four requests.• One transaction.• Failure of either insert rolls back to BT.

Implicit Transaction Explicit Transaction• Each SQL request is an implicit

transaction by default.• Each failed request is

automatically rolled back.

• Launched with explicit BEGIN TRANSACTION (BT)

• Ended explicitly by:END TRANSACTION (ET)commit work done since BTROLLBACKrollback work done since BT

• Ended implicitly by:Any request failurerollback work done since BT

MERGE INTO Statement Page 15-5

Page 594: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Transaction Semantics: ANSI Mode ANSI Mode Transactions

.SET SESSION TRANSACTION ANSI ANSI mode is also referred to as COMMIT mode. It automatically accumulates all requests until an explicit COMMIT is submitted. At that point, all transactions are committed to the database and the transaction is ended. All transactions in ANSI mode are considered explicit, that is, they require an explicit COMMIT command to complete. When an error occurs in a request, the transaction continues. If the request was the first request in the transaction, the transaction begins with the next request. In ANSI mode, there is a distinction made between a request error and a request failure. This will be seen in more detail on the next slide. Teradata mode makes no such distinction and all errors are treated as failures.

MERGE INTO Statement Page 15-6

Page 595: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

.LOGONINSERT row1;INSERT row2;.LOGOFF

.LOGONINSERT row1;INSERT row2;.LOGOFF

Without COMMIT

Transaction Semantics: ANSI Mode

• Logoff without COMMIT.• Both INSERTs roll back.• ANSI requires explicit

COMMIT of transaction.

• All transactions in ANSI mode are explicit.• Transaction is launched with first SQL request.• Transaction are ended explicitly by:

COMMIT WORK: commit work done since launchROLLBACK: rollback work done since launch

• Request error causes:−Rollback of request−Transaction to continue

• COMMIT commits all work since start of transaction.

.LOGONINSERT row1;INSERT row2; COMMIT WORK;.LOGOFF

.LOGONINSERT row1;INSERT row2; COMMIT WORK;.LOGOFF

With COMMIT

Insert 1 error - Request rollback.Insert 2 - Starts transaction.COMMIT - Commits Insert 2.

Insert 1 – Starts transaction.Insert 2 error - Request rollback. COMMIT - Commits Insert 1

Insert 1 error - Request rollback.Insert 2 - Starts transaction.COMMIT - Commits Insert 2.

Insert 1 – Starts transaction.Insert 2 error - Request rollback. COMMIT - Commits Insert 1

Explicit TransactionScenarios

MERGE INTO Statement Page 15-7

Page 596: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Transaction ROLLBACK: ANSI Mode A rollback operation occurs when for some reason, the transaction is not able to complete. Rollback will automatically undo any changes which have been applied during the transaction and release all locks which are being held by the transaction. Following a rollback, there is no active transaction until another one is initiated. In ANSI mode, a rollback will occur in the following situations:

• ROLLBACK work: Explicit rollback of an active transaction • Session Abort : Rollback of active transaction • SQL statement failure: Rollback of active transaction • SQL statement error: Rollback the current statement only

It is important to understand the difference between ANSI and Teradata modes:

• ANSI mode: Makes a distinction between an SQL statement ‘error' and ‘failure'. • SQL Error encompasses most failures of SQL statements, including syntax problems. • SQL Failure rolls back entire transaction when any of the following occur:

• A deadlock condition occurs requiring transaction rollback • A DDL statement has failed • An explicit ROLLBACK command is encountered

SQL Error: Rolls back individual SQL statement when

• An SQL statement completes unsuccessfully, and • None of the above SQL 'failure' situations apply.

In this case: • The transaction continues • If this was the first statement in the TXN, then the next statement starts the TXN. • A statement that follows a COMMIT automatically starts a new transaction. Locks are

accumulated until a COMMIT is used. SQL Failure:

• All work associated with the transaction is rolled back. • The transaction terminates. • The next SQL request starts a new transaction.

MERGE INTO Statement Page 15-8

Page 597: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Transaction ROLLBACK: ANSI Mode

A rollback in ANSI-mode can occur at the transaction or the request level.A request-level rollback undoes the work associated with the request. A transaction-level rollback undoes the work associated with the transaction.

• ROLLBACK work: Explicit rollback of an active transaction• Session Abort: Rollback of active transaction• SQL statement failure: Rollback of active transaction• SQL statement error: Rollback the current statement only

• ROLLBACK work: Explicit rollback of an active transaction• Session Abort: Rollback of active transaction• SQL statement failure: Rollback of active transaction• SQL statement error: Rollback the current statement only

In ANSI mode, a rollback occurs in the following situations:

Rolls back entire transaction when any of the following occur:• A deadlock condition occurs requiring transaction rollback. • A DDL statement has failed • An explicit ROLLBACK command is encountered.

In this case the transaction is rolled back and terminated.

Rolls back entire transaction when any of the following occur:• A deadlock condition occurs requiring transaction rollback. • A DDL statement has failed • An explicit ROLLBACK command is encountered.

In this case the transaction is rolled back and terminated.

Rolls back an individual SQL request when:• An SQL request completes unsuccessfully, and • None of the above SQL 'failure' situations apply.

In this case:• The request is rolled back, but the transaction continues. • If this was the first request in the TXN, the next request starts the TXN.

Rolls back an individual SQL request when:• An SQL request completes unsuccessfully, and • None of the above SQL 'failure' situations apply.

In this case:• The request is rolled back, but the transaction continues. • If this was the first request in the TXN, the next request starts the TXN.

SQL Failure

SQL Error

MERGE INTO Statement Page 15-9

Page 598: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Transaction Semantics: ANSI vs. BTET Mode The matrix on the facing page displays differences in transaction semantics between ANSI and BTET modes. A successful request may yield a zero activity count or a warning code. Using client-side logic, subsequent statements in the transaction may be skipped or redirected based on activity counts or other warning return codes of previous statements. Important Note: When using BEGIN and END transaction (BTET) to delineate a transaction, the outcome status may be checked after each statement. A failure will cause the current transaction to be terminated, however subsequent statements will continue to process serially. Any following SQL statements will be executed using implicit transaction mode and an encountered ET will generate a warning because the transaction actually ended on the failed statement.

MERGE INTO Statement Page 15-10

Page 599: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Transaction Semantics: ANSI vs. BTET Mode

ANSI mode BTET mode (explicit) BTET mode (implicit)

UPD AUPD BCOMMIT /* both commit */

BTUPD AUPD B

ET /* both commit */

UPD A /* A commits */UPD B /* B commits */

UPD AUPD B /* B error */COMMIT /* A commits */

BTUPD AUPD B /* B fails then both rollback*/

UPD A /* A commits */UPD B /* B fails */

UPD AUPD BROLLBACK /* both rollback */

BTUPD AUPD B

ROLLBACK /* both rollback */

ROLLBACK isnotapplicable in implicittransactions.

UPD AUPD BLOGOFF /* both rollback */

BTUPD AUPD B

LOGOFF /* both rollback */

UPD A /* A commits */UPD B /* B commits */LOGOFF

MERGE INTO Statement Page 15-11

Page 600: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

DDL and Transactions ANSI Mode In the special case of DDL (Data Definition Language) statements, they must always be followed immediately by a COMMIT statement. Macros containing DDL statements must contain a single DDL statement and be followed by an immediate COMMIT. BTET Mode A DDL statement must be immediately followed by an explicit END TRANSACTION if the transaction was started with BEGIN TRANSACTION. Macros Because macros are considered single request transactions, DDL statements must be either the last or the only statement in a macro.

MERGE INTO Statement Page 15-12

Page 601: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

DDL and Transactions

• DDL must be the last statement in a transaction.• DDL must be the last or the only statement in a macro.

In explicit transactions, DDL must be followed by ET. (End Transaction)

.SET SESSION TRANSACTION ANSI.SET SESSION TRANSACTION ANSIDDL must be followed by COMMIT WORK or ROLLBACK WORK.

.SET SESSION TRANSACTION BTET.SET SESSION TRANSACTION BTETIn implicit transactions, DDL is a single-statement transaction.

CREATE TABLE test_1 (f1 INTEGER);CREATE TABLE test_1 (f1 INTEGER);Example:

BT;CREATE TABLE test_1 (f1 INTEGER);ET;

BT;CREATE TABLE test_1 (f1 INTEGER);ET;

Example:

CREATE TABLE test_1 (f1 INTEGER);COMMIT WORK;CREATE TABLE test_1 (f1 INTEGER);COMMIT WORK;

Example:

MERGE INTO Statement Page 15-13

Page 602: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Traditional Upsert Processing Traditionally, Upsert processing is the ability to update a row if it exists and to insert it if it does not exist. Ideally, this should be accomplished with a single SQL command. Teradata SQL has addressed this issue by extending the functionality of the UPDATE command to perform an optional INSERT when the row does not exist. Consider the following table. It is used to support the examples on the facing page.

CREATE SET TABLE department (department_number SMALLINT, department_name CHAR(30) NOT NULL, budget_amount DECIMAL(10,2), manager_employee_number INTEGER)

UNIQUE PRIMARY INDEX ( department_number ) UNIQUE INDEX ( department_name ); SELECT department_number AS Dept /* prior to upserts */

, department_name (CHAR(10)) AS DeptName , budget_amount , manager_employee_number

FROM department ORDER BY 1;

Dept DeptName budget_amount manager_employee_number 100 president 400000.00 801 201 technical 293800.00 1025 301 research a 465600.00 1019 302 product pl 226000.00 1016 401 customer s 982300.00 1003 402 software s 308000.00 1011 403 education 932000.00 1005 501 marketing 308000.00 1017 600 None ? 1099 SELECT department_number AS Dept /* post upserts */

, department_name (CHAR(10))AS DeptName , budget_amount , manager_employee_number

FROM department WHERE department_number IN (600, 601); Dept DeptName budget_amount manager_employee_number 600 None 900000.00 1099 601 new dept 900000.00 ?

MERGE INTO Statement Page 15-14

Page 603: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Traditional Upsert Processing

Traditionally, Upsert updates a row if it exists and inserts it if it does not exist.

.

Example 1

Using traditional Upsert processing, change department 600 budget to be $900,000.00.

UPDATE departmentSET budget_amount = 900000WHERE department_number = 600ELSE INSERT INTO departmentVALUES (600, 'new dept', 900000,NULL);

UPDATE departmentSET budget_amount = 900000WHERE department_number = 600ELSE INSERT INTO departmentVALUES (600, 'new dept', 900000,NULL);

The department already exists, thus an update occurs

Example 2

Using traditional Upsert processing, insert new department 601.

UPDATE departmentSET budget_amount = 900000WHERE department_number = 601ELSE INSERT INTO departmentVALUES (601, 'new dept', 900000,NULL);

UPDATE departmentSET budget_amount = 900000WHERE department_number = 601ELSE INSERT INTO departmentVALUES (601, 'new dept', 900000,NULL);

The department doesn't exists, thus an insert takes place.

Dept DeptName budget_amount manager_employee_number600 None 900000.00 1099601 new dept 900000.00 ?

Dept DeptName budget_amount manager_employee_number600 None 900000.00 1099601 new dept 900000.00 ?

Result of these two upserts:

MERGE INTO Statement Page 15-15

Page 604: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

UPDATE Form of UPSERT: Restrictions These are some Restrictions on Upserts via the UPDATE Command. Both the UPDATE and INSERT portions of the statement must conform to these requirements or an error is returned:

• Both must reference the same table. • Both must reference the same row. • Both must use the Primary Index value of the row to perform their respective operations. • The Primary Index column cannot be an Identity Column

When performing an Upsert using UPDATE to a table which has a Partitioned Primary Index, the following rules must be followed or an error is returned:

• The INSERT portion must specify the same partition as the UPDATE portion. • The UPDATE must not modify any of the partitioning columns. • All values of the partitioning columns must be specified in the WHERE clause of the

UPDATE portion.

MERGE INTO Statement Page 15-16

Page 605: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

UPDATE Form of UPSERT: Restrictions

• Both must reference the same table. • Both must reference the same same row. • Both must use the Primary Index value of the row to perform their respective

operations. • The Primary Index column cannot be an Identity Column

Both the UPDATE and INSERT portions of the statement must conform to these requirements or an error is returned:

• The INSERT portion must specify the same partition as the UPDATE portion. • The UPDATE must not modify any of the partitioning columns. • All values of the partitioning columns must be specified in the WHERE clause

of the UPDATE portion.

When performing an Upsert using UPDATE to a table which has a Partitioned Primary Index, the following rules must be followed or an error is returned:

MERGE INTO Statement Page 15-17

Page 606: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

MERGE INTO Statement: Example 1 The MERGE INTO statement 'merges' a source row into a target table based on whether any rows in the target table match a specified condition with the source row. MERGE INTO replicates the functionality of the Upsert capability provided by the UPDATE with INSERT option. This option updates a row if it exists and inserts it if it does not. MERGE INTO is a Teradata extension to the ANSI SQL-99 standard. It is a partial implementation of the anticipated full ANSI SQL-99 specification for MERGE. Results of MERGE INTO on facing page:

SELECT department_number AS Dept , department_name (CHAR(10))AS DeptName , budget_amount , manager_employee_number

FROM department WHERE department_number = 700; Dept DeptName budget_amount manager_employee_number ------- --------------- ----------------------- ----------------------------------------- 700 Shipping 800000.00 ?

MERGE INTO Statement Page 15-18

Page 607: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

MERGE INTO Statement: Example 1

The MERGE INTO statement:• ‘Merges' a source row into a target table based on whether any rows in the target

table match a specified condition with the source row.• Replicates the functionality of Upsert provided by the UPDATE with INSERT option. • Is a Teradata extension to the ANSI SQL-99 standard.

Add a new row for Department 700 using the MERGE INTO form of Upsert.

MERGE INTO departmentUSING VALUES (700,'Shipping',800000.00)AS Dept (deptnum, deptname, budgamt)ON Dept.deptnum = department_numberWHEN MATCHED THEN UPDATE

WHEN NOT MATCHED THEN INSERTSET budget_amount = Dept.budgamt

VALUES (Dept.deptnum,Dept.deptname,Dept.budgamt,NULL);

MERGE INTO departmentUSING VALUES (700,'Shipping',800000.00)AS Dept (deptnum, deptname, budgamt)ON Dept.deptnum = department_number

SET budget_amount = Dept.budgamt

VALUES (Dept.deptnum,Dept.deptname,Dept.budgamt,NULL);

Things to notice about this example:• It inserts a 'Shipping' department row. • The USING clause defines the source

WHEN MATCHED THEN UPDATE

WHEN NOT MATCHED THEN INSERT

row to be merged. • The AS clause defines the name of the

temporary source table and columns which contain the source row. This is also referred to as the 'correlation table'.

• The ON clause specifies the match condition between the source and target tables.

• It must use the primary index of the target table.

• If the primary index is non-unique, additional qualifying conditions can be added if a single target row is desired.

Things to notice about this example:• It inserts a 'Shipping' department row. • The USING clause defines the source

row to be merged. • The AS clause defines the name of the

temporary source table and columns which contain the source row. This is also referred to as the 'correlation table'.

• The ON clause specifies the match condition between the source and target tables.

• It must use the primary index of the target table.

• If the primary index is non-unique, additional qualifying conditions can be added if a single target row is desired.

MERGE INTO Statement Page 15-19

Page 608: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

MERGE INTO Statement: Example 2 This example does an update of the previously inserted 'Shipping' department row.

MERGE INTO Statement Page 15-20

Page 609: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

MERGE INTO Statement: Example 2

Update Department 700’s budget to $9,900,000 using the MERGE INTO form of Upsert.

MERGE INTO departmentUSING VALUES (700,'Shipping',9900000.00) AS Dept (deptnum, deptname, budgamt)ON Dept.deptnum = department_numberWHEN MATCHED THEN UPDATESET budget_amount = Dept.budgamtWHEN NOT MATCHED THEN INSERTVALUES (Dept.deptnum, Dept.deptname,Dept.budgamt,NULL);

MERGE INTO departmentUSING VALUES (700,'Shipping',9900000.00) AS Dept (deptnum, deptname, budgamt)ON Dept.deptnum = department_numberWHEN MATCHED THEN UPDATESET budget_amount = Dept.budgamtWHEN NOT MATCHED THEN INSERTVALUES (Dept.deptnum, Dept.deptname,Dept.budgamt,NULL);

SELECT department_number AS Dept, department_name (CHAR(10))AS DeptName, budget_amount, manager_employee_numberFROM departmentWHERE department_number = 700;

SELECT department_number AS Dept, department_name (CHAR(10))AS DeptName, budget_amount, manager_employee_numberFROM departmentWHERE department_number = 700;

Dept DeptName budget_amount manager_employee_number700 Shipping 9900000.00 ?

Dept DeptName budget_amount manager_employee_number700 Shipping 9900000.00 ?

This example does an update of the previously inserted 'Shipping' department row.

This example does an update of the previously inserted 'Shipping' department row.

MERGE INTO Statement Page 15-21

Page 610: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a MERGE INTO Macro The example seen on this page demonstrates the MERGE INTO statement in a macro.

MERGE INTO Statement Page 15-22

Page 611: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a MERGE INTO Macro

Create a macro which upserts a department row and a budget amount and which shows the row after updating or inserting it.

CREATE MACRO dept_budget (deptno INT, budget DEC(10,2)) AS (

MERGE INTO departmentUSING VALUES (:deptno,:budget) AS Dept (deptnum,budgamt)ON Dept.deptnum = department_numberWHEN MATCHED THEN UPDATESET budget_amount = Dept.budgamtWHEN NOT MATCHED THEN INSERTVALUES (Dept.deptnum, 'new dept',Dept.budgamt,NULL);

SELECT department_number AS Dept, department_name (CHAR(10))AS DeptName, budget_amount, manager_employee_number

FROM departmentWHERE department_number = :deptno;);

CREATE MACRO dept_budget (deptno INT, budget DEC(10,2)) AS (

MERGE INTO departmentUSING VALUES (:deptno,:budget) AS Dept (deptnum,budgamt)ON Dept.deptnum = department_numberWHEN MATCHED THEN UPDATESET budget_amount = Dept.budgamtWHEN NOT MATCHED THEN INSERTVALUES (Dept.deptnum, 'new dept',Dept.budgamt,NULL);

SELECT department_number AS Dept, department_name (CHAR(10))AS DeptName, budget_amount, manager_employee_number

FROM departmentWHERE department_number = :deptno;);

MERGE INTO Statement Page 15-23

Page 612: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Executing a MERGE INTO Macro The example seen on this page demonstrates the execution of the MERGE INTO statement via the following macro:

CREATE MACRO dept_budget (deptno INT, budget DEC(10,2)) AS ( MERGE INTO department USING VALUES (:deptno,:budget) AS Dept (deptnum, budgamt) ON Dept.deptnum = department_number WHEN MATCHED THEN UPDATE SET budget_amount = Dept.budgamt WHEN NOT MATCHED THEN INSERT VALUES (Dept.deptnum, 'new dept',Dept.budgamt,NULL); SELECT department_number AS Dept , department_name (CHAR(10))AS DeptName , budget_amount , manager_employee_number FROM department WHERE department_number = :deptno;);

MERGE INTO Statement Page 15-24

Page 613: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Executing a MERGE INTO Macro

Use this macro to update the budget amount for department 600 to $950,000.00.EXEC dept_budget (600, 950000.00);EXEC dept_budget (600, 950000.00);

Dept DeptName budget_amt mgr_emp_num600 None 950000.00 1099

Dept DeptName budget_amt mgr_emp_num600 None 950000.00 1099

Things to notice about this example:•This example updates the budget

amount of the existing department 600 row.

•The USING VALUES are specified as host variables.

•The values of these variables are provided as parameters to the macro at execution time.

•This example updates the budget amount of the existing department 600 row.

•The USING VALUES are specified as host variables.

•The values of these variables are provided as parameters to the macro at execution time.

Example 2Example 1

Use the macro to insert a row for department 800.EXEC dept_budget (800, 975000.00);EXEC dept_budget (800, 975000.00);

Dept DeptName budget_amt mgr_emp_num800 new dept 975000.00 ?

Dept DeptName budget_amt mgr_emp_num800 new dept 975000.00 ?

This example inserts a row for dept number 800.This example inserts a row for dept number 800.

Things to notice about this example:

MERGE INTO Statement Page 15-25

Page 614: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using a Subquery with MERGE INTO The USING VALUES clause may be replaced by a subquery which returns a single row of values to be used by the MERGE operation. It must be a UPI or USI equality access which cannot return more than a single row.

REPLACE MACRO dept_budget (deptno INT, budgamt DEC(10,2)) AS ( MERGE INTO department USING (SELECT :deptno, dept_name, :budgamt FROM dept_name_assignment WHERE dept_number = :deptno) AS Dept (deptnum, deptname, budgamt) ON Dept.deptnum = department_number WHEN MATCHED THEN UPDATE SET budget_amount = Dept.budgamt, department_name = Dept.deptname WHEN NOT MATCHED THEN INSERT VALUES (Dept.deptnum, Dept.deptname,Dept.budgamt,NULL);); EXEC dept_budget(600,2000000);

Things to notice about this example:

• The macro is given a department number and a budget amount as parameters. • It checks the department name assignment table to see the current name assignment for

this department. • If the department exists, the budget amount and the department name are updated. • If the department doesn't exist, a row is inserted for the department with the designated

budget amount and name. • A SELECT subquery was used in place of a value list. • The host variables are treated as 'constants' in the SELECT list.

Other considerations:

• The SELECT statement must return a single row. • It must be either a UPI or USI access • If the subquery returns no rows, then neither the insert nor the update are performed on

the target table.

MERGE INTO Statement Page 15-26

Page 615: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Using a Subquery with MERGE INTO

REPLACE MACRO dept_budget (deptno INT, budgamt DEC(10,2)) AS (

MERGE INTO departmentUSING (SELECT :deptno

,dept_name,:budgamt

FROM dept_name_assignmentWHERE dept_number = :deptno)

AS Dept (deptnum, deptname, budgamt)ON Dept.deptnum = department_numberWHEN MATCHED THEN UPDATESET budget_amount = Dept.budgamt

,department_name = Dept.deptnameWHEN NOT MATCHED THEN INSERTVALUES (Dept.deptnum,

Dept.deptname,Dept.budgamt,NULL););

REPLACE MACRO dept_budget (deptno INT, budgamt DEC(10,2)) AS (

MERGE INTO departmentUSING

AS Dept (deptnum, deptname, budgamt)ON Dept.deptnum = department_numberWHEN MATCHED THEN UPDATESET budget_amount = Dept.budgamt

,department_name = Dept.deptnameWHEN NOT MATCHED THEN INSERTVALUES (Dept.deptnum,

Dept.deptname,Dept.budgamt,NULL););

• The macro is given a department number and budget as parameters.

• It checks the department name assignment table to see the current name assignment for this department.

• If the dept exists, the budget and the dept name are updated.

• If the dept doesn't exist, a row is inserted for the dept.

• A SELECT subquery was used in place of a value list.

• The host variables are treated as 'constants' in the SELECT list.

• The SELECT statement must return a single row via a a UPI or USI access.

• If the subquery returns no rows, then neither the insert nor the update are performed on the target table.

(SELECT :deptno,dept_name,:budgamt

FROM dept_name_assignmentWHERE dept_number = :deptno)

• The macro is given a department number and budget as parameters.

• It checks the department name assignment table to see the current name assignment for this department.

• If the dept exists, the budget and the dept name are updated.

• If the dept doesn't exist, a row is inserted for the dept.

• A SELECT subquery was used in place of a value list.

• The host variables are treated as 'constants' in the SELECT list.

• The SELECT statement must return a single row via a a UPI or USI access.

• If the subquery returns no rows, then neither the insert nor the update are performed on the target table.

USING VALUES may be replaced by a subquery that returns 1 row to be used by the MERGE. It must be a UPI or USI equality access that cannot return more than 1 row.

MERGE INTO Statement Page 15-27

Page 616: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

MERGE Form of UPSERT: Fewer Restrictions The restrictions for Upserts via UPDATE do not apply to the MERGE statement. The following list shows that the UPDATE and INSERT portions of the MERGE statement do not carry the same restrictions as the UPDATE form of Upsert.

• Both need not reference the same table. • Both need not reference the same row. • Both must still use the Primary Index value of the row to perform their respective

operations. • The Primary Index column may be an Identity Column • The INSERT portion may specify a different partition as the UPDATE portion. • The UPDATE may modify any of the partitioning columns. • All values of the partitioning columns need not be specified in the WHERE clause of the

UPDATE portion. In short, the MERGE INTO statement is a more flexible command that enables both traditional and non-traditional forms of Upsert processing. There are however, some restrictions on MERGE which do not exist on the UPDATE form.

• An UPDATE version of Upsert may be either a triggering or a triggered statement. • A MERGE statement may only be a triggering, not a triggered statement.

MERGE INTO Statement Page 15-28

Page 617: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

MERGE Form of UPSERT: Fewer Restrictions

• Both need not reference the same table. • Both need not reference the same same row. • Both must still use the Primary Index value of the row to perform their

respective operations. • The Primary Index column may be an identity column.• The INSERT portion may specify a different partition as the UPDATE portion.• The UPDATE may modify any of the partitioning columns.• All values of the partitioning columns need not be specified in the WHERE

clause of the UPDATE portion.

• An UPDATE version of Upsert may be either a triggering or a triggered statement.

• A MERGE statement may only be a triggering, not a triggered statement.

There are however, some restrictions on MERGE which do not exist on the UPDATE form:

The restrictions for Upserts via UPDATE do not apply to the MERGE statement. The UPDATE and INSERT portions of the MERGE statement do not carry the same restrictions as the UPDATE form of Upsert.

MERGE INTO Statement Page 15-29

Page 618: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

MERGE INTO and Partitioned Primary Index The following list shows that the UPDATE and INSERT portions of the MERGE statement for a table which has a Partitioned Primary Index do not carry the same restrictions as the UPDATE form of Upsert.

• The INSERT portion may specify a different partition as the UPDATE portion. • The UPDATE may modify any of the partitioning columns. • All values of the partitioning columns need not be specified in the WHERE clause of the

UPDATE portion. In short, the MERGE INTO statement is a more flexible command that enables both traditional and non-traditional forms of Upsert processing. Additionally, MERGE INTO has the following properties.

• The WHEN MATCHED THEN UPDATE clause may be used without the presence of a WHEN NOT MATCHED clause.

• The WHEN NOT MATCHED THEN INSERT clause may be used without the presence of a WHEN MATCHED clause.

• When both are present, MATCHED must precede NOT MATCHED.

MERGE INTO Statement Page 15-30

Page 619: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

MERGE INTO and Partitioned Primary Index

The following list shows that the UPDATE and INSERT portions of the MERGE statement for a table which has a Partitioned Primary Index do not carry the same restrictions as the UPDATE form of Upsert.

• The INSERT portion may specify a different partition as the UPDATE portion.

• The UPDATE may modify any of the partitioning columns. • All values of the partitioning columns need not be specified in the WHERE

clause of the UPDATE portion.

• The INSERT portion may specify a different partition as the UPDATE portion.

• The UPDATE may modify any of the partitioning columns. • All values of the partitioning columns need not be specified in the WHERE

clause of the UPDATE portion.

In short, the MERGE INTO statement is a more flexible command that enables both traditional and non-traditional forms of Upsert processing.In short, the MERGE INTO statement is a more flexible command that enables both traditional and non-traditional forms of Upsert processing.

Additionally, MERGE INTO has the following properties:

•The WHEN MATCHED THEN UPDATE clause may be used without the presence of a WHEN NOT MATCHED clause.

•The WHEN NOT MATCHED THEN INSERT clause may be used without the presence of a WHEN MATCHED clause.

•When both are present, MATCHED must precede NOT MATCHED.

•The WHEN MATCHED THEN UPDATE clause may be used without the presence of a WHEN NOT MATCHED clause.

•The WHEN NOT MATCHED THEN INSERT clause may be used without the presence of a WHEN MATCHED clause.

•When both are present, MATCHED must precede NOT MATCHED.

MERGE INTO Statement Page 15-31

Page 620: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)

MERGE INTO Statement Page 15-32

Page 621: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (1 of 2)

1.) Create a copy of this table and its data in your own database. Be sure to set your default database to your own 'wbtnnn' database id. Following the creation and populating of the table, drop the unique secondary index on the 'description' column.

CREATE TABLE job AS Customer_Service.job WITH DATA;DROP INDEX (description) ON job;

The following table exists in the Customer Service database and is used in this lab.

CREATE SET TABLE job(job_code INTEGER,description VARCHAR(40) NOT NULL,hourly_billing_rate DECIMAL(6,2),hourly_cost_rate DECIMAL(6,2))UNIQUE PRIMARY INDEX ( job_code )UNIQUE INDEX ( description );

2.) Create a macro called 'job_merge' which does the following: • Accepts input parameters: job INT, bill_rate DEC(6,2), cost_rate DEC(6,2) • If the input job code exists, update the bill and cost rates using the input

parameter values. • If the input job code does not exist, insert a new row for this job code using the

input parameter values and specifying a job description of 'new job'. • Selects the row that has been updated or inserted for display.

MERGE INTO Statement Page 15-33

Page 622: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

MERGE INTO Statement Page 15-34

Page 623: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab (2 of 2)

3.) Using the macro, add a new row with the following input values:job = 104200bill_rate = 26.00cost_rate = 13.00

4.) Select all rows from the table, ordering by job code.

SELECT * FROM job ORDER BY 1;

Verify the number of rows in the table and in each of these ranges: 100000's - 4 rows200000's - 1 rows300000's - 5 rows400000's - 9 rows500000's - 2 rows--------------------------

21 rows

Using the macro, update an existing row with following input values:job = 104201bill_rate = 24.00cost_rate = 12.00

MERGE INTO Statement Page 15-35

Page 624: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab Optional (1 of 2)

MERGE INTO Statement Page 15-36

Page 625: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab Optional (1 of 2)

6.) Create a macro called 'job_merge2' which does the following:

• Accepts a single input parameter:( job INTEGER).

• If the input job code exists, update the bill and cost rates by adding 1.25 to the existing rate.

• If the input job code does not exist, insert a new row for this job code using the input parameter job code, specifying a job description of 'new job' and providing bill and cost rates of 20.00.

5.) Set up an export script to extract all job codes from this table into a file. (Note: This is the BTEQ method. If working with Teradata SQL Assistant, please check the solution in Appendix B for the appropriate export method.)

.EXPORT data file = job_dataSELECT job_code FROM job;.EXPORT RESET ;

Note: If for any reason you need to recreate this file, you may do so by using this command to remove the file while in BTEQ:

.OS rm job_data ;

MERGE INTO Statement Page 15-37

Page 626: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab Optional (2 of 2)

MERGE INTO Statement Page 15-38

Page 627: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab Optional (2 of 2)

7.) Delete rows from the job table with job codes between 400000 and 499999. DELETE FROM job WHERE job_code BETWEEN 400000 and 499999;(9 rows deleted)

Create a script which reads each job code from the job_data file and inputs it to the macro.

(Note: This is the BTEQ method. If working with Teradata SQL Assistant, please check the solution in Appendix B for the appropriate import method.)

.IMPORT DATA FILE = job_data;

.REPEAT *USING (jobnum INT) EXEC job_merge_2 (:jobnum);

8.) Verify the results of the script execution. You should have reinserted the 9 rows previously deleted, and each of the existing rows should have its rate updated.

MERGE INTO Statement Page 15-39

Page 628: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

MERGE INTO Statement Page 15-40

Page 629: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Module 16

Module 16 – Recursive Queries

After completing this module, you should be able to:

• Use recursive queries to answer business questions.

• Use WITH derived tables as temporary tables in a query.

• Use WITH RECURSIVE tables to model recursive data.

Recursive Queries Page 16-1

Page 630: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Recursive Queries Page 16-2

Page 631: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Table of Contents

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

Recursive Queries Page 16-3

Page 632: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Recursive Query Description The recursive query feature enables queries against hierarchical data such as bills of material or corporate org charts. Recursive queries enable more than a single pass over the rows of a table in a single SQL statement. In traditional SQL queries, a set of rows is defined and an operation, such as a SELECT, is performed on those rows in a single pass.

A recursive query is useful when relationships between data elements may exist in multiple iterations. For example, assume that a table exists which shows all possible direct flights provided by an airline. To capture all the possible ways to fly from Los Angeles to New York, one would have to consider direct flights, flights with a single stopover, flights with two stopovers, etc. Each level of response will require a separate pass over the data.

The first pass over the table would provide non-stop flights only. The second pass over the table would provide flights with a single stopover. The third pass would provide flights with two stopovers, etc. Without the recursive query feature, this is done using one of the following techniques:

a. Embedding of SQL into a procedural language such as C or Stored Procedure Language (SPL).

b. Creating a new temporary table for each subsequent iteration against the data.

With the recursive query feature, this capability is built into the SQL language so that no external procedural language is required.

This feature has been implemented by adding the following new syntax to the SQL lexicon:

• WITH – Creates a derived table for non-recursive processing. • WITH RECURSIVE – Creates a derived table for recursive processing. • CREATE RECURSIVE VIEW – Creates a view for recursive processing.

We will review all of these in this module.

Recursive Queries Page 16-4

Page 633: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Recursive Query Description

Recursive queries are queries that process the same data more than once.

These queries are useful for processing data that is hierarchical in nature, i.e.,

• Bill of materials• Corporate org charts• Airline flight plans

Each pass over the data can add new rows to the recursive table.

To process those new rows, another pass over the data is made.

WITH – Creates a derived table for non-recursive processing. WITH RECURSIVE – Creates a derived table for recursive processing. CREATE RECURSIVE VIEW – Creates a view for recursive processing.

The following syntax supports recursive and non-recursive table/view creation.

Assume a table has all direct airline flights to Los Angeles for a particular airline:

You can see all direct flights to New York by reading through the table one time.You can see all one-stopover flights to NY by reading the table through twice.You can see all two-stopover flights to NY by reading the table through three times.

Each iteration may require a temporary table to be built and a new query to be run.

Recursive Queries Page 16-5

Page 634: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Building Tables For Recursive Processing First, we will create a Flights table to demonstrate how recursive querying would be accomplished without the recursive feature available. This table contains direct flights only, with no stopovers.

CREATE TABLE Flights ,FALLBACK ( Origin CHAR(3) NOT NULL ,Destination CHAR(3) NOT NULL ,Cost INT );

Next we can populate this table.

INSERT INTO Flights VALUES ('LAX', 'BOS', 300); INSERT INTO Flights VALUES ('LAX', 'SFO', 90); INSERT INTO Flights VALUES ('SFO', 'CHI', 275); INSERT INTO Flights VALUES ('CHI', 'BOS', 180); INSERT INTO Flights VALUES ('LAX', 'ATL', 250); INSERT INTO Flights VALUES ('ATL', 'BOS', 140);

SELECT * FROM Flights ORDER BY 1;

Origin Destination Cost ------ ----------- ------ ATL BOS 140 LAX BOS 300 LAX ATL 250 LAX SFO 90 CHI BOS 180 SFO CHI 275

Recursive Queries Page 16-6

Page 635: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Building Tables For Recursive Processing

First, we begin with a Flights table that contains direct flights only and no stopovers.

CREATE TABLE Flights( Origin CHAR(3) NOT NULL,Destination CHAR(3) NOT NULL,Cost INT);

SELECT * FROM Flights ORDER BY 1;

Origin Destination Cost--------- ----------------- ------

ATL BOS 140LAX BOS 300LAX ATL 250LAX SFO 90CHI BOS 180SFO CHI 275

Show all direct flights originating in Los Angeles

SELECT * FROM Flights WHERE Origin = 'LAX' ;

Origin Destination Cost--------- ----------------- ------LAX BOS 300LAX ATL 250LAX SFO 90

Show all direct flights.

Recursive Queries Page 16-7

Page 636: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a ‘One Stopover’ Table We will first assume that no recursive query feature exists in Teradata SQL. We will go through the steps needed to produce a recursive query.

First, create a new table containing all flights originating at LAX with one stopover.

CREATE TABLE Flights_1Stop (Origin, Destination, Cost) AS ( SELECT Flight1.Origin ,Flight2.Destination ,Flight1.Cost+Flight2.Cost AS Price FROM Flights Flight1 INNER JOIN Flights Flight2 ON Flight1.Destination = Flight2.Origin AND Flight1.Origin = 'LAX') WITH DATA;

Things to notice:

• The Flights table is joined to itself to find flights with one stopover. • Rows are joined if the destination of one flight equals the origin of the next flight.

Show all flight plans and pricing for flights originating at LAX and making one stopover.

SELECT * FROM Flights_1Stop;

Origin Destination Price ------ ----------- ----------- LAX CHI 365 (via SFO) LAX BOS 390 (via ATL)

Recursive Queries Page 16-8

Page 637: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a ‘One Stopover’ Table

Origin Destination Price--------- ---------------- -----------

LAX CHI 365 (via SFO)LAX BOS 390 (via ATL)

Assume that we don’t have the recursive feature available, but we wish to find all LAX-based flights with one stopover.

Create a new table containing all flights originating at LAX with one stopover

CREATE TABLE (Origin, Destination, Cost) AS (SELECT Flight1.Origin

,Flight2.Destination ,Flight1.Cost+Flight2.Cost AS Price

FROM INNER JOIN ON Flight1.Destination = Flight2.OriginAND Flight1.Origin = 'LAX')WITH DATA

Flights_1Stop

Flights Flight1 Flights Flight2

Things to notice:

Origin Destination Cost--------- ----------------- ------

ATL BOS 140LAX BOS 300LAX ATL 250LAX SFO 90CHI BOS 180SFO CHI 275

Flights Table

The Flights table is joined to itself to find flights with one stopover. Rows are joined if the destination of one flight equals the origin of the next flight.

Show all flight plans and pricing for flights starting at LAX making one stopover.

SELECT * FROM Flights_1Stop;

Recursive Queries Page 16-9

Page 638: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Querying Two Levels of Recursion To show all flights with either no connections or only one connection, a union of the two tables is needed.

Show all flights with either no connections or one connection originating from LAX.

SELECT * FROM Flights WHERE origin = 'LAX' UNION ALL SELECT * FROM Flights_1Stop;

Origin Destination Cost------ ----------- ----------- LAX BOS 300 LAX SFO 90 LAX ATL 250 LAX BOS 390 (one stopover in Atlanta) LAX CHI 365 (one stopover in San Francisco)

Things to notice: Table Flights = flights with no stopovers. Table Flights_1Stop = flights with a single stopover.

Recursive Queries Page 16-10

Page 639: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Querying Two Levels of Recursion

Table Flights = flights with no stopovers.Table Flights_1Stop = flights with a single stopover.

To show all flights with either no connections or only one connection, a union of the two tables is needed.

Show all flights with either no connections or one connection originating from LAX.

SELECT * FROM Flights WHERE origin = 'LAX'UNION ALLSELECT * FROM Flights_1Stop;

Things to notice:

Origin Destination Cost--------- ----------------- -----------

LAX BOS 300LAX SFO 90LAX ATL 250LAX BOS 390 (one stopover in Atlanta)LAX CHI 365 (one stopover in San Francisco)

Recursive Queries Page 16-11

Page 640: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a ‘Two Stopover’ Table (1 of 2) Create a new table with all flight plans using two stopovers.

CREATE TABLE Flights_2Stops (Origin, Destination, Cost) AS ( SELECT Flight1.Origin ,Flight2.Destination ,Flight1.Cost+Flight2.Cost AS Price FROM Flights_1Stop Flight1 INNER JOIN Flights Flight2 ON Flight1.Destination = Flight2.Origin AND Flight1.Origin = 'LAX') WITH DATA;

Things to notice: By joining the Flights and the Flights_1Stop tables, we can find flights with one additional stopover.

Recursive Queries Page 16-12

Page 641: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a ‘Two Stopover’ Table (1 of 2)

Create a new table with all flight plans using two stopovers.

CREATE TABLE (Origin, Destination, Cost) AS (SELECT Flight1.Origin

,Flight2.Destination ,Flight1.Cost+Flight2.Cost AS Price

FROM INNER JOIN ON Flight1.Destination = Flight2.OriginAND Flight1.Origin = 'LAX')WITH DATA;

Flights_2Stops

Flights_1Stop Flight1 Flights Flight2

A new table is created by joining the Flights and the Flights_1Stop tables.This new table contains all LAX originating flights with two stopovers.

Things to Notice

Recursive Queries Page 16-13

Page 642: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a ‘Two Stopover’ Table (2 of 2) Now show all flights with two connections

SELECT * FROM Flights_2Stops;

Origin Destination Price ------ ----------- -------- LAX BOS 545

Things to notice: There is only one flight plan that has two stopovers. The plan consists of the following flights:

Origin Destination Cost ------ ----------- ------ LAX SFO 90 SFO CHI 275 CHI BOS 180 ----- 545

Recursive Queries Page 16-14

Page 643: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a ‘Two Stopover’ Table (2 of 2)

Origin Destination Cost--------- ---------------- ---------LAX SFO 90SFO CHI 275CHI BOS 180

-------545

Now, using the table just created, show all LAX flights with two connections.

SELECT * FROM Flights_2Stops;

Origin Destination Price--------- ---------------- --------LAX BOS 545

There is only one flight plan from LAX that has two stopovers.The flight plan consists of the following three flights:

Things to notice:

Recursive Queries Page 16-15

Page 644: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Querying Three Levels of Recursion Show all flights from LAX with up to two stopovers.

SELECT * FROM Flights WHERE origin = 'LAX' UNION ALL SELECT * FROM Flights_1Stop UNION ALL SELECT * FROM Flights_2Stops;

Origin Destination Cost ------ ----------- ----------- LAX BOS 300 LAX SFO 90 LAX ATL 250 LAX BOS 390 (one stopover) LAX CHI 365 (one stopover) LAX BOS 545 (two stopovers)

Things to notice:

• Table Flights = flights with no stopovers. • Table Flights_1Stop = flights with a single stopover. • Table Flights_2Stop = flights with two stopovers. • To get the list of all types of flights, a union of the three sets is performed.

As can be seen from this example, the more levels of recursive information that are needed, the more temporary tables must be created and joined together.

Recursive Queries Page 16-16

Page 645: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Querying Three Levels of Recursion

Table Flights = flights with no stopovers. Table Flights_1Stop = flights with a single stopover. Table Flights_2Stop = flights with two stopovers. To get the list of all types of flights, a union of the three sets is performed.

Show all flights from LAX with up to two stopovers.

SELECT * FROM Flights WHERE origin = 'LAX'UNION ALLSELECT * FROM Flights_1StopUNION ALLSELECT * FROM Flights_2Stops;

Origin Destination Cost--------- ---------------- -----------LAX BOS 300LAX SFO 90LAX ATL 250LAX BOS 390 (one stopover)LAX CHI 365 (one stopover)LAX BOS 545 (two stopovers)

Things to notice:

Recursive Queries Page 16-17

Page 646: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a Recursive Table Returning to the problem of capturing all possible flight plans, we may use a recursive table to solve this problem with a single query as will be seen. The keywords WITH RECURSIVE specify that the table All_Trips is to be a 'recursive table'. In the following example we are only interested in flights originating from LAX with one, two or no stopovers. We can now use this table to produce the same results that previous required multiple tables.

WITH RECURSIVE All_Trips (Origin, Destination, Cost, Depth) AS ( SELECT Origin, Destination, Cost, 0 FROM Flights WHERE Origin = 'LAX' UNION ALL SELECT All_Trips.Origin, Flights.Destination, All_Trips.Cost + Flights.cost, All_Trips.Depth + 1 FROM All_Trips INNER JOIN Flights ON All_Trips.Destination = Flights.Origin AND All_Trips.Depth < 3 ) SELECT * FROM All_Trips;

Origin Destination Cost Depth ------ ----------- ------ ----- LAX BOS 300 0 LAX SFO 90 0 LAX ATL 250 0 LAX CHI 365 1 LAX BOS 390 1 LAX BOS 545 2

Things to notice about this example:

• The table is defined as a recursive table by using the keywords WITH RECURSIVE. • It is first populated with a SELECT of all rows from the Flights table with origin of LAX. • Each row from the original Flights table has a depth of zero (no stopovers). • Additional rows are added to the WITH table as a result of the UNION ALL operator. • The second SELECT joins the existing Flights table with the All_Trips table which is

currently under construction. • When it can match an existing destination with an existing source, it adds a new

flight plan to the All_Trips table. • Each time a new row is added to All_Flights, the possibility exists for a new match

and thus a new flight plan. • It will recursively execute this join operation until it has found all possible flight plans

which have up to 2 (less than 3) stopovers. • A depth limit is usually specified to prevent the possibility of infinite looping. • When the WITH table is completed, the final SELECT against All_Trips may execute.

Recursive Queries Page 16-18

Page 647: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating a Recursive Table

We may use a recursive table

All_Trips(Origin, Destination, Cost, Depth) AS (SELECT Origin, Destination, Cost, 0FROM Flights WHERE Origin = 'LAX'

SELECT All_Trips.Origin,Flights.Destination,All_Trips.Cost + Flights.cost,All_Trips.Depth + 1

FROM All_Trips INNER JOIN FlightsON All_Trips.Destination = Flights.OriginAND All_Trips.Depth < 3 )SELECT * FROM All_Trips;

to solve this problem with a single query as follows. The keywords WITH RECURSIVE specify that the table All_Trips is to be a 'recursive table'.

WITH RECURSIVE

UNION ALLOrigin Destination Cost Depth--------- ---------------- ----------- ----------LAX BOS 300 0LAX SFO 90 0LAX ATL 250 0LAX BOS 390 1 LAX CHI 365 1LAX BOS 545 2

Results

Things to notice about this example:The table is first populated with a SELECT of all rows from the Flights table with origin LAX (depth 0). Additional rows are added to the WITH table as a result of the UNION ALL operator. The second SELECT joins the Flights table with the All_Trips table (currently under construction). For each matching destination and source, it adds a new flight plan to the All_Trips table. For each new row is added to All_Flights, a possibility exists for a new match, i.e., a new flight plan.A depth limit (3) is usually specified to prevent the possibility of infinite looping. When the WITH table is completed, the final SELECT may execute.

Recursive Queries Page 16-19

Page 648: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Recursive Views A recursive view may be created and then SELECTed against. Using the prior example, a view definition is seen here:

CREATE RECURSIVE VIEW All_Trips_View (Origin, Destination, Cost, Depth) AS (SELECT Origin, Destination, Cost, 0 FROM Flights WHERE Origin = 'LAX' UNION ALL SELECT All_Trips_View.Origin, Flights.Destination, All_Trips_View.Cost + Flights.cost, All_Trips_View.Depth + 1 FROM All_Trips_View, Flights WHERE All_Trips_View.Destination = Flights.Origin AND All_Trips_View.Depth < 3 );

The view may then be accessed as with any table or view.

Recursive Queries Page 16-20

Page 649: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating Recursive Views

A recursive view may be created and then SELECTed against. Using the prior example, a view definition is seen here:

CREATE All_Trips_View(Origin, Destination, Cost, Depth) AS(SELECT Origin, Destination, Cost, 0FROM Flights WHERE Origin = 'LAX'

SELECT All_Trips_View.Origin,Flights.Destination,All_Trips_View.Cost + Flights.cost,All_Trips_View.Depth + 1

FROM All_Trips_View, FlightsWHERE All_Trips_View.Destination = Flights.OriginAND All_Trips_View.Depth < 3 );

RECURSIVE VIEW

UNION ALL

The view may then be accessed as with any table or view.Several examples are seen on the next slide.

Recursive Queries Page 16-21

Page 650: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Accessing Via a Recursive View CREATE RECURSIVE VIEW All_Trips_View (Origin, Destination, Cost, Depth) AS (SELECT Origin, Destination, Cost, 0 FROM Flights WHERE Origin = 'LAX' UNION ALL SELECT All_Trips_View.Origin, Flights.Destination, All_Trips_View.Cost + Flights.cost, All_Trips_View.Depth + 1 FROM All_Trips_View, Flights WHERE All_Trips_View.Destination = Flights.Origin AND All_Trips_View.Depth < 3 );

The view may then be accessed as with any table or view.

Show all LAX originating flights with no stopovers.

SELECT * FROM All_Trips_View WHERE Depth = 0;

Origin Destination Cost Depth------ ----------- ----- ----- LAX BOS 300 0 LAX SFO 90 0 LAX ATL 250 0

Show all LAX originating flights with one stopover.

SELECT * FROM All_Trips_View WHERE Depth = 1;

Origin Destination Cost Depth------ ----------- ----- ----- LAX CHI 365 1 LAX BOS 390 1

Show all LAX originating flights with two stopovers.

SELECT * FROM All_Trips_View WHERE Depth = 2;

Origin Destination Cost Depth------ ----------- ---- ----- LAX BOS 545 2

Recursive Queries Page 16-22

Page 651: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Accessing Via a Recursive View

Show all LAX originating flights with no stopovers.

SELECT * FROM All_Trips_View WHERE Depth = 0;

SELECT * FROM All_Trips_View WHERE Depth = 1;

SELECT * FROM All_Trips_View WHERE Depth = 2;

Origin Destination Cost Depth--------- ---------------- ----------- ----------LAX BOS 300 0LAX SFO 90 0LAX ATL 250 0

Origin Destination Cost Depth--------- ---------------- ----------- ----------LAX BOS 390 1 LAX CHI 365 1

Show all LAX originating flights with one stopover.

Show all LAX originating flights with two stopovers.

Origin Destination Cost Depth--------- ---------------- ----------- ----------LAX BOS 545 2

Recursive Queries Page 16-23

Page 652: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Recursive INSERT SELECT WITH tables may also be used in an INSERT SELECT operation as seen here. The processing sequence of this operation is explained in the comments below.

INSERT-SELECT

INSERT INTO Gold_Customers WITH Valued_Customers (a,b,c) AS (SELECT … FROM xyz WHERE ……) SELECT * FROM Valued_Customers WHERE …;

Things to notice:

• This is basically a two-tiered INSERT SELECT. • The goal is to populate the Gold_Customers table. • First, the WITH table (Valued_Customers) is created and populated using the first

SELECT statement. • Next, Valued_Customers is used as the source of the second SELECT statement, used to

populate the Gold_Customers table. • Each of the two WHERE clauses can be used to control which rows will populate the

Gold_Customers table.

Recursive Queries Page 16-24

Page 653: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Recursive INSERT SELECT

• This is basically a two-tiered INSERT SELECT. • The goal is to populate the Gold_Customers table.• First, the WITH table (Valued_Customers) is created and populated using the

first SELECT statement. • Next, Valued_Customers is used as the source of the second SELECT

statement, used to populate the Gold_Customers table.• Each of the two WHERE clauses can be used to control which rows will

populate the Gold_Customers table.

WITH tables may also be used in an INSERT SELECT operation as seen here.

The processing sequence of this operation is explained in the comments below.

INSERT INTO Gold_CustomersWITH Valued_Customers (a,b,c) AS (SELECT a,b,c FROM xyz

WHERE ……)* FROM Valued_Customers

WHERE …;SELECT

Things to notice:

Recursive Queries Page 16-25

Page 654: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Recursive CREATE TABLE WITH tables may also be used in a CREATE TABLE statements as seen here. The processing sequence of this operation is explained in the comments below.

CREATE TABLE Gold_Customers AS WITH Valued_Customers(…) AS (SELECT … FROM xyz WHERE …) (SELECT * FROM Valued_Customers WHERE …) WITH DATA;

Things To Notice:

• First, the WITH table is populated as in the previous example. • Then, Valued_Customer is SELECTed against to populate the target table

Gold_Customers. • This example results in the creation and population of the same table as seen in the

previous example.

Recursive Queries Page 16-26

Page 655: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Recursive CREATE TABLE

• This example results in the creation and population of the same table as seen in the previous example.

• First, the WITH table is populated as before. • Then, Valued_Customer is SELECTed against to populate the target table

Gold_Customers.

WITH tables may also be used in a CREATE TABLE statements as seen here.

The processing sequence of this operation is explained in the comments below.

CREATE TABLE Gold_Customers ASWITH Valued_Customers (a,b,c) AS (SELECT a, b, c FROM xyz WHERE …)(SELECT * FROM Valued_CustomersWHERE …)WITH DATA;

Things To Notice:

Recursive Queries Page 16-27

Page 656: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Recursion Limitations The following are not permitted in conjunction with the use of recursion:

• INTERSECT or MINUS operators (UNION ALL is the only operator permitted for combining segments of a recursive query.)

• The recursive reference may not be an inner table of a LEFT or RIGHT join. Full outer joins are also not allowed.

The following are not allowed in the recursive portion of the query but are allowed in the seeding portion:

• Subqueries and derived tables • Aggregation and OLAP functions • Negation of recursion references (e.g. NOT IN and NOT EXISTS ) • GROUP BY and extended grouping functions • DISTINCT

The following are not allowed at all in a recursive query:

• TOP N function • Cursors • Stored procedures

Additional items not permitted with recursive queries:

• Triggers • Multiple WITH definitions • Nested WITH clauses • WITH clauses within views • Recursive views within views • Recursive views within WITH clauses

Recursive Queries Page 16-28

Page 657: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Recursion Limitations

The following are not permitted in conjunction with the use of recursion: • INTERSECT or MINUS operators (UNION ALL is the only operator permitted for

combining segments of a recursive query). • The recursive reference may not be an inner table of a LEFT or RIGHT join. • Full outer joins are also not allowed.

The following are not allowed in the recursive portion of the query but are allowed in the seeding portion:• Subqueries and derived tables • Aggregation and OLAP functions • Negation of recursion references (e.g. NOT IN and NOT EXISTS ) • GROUP BY and extended grouping functions • DISTINCT

The following are not allowed at all in a recursive query:TOP N function Cursors Stored procedures

Recursive Queries Page 16-29

Page 658: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Traditional Derived Tables Derived tables are a technique for creating a temporary table which exists for the duration of the query. An additional way to implement derived tables is to use a WITH derived table. The following examples will demonstrate the use of a traditional derived table.

What is the average cost of a non-stop flight?

SELECT AVG(Cost) FROM Flights;

Average(Cost)------------- 206

What is the average cost of a one-stop flight?

SELECT AVG(Cost) FROM Flights_1Stop;

Average(Cost)------------- 378

What is the average cost of all non-stop and one-stop flights? A traditional derived table can be used to accomplish this.

SELECT AVG(Costing) FROM ( SELECT Cost FROM Flights UNION ALL SELECT Cost FROM Flights_1Stop ) AllFlights (Costing);

Average(Costing)---------------- 249

Things To Notice: The derived table is defined following the FROM clause. The main query is seen in the first SELECT statement.

Recursive Queries Page 16-30

Page 659: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Traditional Derived Tables

The derived table is defined following the FROM clause.The main query is seen in the first SELECT statement.

Derived tables are a technique for creating a temporary table which exists for the duration of the query.

What is the average cost of a non-stop flight? Average(Cost)---------------------

206SELECT AVG(Cost) FROM Flights;

SELECT AVG(Cost) FROM Flights_1Stop;

SELECT AVG(Costing) FROM(SELECT Cost FROM FlightsUNION ALLSELECT Cost FROM Flights_1Stop) AllFlights (Costing);

Average(Cost)---------------------

378

What is the average cost of a one-stop flight?

What is the average cost of all non-stop and one-stop flights?A single derived table can be used to accomplish this.

Things To Notice:

Average(Costing)-------------------------

249

Recursive Queries Page 16-31

Page 660: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating 'Derived Tables' using WITH. The previous problem might also have been solved with a different type of derived table called a WITH table. The syntax for using WITH for this purpose follows.

What is the average cost of all non-stop and one-stop flights. Use a WITH derived table.

WITH AllFlights(Costing) AS ( SELECT Cost FROM Flights UNION ALL SELECT Cost FROM Flights_1Stop ) SELECT AVG(Costing) FROM AllFlights;

Average(Costing)---------------- 249

Things to notice about this query:

• The WITH table and column names are defined in front of the query. • The SELECT statement in parenthesis defines the content of the WITH table. • The final SELECT statement is the main query against the WITH table.

There are some advantages and disadvantages to using WITH tables in place of traditional derived tables.

WITH Table Advantages: • Allows user to reference the derived WITH table in multiple locations in the query. • Generally makes the query more readable.

WITH Table Disadvantages: • Requires explicit column list – traditional derived tables can inherit the column list from the inner query.

Recursive Queries Page 16-32

Page 661: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Creating ‘Derived Tables' using WITH.

The WITH table and column names are defined in front of the query. The SELECT statement in parenthesis defines the content of the WITH table. The final SELECT statement is the main query against the WITH table.

The previous problem might also have been solved with a different type of derived table called a WITH table. The syntax for this approach follows.

What is the average cost of all non-stop and one-stop flights. Use a WITH derived table.

WITH AllFlights(Costing) AS(SELECT CostFROM Flights

UNION ALLSELECT CostFROM Flights_1Stop)SELECT AVG(Costing) FROM AllFlights;

Average(Costing)-------------------------

249

Things to notice about this query

Recursive Queries Page 16-33

Page 662: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary

• The recursive query feature enables queries against hierarchical data by permitting multiple passes over the rows of a table in a single SQL statement.

• This feature is supported by the following additions to the SQL lexicon: o WITH – Creates a derived table for non-recursive processing. o WITH RECURSIVE – Creates a derived table for recursive processing. o CREATE RECURSIVE VIEW – Creates a view for recursive processing.

• WITH tables may be used in an INSERT SELECT operation.

• WITH tables may also be used in a CREATE TABLE statements

• There are many restrictions on the use of recursive tables o UNION ALL is the only set operator permitted for combining segments of a

recursive query. o May not be used in conjunction with LEFT, RIGHT or FULL OUTER JOIN

syntax. o Triggers, cursors, stored procedures, subqueries and derived tables all have

restrictions when used with recursive queries. o GROUP BY, DISTINCT, TOP N, NOT IN and NOT EXISTS all have

restrictions when used with recursive queries.

• Use of the WITH RECURSIVE clause is an ANSI-99 supported feature

Recursive Queries Page 16-34

Page 663: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Summary

The recursive query feature enables queries against hierarchical data by permitting multiple passes over the rows of a table in a single SQL statement.

This feature is supported by the following additions to SQL:

WITH – Creates a derived table for non-recursive processing. WITH RECURSIVE – Creates a derived table for recursive processing. CREATE RECURSIVE VIEW – Creates a view for recursive processing.

WITH tables may also be used in an INSERT SELECT operation.WITH tables may also be used in a CREATE TABLE statements

• UNION ALL is the only set operator permitted for combining segments of a recursive query.

• May not be used in conjunction with LEFT, RIGHT or FULL OUTER JOIN syntax.

• Triggers, cursors, stored procedures, subqueries and derived tables all have restrictions when used with recursive queries.

• GROUP BY, DISTINCT, TOP N, NOT IN and NOT EXISTS all have restrictions when used with recursive queries.

There are many restrictions on the use of recursive tables

Recursive Queries Page 16-35

Page 664: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise

Recursive Queries Page 16-36

Page 665: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Review Exercise

1. A WITH table may be defined once and referenced multiple times throughout the query.a. Trueb. False

2. If a query depth is not specified in a recursive query, the query is unprotected from infinite looping.a. Trueb. False

3. Which one is not a feature of WITH clause processing?a. Non-recursive derived tables.b. Recursive INSERT/SELECTsc. Recursive viewsd. Iterative control statements

4. Which two of the following are permitted as part of a WITH RECURSIVE statement.a.) UNION ALLb.) OUTER JOINc.) INNER JOINd.) GROUP BY

5. WITH RECURSIVE is an ANSI standard feature of Teradata SQL.a. Trueb. False

Recursive Queries Page 16-37

Page 666: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

Recursive Queries Page 16-38

Page 667: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Lab

1.) Using the employee table, show the reporting structure under manager 801 using the following guidelines. Show only two levels of report structure, i.e., an employee, his boss and his boss's boss (801).

Output four columns of data:

The employeeHis first level managerHis second level manager (null is there is none)The number of levels removed (1 = direct report, 2 = two levels above)

Recursive Queries Page 16-39

Page 668: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Teradata Division Training

Notes

Recursive Queries Page 16-40

Page 669: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Appendix A

Customer Service Database Tables & Data

Customer Service Database - Tables & Data A-1

Page 670: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

CUSTOMER TYPE NULLS SIZE

customer_number I Y -9(10)

customer_name CF N X(30)

parent_customer_number I Y -9(10)

sales_employee_number I Y -9(10)

CONTACT

contact_number I Y -9(10)

contact_name CV N X(30)

area_code I2 N -9(5)

phone I N -9(10)

extension I Y -9(10)

last_call_date DA N YY/MM/DD

DEPARTMENT

department_number I2 Y -9(5)

department_name CF N X(30)

budget_amount D Y -9(9).99

manager_employee_number I Y -9(10)

EMPLOYEE

employee_number I Y -9(10)

manager_employee_number I Y -9(10)

department_number I Y -9(10)

job_code I Y -9(10)

last_name CF N x(20)

first_name CV N X(30)

hire_date DA N YY/MM/DD

birthdate DA N YY/MM/DD

salary_amount D N -(6).99

EMPLOYEE_PHONE

employee_number I N -9(10)

area_code I2 N -9(5)

phone I N -9(10)

extension I Y -9(10)

comment_line CF Y X(72)

JOB TYPE NULLS SIZE

job_code I Y -9(10)

description CV N X(40)

hourly_billing_rate D Y -(5).99

hourly_cost_rate D Y -(5).99

LOCATION

location_number I Y -9(10)

customer_number I N -9(10)

first_address_line CF N X(30)

city CV N X(30)

state CF N X(15)

zip_code I N -9(10)

second_address_line CF Y X(30)

third_address_line CF Y X(30)

LOCATION_EMPLOYEE

location_number I N -9(10)

employee_number I N -9(10)

LOCATION_PHONE

location_number I Y -9(10)

area_code I2 N -9(5)

phone I N -9(10)

extension I Y -9(10)

description CV N X(40)

comment_line CV Y X(32000)

Legend

I integer

I2 small integer

D decimal

DA date

CF character, fixed

CV character, variable

Customer Service Database - Tables & Data A-2

Page 671: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

‘Customer_Service’ Table Definitions

CREATE SET TABLE agent_sales ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( agent_id INTEGER, sales_amt INTEGER) UNIQUE PRIMARY INDEX ( agent_id ); --

CREATE SET TABLE BLOB_files (Presentation_id INTEGER NOT NULL ,Presentation_obj BLOB(102400)) UNIQUE PRIMARY INDEX ( Presentation_id ); --

CREATE TABLE city ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( cityname CHAR(15) NOT CASESPECIFIC, citystate CHAR(2) NOT CASESPECIFIC, citypop INTEGER) PRIMARY INDEX ( cityname ); -- CREATE TABLE clob_files (Id INTEGER NOT NULL, text_file CLOB(10000)) UNIQUE PRIMARY INDEX ( Id ); -- CREATE TABLE contact, FALLBACK (contact_number INTEGER ,contact_name CHAR(30) NOT NULL ,area_code SMALLINT NOT NULL ,phone INTEGER NOT NULL ,extension INTEGER ,last_call_date DATE NOT NULL) UNIQUE PRIMARY INDEX (contact_number); -- CREATE TABLE customer, FALLBACK (customer_number INTEGER ,customer_name CHAR(30) NOT NULL

Customer Service Database - Tables & Data A-3

Page 672: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

,parent_customer_number INTEGER ,sales_employee_number INTEGER ) UNIQUE PRIMARY INDEX (customer_number); -- CREATE TABLE customers ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( cust_id integer not null ,cust_name char(15) ,cust_addr char(25) compress) PRIMARY INDEX ( cust_id); -- CREATE SET TABLE daily_sales ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( itemid INTEGER, salesdate DATE FORMAT 'YY/MM/DD', sales DECIMAL(9,2)) PRIMARY INDEX ( itemid ); -- CREATE SET TABLE daily_sales_2004 ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( itemid INTEGER, salesdate DATE FORMAT 'YY/MM/DD', sales DECIMAL(9,2)) PRIMARY INDEX ( itemid ); -- CREATE TABLE department, FALLBACK (department_number SMALLINT ,department_name CHAR(30) NOT NULL ,budget_amount DECIMAL(10,2) ,manager_employee_number INTEGER )

Customer Service Database - Tables & Data A-4

Page 673: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

UNIQUE PRIMARY INDEX (department_number) ,UNIQUE INDEX (department_name); -- CREATE TABLE employee, FALLBACK (employee_number INTEGER ,manager_employee_number INTEGER ,department_number INTEGER ,job_code INTEGER ,last_name CHAR(20) NOT NULL ,first_name VARCHAR(30) NOT NULL ,hire_date DATE NOT NULL ,birthdate DATE NOT NULL ,salary_amount DECIMAL(10,2) NOT NULL ) UNIQUE PRIMARY INDEX (employee_number); -- CREATE TABLE employee_phone, FALLBACK (employee_number INTEGER NOT NULL ,area_code SMALLINT NOT NULL ,phone INTEGER NOT NULL ,extension INTEGER ,comment_line CHAR(72) ) PRIMARY INDEX (employee_number); -- CREATE SET TABLE Jan_sales ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( itemid INTEGER, salesdate DATE FORMAT 'YY/MM/DD', sales DECIMAL(9,2)) PRIMARY INDEX ( itemid ); -- CREATE TABLE job, FALLBACK (job_code INTEGER ,description VARCHAR(40) NOT NULL ,hourly_billing_rate DECIMAL(6,2) ,hourly_cost_rate DECIMAL(6,2) ) UNIQUE PRIMARY INDEX (job_code) ,UNIQUE INDEX (description);

Customer Service Database - Tables & Data A-5

Page 674: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

-- CREATE TABLE location, FALLBACK (location_number INTEGER ,customer_number INTEGER NOT NULL ,first_address_line CHAR(30) NOT NULL ,city VARCHAR(30) NOT NULL ,state CHAR(15) NOT NULL ,zip_code INTEGER NOT NULL ,second_address_line CHAR(30) ,third_address_line CHAR(30) ) PRIMARY INDEX (customer_number); -- CREATE TABLE location_employee, FALLBACK (location_number INTEGER NOT NULL ,employee_number INTEGER NOT NULL ) PRIMARY INDEX (employee_number); -- CREATE TABLE location_phone, FALLBACK (location_number INTEGER ,area_code SMALLINT NOT NULL ,phone INTEGER NOT NULL ,extension INTEGER ,description VARCHAR(40) NOT NULL ,comment_line LONG VARCHAR ) PRIMARY INDEX (location_number); -- CREATE TABLE orders ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( order_id INTEGER NOT NULL ,order_date DATE FORMAT 'YYYY-MM-DD' ,cust_id INTEGER ,order_status CHAR(1)) UNIQUE PRIMARY INDEX ( order_id ); -- CREATE TABLE phonelist ( LastName CHAR(20), FirstName CHAR(20),

Customer Service Database - Tables & Data A-6

Page 675: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Number CHAR(12) NOT NULL, Photo BLOB(10000)) UNIQUE PRIMARY INDEX ( Number ); -- CREATE TABLE repair_time ( serial_number INTEGER ,product_desc CHAR(8) ,start_time TIMESTAMP(0) ,end_time TIMESTAMP(0)) UNIQUE PRIMARY INDEX (serial_number); -- CREATE SET TABLE salestbl ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( storeid INTEGER, prodid CHAR(1), sales DECIMAL(9,2)) PRIMARY INDEX ( storeid ); -- CREATE TABLE state ,NO FALLBACK , NO BEFORE JOURNAL, NO AFTER JOURNAL ( stateid CHAR(2) NOT CASESPECIFIC NOT NULL, statename CHAR(15) NOT CASESPECIFIC, statepop INTEGER NOT NULL, statecapitol CHAR(15) NOT CASESPECIFIC) PRIMARY INDEX ( stateid );

Contact Table

Contact Contact Area Call Number Name Code Phone Extension Date

8007 Smith, Ginny 408 3792152 333 88 / 08 / 05 8010 Brayton, Connie 408 1112345 112 87 / 07 / 21 8005 Hughes, Jack 212 5432126 710 87 / 08 / 05 8001 Leblanc, James 805 2213456 221 87 / 08 / 01 8008 Torres, Alison 802 5487890 444 88 / 08 / 14 8015 Dibble, Nancy 602 2713387 652 88 / 08 / 09

Customer Service Database - Tables & Data A-7

Page 676: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Customer Table

Parent Sales Customer Customer Customer Employee Number Name Number Number

9 More Data Enterprise ? 1023 18 Wall Street Connection ? 1023 11 Hotel California ? 1015 8 Colby Co. ? 1018 2 Simple Instruments Co. 1 1015 13 First American Bank 3 1023 4 Sum Bank 3 1023 0 Corporate Headquarters ? ? 6 Liberty Tours ? 1023 3 First American Bank ? 1023 7 Cream of the Crop ? 1018 17 East Coast Dating Service ? 1023 12 Cheap Rentals ? 1018 10 Graduates Job Service ? 1015 19 More Data Enterprise 9 1015 14 Metro Savings ? 1018 5 Federal Bureau of Rules ? 1018 1 A to Z Communications, Inc, ? 1015 16 VIP Investments 3 1023 15 Cates Modeling ? 1015 20 Metro Savings 14 1018

Customer Service Database - Tables & Data A-8

Page 677: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Department Table

Manager Department Department Budget Employee Number Name Amount Number

100 president 400000.00 801 600 None ? 1099 403 education 932000.00 1005 301 research and development 465600.00 1005 302 product planning 226000.00 1016 401 customer support 982300.00 1003 201 technical operations 293800.00 1025 402 software support 308000.00 1011 501 marketing sales 308000.00 1017

Customer Service Database - Tables & Data A-9

Page 678: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Employee Table

Manager Employee Employee Department Job Last First N Numb Num Code Name Name

umber er ber

1008 1019 301 312102 Kanieski Carol 1010 1003 401 412101 Rogers Frank 1014 1011 402 422101 Crane Robert 1006 1019 301 312101 Stein John- 1017 801 501 511100 Runyon Irene 1003 801 401 411100 Trader James 1019 801 301 311100 Kubic Ron 1011 801 402 421100 Daly James 1013 1003 401 412102 Phillips Charles 1009 1005 403 432101 Lombardo Domingus 1025 801 201 211100 Short Michael 1024 1005 403 432101 Brown Allen 1007 1005 403 432101 Villegas Arnando 1005 801 403 431100 Ryan Loretta 1021 1025 201 222101 Morrissey Jim 1020 1005 403 432101 Charles John 1022 1003 401 412102 Machado Albert 801 801 100 111100 Trainer I.B. 1015 1017 501 512101 Wilson Edward 1002 1003 401 413201 Brown Alan 1004 1003 401 412101 Johnson Darlene 1016 801 302 321100 Rogers Nora 1018 1017 501 512101 Ratzlaff Larry 1001 1003 401 412101 Hoover William 1023 1017 501 512101 Rabbit Peter 1012 1005 403 432101 Hopkins Paulene

Customer Service Database - Tables & Data A-10

Page 679: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Employee Table Continued

Hire Salary Date Birthdate Amount

77/02/01 58/05/17 29250.00 77/03/01 35/04/23 46000.00 78/01/15 60/07/04 24500.00 76/10/15 53/10/15 29450.00 78/05/01 51/11/10 66000.00 76/07/31 47/06/19 37850.00 78/08/01 42/12/11 57700.00 77/03/15 49/12/11 52500.00 77/04/01 63/08/10 24500.00 77/02/01 45/11/15 31000.00 79/05/01 47/07/07 34700.00 79/05/01 54/01/16 43700.00 77/01/02 37/01/31 49700.00 76/10/15 55/09/10 31200.00 78/10/01 43/04/29 38750.00 78/10/01 49/06/21 39500.00 79/03/01 57/07/14 32300.00 73/03/01 45/08/11 100000.00 78/03/01 57/03/04 53625.00 76/07/31 44/08/09 43100.00 76/10/15 46/04/23 36300.00 78/03/01 59/09/04 56500.00 78/07/15 54/05/31 54000.00 76/06/18 50/01/14 25525.00 79/03/01 62/10/29 26500.00 77/03/15 42/02/18 37900.00

Customer Service Database - Tables & Data A-11

Page 680: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Employee_Phone Table

Employee Area Number Code Phone Extension Comment

1008 213 3788092 ? 1010 202 5456187 ? 1003 213 3774534 ? 1014 213 8278777 442 1006 213 8278777 410 1017 213 8278777 425 1019 213 8278777 418 1011 213 8278777 422 1010 213 8278777 ? 1014 213 2528809 ? 1006 213 3716087 ? 1017 213 9231070 ? 1019 213 2640855 ? 1011 213 3549138 ? 1008 213 8278777 429 1003 213 8278777 401 1010 202 3239119 ? 1007 213 2274764 ? 1005 213 2514189 ? 1013 213 8278777 411 1025 213 8278777 429 1024 213 8278777 417 1021 213 8278777 428 1013 213 9857506 ? 1010 804 9230911 ? 1025 213 2964652 ? 1024 213 2724743 ? 1021 213 2659291 ? 1007 213 8278777 440 1005 213 8278777 415 1010 313 4630300 ? 1002 213 2721606 ? 1004 212 7230101 ? 1022 213 8278777 416 801 213 8278777 100 1015 213 8278777 436 1010 312 990988 ? 1022 213 4982012 ? 1018 202 3239119 ? 1015 213 3012906 ? 1004 212 7232121 ? 1002 213 8278777 439 1018 804 2989791 ? 1015 415 4491225 ? 1023 212 7232121 ? 1010 804 4563000 370 1016 213 8278777 412 1004 609 5591011 213

Customer Service Database - Tables & Data A-12

Page 681: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Employee_Phone Table Continued Employee Area Number Code Phone Extension Comment

1016 213 2925224 ? 1015 415 9237892 ? 1023 212 8283747 ? 1010 804 4563000 375 1012 213 8278777 418 1004 609 5591011 224 1010 312 5692122 ? 1012 213 9788422 ? 1001 415 2412021 ? 1023 609 5591011 213 1004 609 5591011 225 1009 213 2482619 ? 1001 415 3563560 ? 1004 609 5785781 ? 1001 213 2872019 ? 1004 212 5786099 ? 1009 213 8278777 413 1001 415 6567000 ? 1004 919 9789000 ? 1020 213 8278777 433 1020 213 2248513 ? 1001 415 4491221 ? 1004 617 7567676 ? 1001 415 4491225 ? 1004 212 8282828 ? 1001 415 4491244 ? 1004 718 2243283 ? 1001 415 9234864 ? 1004 212 4909190 ? 1001 415 9237892 ?

Customer Service Database - Tables & Data A-13

Page 682: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Job Table

Hourly Hourly Job Billing Cost Code Description Rate Rate

312101 Software-Engineer .00 .00 432101 Instructor .00 .00 413201 Dispatcher .00 .00 111100 Corporate President .00 .00 421100 Manager Software Support .00 .00 104201 Electronic Assembler .00 .00 312102 Hardware Engineer .00 .00 321100 Manager - Product Planning .00 .00 412101 Field Engineer .00 .00 322101 Planning Specialist .00 .00 511100 Manager - Marketing Sales .00 .00 431100 Manager - Education .00 .00 311100 Manager - Reasearch and Development .00 .00 411100 Manager - Customer Support .00 .00 104202 Mechanical Assembler .00 .00 412102 Product Specialist .00 .00 512101 Sales Rep .00 .00 412103 System Support Analyst .00 .00 222101 System Analyst .00 .00 422101 Software Analyst .00 .00

Customer Service Database - Tables & Data A-14

Page 683: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Location Table

Location Customer Number Number First_Address_Line City

5000002 2 49 Fourth St San Francisco 34000009 9 4400 Greenwood Rd Wilmington 33000018 18 105 Time Square New York City 5000011 11 770 Hotel Dr Menlo Park 22000013 13 1059 Kings Rd Boston 31000004 4 55 Madison Av Trenton 23000008 8 1100 State St Detroit 5000000 0 1294 Jefferson Blvd Los Angeles 33000006 6 10 River Rd Schenectady 33000003 3 10366 25th St New York City 5000019 19 567 El Camino Real San Mateo 47000007 7 4035 South 35th Av Arlington 33000017 17 4001 Harbor Blvd Brooklyn 14000012 12 510 Benton Av Chicago 5000010 10 5171 El Camino Real Palo Alto 47000014 14 1690 Miller Av Richmond 9000005 5 1 Lincoln Square Washington 5000001 1 101 Middlefield Rd Palo Alto 33000016 16 2255 16th Av New York City 5000015 15 687 Culver Blvd Culver City 14000020 20 876 Winston St Chicago

Second Third State _LineZip_Code Address_Line Address

California 941031066 NC 284031199 New York 105082682 California 940585151 Massachusetts 12104091 New Jersey 123419199 MI 484107888 California 951604032 New York 123016166 New York 105293045 California 942153219 Virginia 222061016 New York 105431915 Illinois 606483930 California 94071 Virginia 223104121 DC 156075555 California 951604032 New York 105293033 California 900513965 Illinois 606316166

Customer Service Database - Tables & Data A-15

Page 684: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Location_Employee Table

Location Employee Number Number

9000005 1010 5000001 1001 33000006 1004 47000007 1010 5000002 1001 22000013 1004 23000008 1010 5000010 1001 33000017 1004 14000012 1010 5000011 1001 33000018 1004 47000014 1010 5000015 1001 33000016 1004 14000020 1010 33000019 1001 34000009 1004 310000 1004 33000003 1004

Customer Service Database - Tables & Data A-16

Page 685: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Location_Phone Table Location Area Number Code Phone Extension Description *

23000008 313 4630300 ? Receptionist 33000018 212 4909190 ? Tom-Sellers 5000000 213 8278777 101 Corporate President 33000006 212 5786099 ? Small office 22000013 617 7567676 ? Computer Room 14000012 312 9880988 Tom Thumb 5000010 415 2412021 ? Alice Hamm 47000014 804 4563000 370 Alan Monday 5000011 415 3563560 ? J.R. Stern 5000019 415 6567000 ? Receptionist 5000002 415 9237892 ? Receptionist 22000013 617 7562918 ? System Manager 33000003 212 7230101 ? Computer Room 31000004 609 5591011 213 Receptionist 47000014 804 4563000 375 Receptionist 14000020 312 5692122 ? Receptionist 5000015 213 2872019 ? Charles Cates 33000017 718 2243283 ? various contacts 33000003 212 7232121 ? System Manager 9000005 202 3239119 ? Switchboard 31000004 609 5591011 224 System Manager 14000020 312 5692136 ? System Manager 33000016 212 8282828 ? Andy Moore 34000009 919 9789000 ? Receptionist 31000004 609 5591011 225 Computer Room 5000001 415 4491221 ? Fes office 47000007 804 9230911 ? Switchboard 34000009 919 9789000 601 John Moore 5000001 415 4491225 ? System Manager 5000001 415 4491244 ? Secretary * The sixth column in this table is Comment Line, which is not shown above.

Customer Service Database - Tables & Data A-17

Page 686: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

ADDITIONAL STUDENT NOTES

Customer Service Database - Tables & Data A-18

Page 687: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-1

Appendix B

Lab Solutions

Page 688: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-2

Notes

Page 689: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-3

Module 1: Lab 1 Solution (1 of 3)

Lab Solutions B - 3

Window Aggregate Lab Solutions

1.) How many people in each department were born prior to 1950 and who are they? Show last name, first name (reduce both to 10 characters), birth date(as Birthdate), department number (as Dept), and the count of people in that department who qualify (as Dept_Cnt). Order the results by birth date within department.

SELECT last_name (CHAR(10)),first_name (CHAR(10)) ,birthdate ,department_number As Dept,COUNT(*) OVER (PARTITION BY Dept ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

AS Dept_CntFROM employee WHERE birthdate < DATE '1950-01-01' ORDER BY 4,3;

SELECT last_name (CHAR(10)),first_name (CHAR(10)) ,birthdate ,department_number As Dept,COUNT(*) OVER (PARTITION BY Dept ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

AS Dept_CntFROM employee WHERE birthdate < DATE '1950-01-01' ORDER BY 4,3;

last_name first_name birthdate Dept Dept_Cnt-------------- --------------- ------------ ----------- -----------Trainer I.B. 45/08/11 100 1Morrissey Jim 43/04/29 201 2Short Michael 47/07/07 201 2Kubic Ron 42/12/11 301 1Rogers Frank 35/04/23 401 4Brown Alan 44/08/09 401 4Johnson Darlene 46/04/23 401 4Trader James 47/06/19 401 4Daly James 49/12/11 402 1Villegas Arnando 37/01/31 403 4Hopkins Paulene 42/02/18 403 4Lombardo Domingus 45/11/15 403 4Charles John 49/06/21 403 4

13 rows

Page 690: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-4

Module 1: Lab 1 Solution (2 of 3)

Lab Solutions B - 1

Window Aggregate Lab Solutions

SELECT last_name AS Name ,salary_amount AS Salary ,department_number AS Dept ,SUM(salary) OVER (PARTITION BY dept ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

AS Dept_Sum ,COUNT(salary) OVER (PARTITION BY dept ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

AS Dept_Count FROM employee WHERE Dept IN (401,403);

Name Salary Dept Dept_Sum Dept_Count-------------------- ------------ ----------- -------------- -----------------Phillips 24500.00 401 245575.00 7Rogers 46000.00 401 245575.00 7Hoover 25525.00 401 245575.00 7Brown 43100.00 401 245575.00 7Machado 32300.00 401 245575.00 7Johnson 36300.00 401 245575.00 7Trader 37850.00 401 245575.00 7Villegas 49700.00 403 233000.00 6Ryan 31200.00 403 233000.00 6Charles 39500.00 403 233000.00 6Brown 43700.00 403 233000.00 6Lombardo 31000.00 403 233000.00 6Hopkins 37900.00 403 233000.00 6

13 rows

2.) The following SQL statement (not shown) produces a report showing the employees in departments 401 and 403, the subcount of employees in each department, and the subtotals of their salaries by department. Modify this query to produce the same information using the SUM and COUNT Window functions.

Page 691: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-5

Module 1: Lab 1 Solution (3 of 3)

Lab Solutions B - 1

Window Aggregate Lab Solutions

3.) The solution for Lab #2 may be modified to generate this report. Produce a report which shows each employee in departments 401 and 403, their department, their salary, the total of salaries for their department, and the percentage their salary represents of the department total. Sort the output by salary within department.

SELECT last_name (CHAR(10)) AS Name ,department_number AS Dept ,salary_amount AS Salary ,SUM(salary) OVER (PARTITION BY Dept ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)

AS Dept_Sum ,Salary/Dept_Sum * 100 AS Dept_Percent

FROM employee WHERE Dept IN (401,403) ORDER BY 2,3;

Name Dept Salary Dept_Sum Dept_Percent------------ ----------- ------------- --------------- --------------------Phillips 401 24500.00 245575.00 9.98Hoover 401 25525.00 245575.00 10.39Machado 401 32300.00 245575.00 13.15Johnson 401 36300.00 245575.00 14.78Trader 401 37850.00 245575.00 15.41Brown 401 43100.00 245575.00 17.55Rogers 401 46000.00 245575.00 18.73Lombardo 403 31000.00 233000.00 13.30Ryan 403 31200.00 233000.00 13.39Hopkins 403 37900.00 233000.00 16.27Charles 403 39500.00 233000.00 16.95Brown 403 43700.00 233000.00 18.76Villegas 403 49700.00 233000.00 21.33

13 rows

Page 692: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-6

Module 1: Lab 2 Solution (1 of 1)

Lab Solutions B - 1

Window Aggregate Lab Solutions4.) The following query (not shown) and resulting output show a listing of sales revenue for item 10 for the first two months of 1998. It also shows a cumulative sum, a moving sum over three days, a moving difference over three days, and a moving average over three days. This output is generated using the Teradata CSUM, MSUM, MDIFF, and MAVG functions. Convert this query to ANSI standard syntax to produce the same result.SELECT salesdate

,CAST(itemid AS BYTEINT) (FORMAT '99') AS "itemid" ,sales ,SUM(sales) OVER (ORDER BY salesdate ROWS

UNBOUNDED PRECEDING) AS "CSUM" ,SUM(sales) OVER (ORDER BY salesdate ROWS 2

PRECEDING ) AS "MSUM" ,MDIFF(sales, 3, salesdate) AS "MDIFF" ,AVG(sales) OVER (ORDER BY salesdate ROWS 2

PRECEDING ) AS "MAVG" FROM daily_sales WHERE salesdate BETWEEN 980101 AND 980228;

salesdate itemid sales CSUM MSUM MDIFF MAV------------- -------- ----------- ------------ ------------ ----------- -----------98/01/01 10 150.00 150.00 150.00 ? 150.0098/01/02 10 200.00 350.00 350.00 ? 175.0098/01/03 10 250.00 600.00 600.00 ? 200.0098/01/05 10 350.00 950.00 800.00 200.00 266.6798/01/10 10 550.00 1500.00 1150.00 350.00 383.3398/01/21 10 150.00 1650.00 1050.00 -100.00 350.0098/01/25 10 200.00 1850.00 900.00 -150.00 300.0098/01/31 10 100.00 1950.00 450.00 -450.00 150.0098/02/01 10 150.00 2100.00 450.00 .00 150.0098/02/03 10 250.00 2350.00 500.00 50.00 166.6798/02/06 10 350.00 2700.00 750.00 250.00 250.0098/02/17 10 550.00 3250.00 1150.00 400.00 383.3398/02/20 10 450.00 3700.00 1350.00 200.00 450.0098/02/27 10 350.00 4050.00 1350.00 .00 450.00

14 rows

Page 693: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-7

Module 2: Lab 1 Solution (1 of 5)

Lab Solutions B - 1

Ordered Analytic Functions Lab Solutions

1.) Show the salary amounts for each employee in departments 401and 402 in ascending order and show the group sum for each department.

SELECT department_number AS Dept,employee_number AS Emp,salary_amount AS Sal,SUM(Sal) OVER (PARTITION BY Dept ORDER BY Sal DESCROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) WHERE Dept IN (401,402) FROM employee ;

*** Query completed. 9 rows found.

SELECT department_number AS Dept,employee_number AS Emp,salary_amount AS Sal,SUM(Sal) OVER (PARTITION BY Dept ORDER BY Sal DESCROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) WHERE Dept IN (401,402) FROM employee ;

*** Query completed. 9 rows found.

Dept Emp Sal Group Sum(Sal)----------- ------------ ------------ ----------------------

401 1010 46000.00 245575.00401 1002 43100.00 245575.00401 1003 37850.00 245575.00401 1004 36300.00 245575.00401 1022 32300.00 245575.00401 1001 25525.00 245575.00401 1013 24500.00 245575.00402 1011 52500.00 77000.00402 1014 24500.00 77000.00

Page 694: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-8

Module 2: Lab 1 Solution (2 of 5)

Lab Solutions B - 1

Ordered Analytic Function Lab Solutions

SELECT department_number AS Dept, employee_number AS Emp, salary_amount AS Sal,MAX(Sal) OVER (PARTITION BY Dept ORDER BY Sal DESC)WHERE Dept IN (401,402) FROM employee;

*** Query completed. 9 rows found.

2.) Modify the previous query to also show the maximum salary amount in each department.

Dept Emp Sal Group Max(Sal)----------- ------------- --------------- -----------------------

401 1010 46000.00 46000.00401 1002 43100.00 46000.00401 1003 37850.00 46000.00401 1004 36300.00 46000.00401 1022 32300.00 46000.00401 1001 25525.00 46000.00401 1013 24500.00 46000.00402 1011 52500.00 52500.00402 1014 24500.00 52500.00

Page 695: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-9

Module 2: Lab 1 Solution (3 of 5)

Lab Solutions B - 1

Ordered Analytic Functions Lab Solutions

3.) Modify the previous query to show the salary amounts for each employee in departments 401 and 402 and show the cumlative sum of the salary amounts by department.

SELECT department_number AS Dept, employee_number AS Emp, salary_amount AS Sal, SUM(Sal) OVER (PARTITION BY Dept ORDER BY Sal DESCROWS UNBOUNDED PRECEDING) WHERE Dept IN (401,402) FROM employee;

*** Query completed. 9 rows found.

Dept Emp Sal Cumulative Sum(Sal)----------- ----------- -------------- ----------------------------

401 1010 46000.00 46000.00401 1002 43100.00 89100.00401 1003 37850.00 126950.00401 1004 36300.00 163250.00401 1022 32300.00 195550.00401 1001 25525.00 221075.00401 1013 24500.00 245575.00402 1011 52500.00 52500.00402 1014 24500.00 77000.00

Page 696: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-10

Module 2: Lab 1 Solution (4 of 5)

Lab Solutions B - 1

Ordered Analytic Functions Lab Solutions4.) Modify the previous query to show salaries of all employees in

departments 401 and 402 in descending order with a moving average by department using the two preceding rows.

SELECT department_number AS Dept, employee_number AS Emp, salary_amount AS Sal, AVG(Sal) OVER (PARTITION BY Dept ORDER BY Sal DESCROWS 2 PRECEDING)WHERE Dept IN (401,402) FROM employee;

*** Query completed. 9 rows found.

Dept Emp Sal Moving Avg(Sal)----------- -------------- -------------- ---------------------

401 1010 46000.00 46000.00401 1002 43100.00 44550.00401 1003 37850.00 42316.67401 1004 36300.00 39083.33401 1022 32300.00 35483.33401 1001 25525.00 31375.00401 1013 24500.00 27441.67402 1011 52500.00 52500.00402 1014 24500.00 38500.00

Page 697: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-11

Module 2: Lab 1 Solution (5 of 5)

Lab Solutions B - 1

Ordered Analytic Functions Lab Solutions

5.) Modify the previous query to show salaries of all employees in departments in departments 401 and 402 in ascending sequence. Also show the sum of the remaining salaries of all employees below the current row in the hierarchy.

SELECT department_number AS Dept, employee_number AS Emp, salary_amount AS Sal,SUM(Sal) OVER (ORDER BY Sal DESCROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)WHERE Dept IN (401,402) FROM employee ;

*** Query completed. 9 rows found.

Dept Emp Sal Remaining Sum(Sal)----------- ----------- -------------- ---------------------------

401 1013 24500.00 24500.00402 1014 24500.00 49000.00401 1001 25525.00 74525.00401 1022 32300.00 106825.00401 1004 36300.00 143125.00401 1003 37850.00 180975.00401 1002 43100.00 224075.00401 1010 46000.00 270075.00402 1011 52500.00 322575.00

Page 698: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-12

Module 3: Lab Solution (1 of 2)

Lab Solutions B - 1

Numeric Formatting Lab Solutions

a.) $ 982,300.00SELECT budget_amount (FORMAT 'GLZ(I)D9(F)') FROM departmentWHERE department_number = 401;budget_amount---------------------$ 982,300.00

b.) $982,300.00SELECT budget_amount (FORMAT 'GLLZ(I)D9(F)') FROM departmentWHERE department_number = 401;budget_amount---------------------

$982,300.00

The following exercises repeatedly use the following SQL statement with different formats applied to produce different output results:

SELECT budget_amountFROM departmentWHERE department_number = 401;

(It is recommended that these labs be done using BTEQ or BTEQWin. If an ODBC client such as SQL Assistant is used, the projected column must be CAST to CHARACTER with the correct precision, which may require some trial and error.)

Lab 1.) Apply the appropriate format to the budget_amount column to produce the formatting results displayed. Note, in some cases, there may be more than one possible format choice to produce the desired result.

Page 699: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-13

Module 3: Lab Solution (2 of 2)

Lab Solutions B - 1

Numeric Formatting Lab Solutions

c.) 982,300.00 $SELECT budget_amount (FORMAT 'GZ(I)D9(F)BBL') FROM departmentWHERE department_number = 401;budget_amount---------------------

982,300.00 $

d.) 00,982,300.00 US DollarsSELECT budget_amount (FORMAT 'G9(I)D9(F)BN') FROM departmentWHERE department_number = 401;budget_amount-----------------------------------00,982,300.00 US Dollars

e.) USD 982,300.00SELECT budget_amount (FORMAT 'GUZ(I)D9(F)')FROM departmentWHERE department_number = 401;budget_amount------------------------USD 982,300.00

f.) 982300 USDSELECT budget_amount (FORMAT 'Z(I)BU')FROM departmentWHERE department_number = 401;budget_amount----------------------

982300 USD

Page 700: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-14

Module 4: Lab 1 Solution (1 of 2)

Lab Solutions B - 1

Date Lab Solutions1.) Show the total number of years between 2000-01-01 and 2010-12-31.

SELECT (DATE '2010-12-31' - DATE '2000-01-01') YEAR;

(2010-12-31 - 2000-01-01) YEAR --------------------------------------------

10

2.) Show the total number of months between 2000-01-01 and 2010-12-31.

SELECT (DATE '2010-12-31' - DATE '2000-01-01') MONTH(3);

(2010-12-31 - 2000-01-01) MONTH -----------------------------------------------

131

3.) Show the total number of days between 2000-01-01 and 2010-12-31.

SELECT (DATE '2010-12-31' - DATE '2000-01-01') DAY(4);

(2010-12-31 - 2000-01-01) DAY------------------------------------------

4017

4.) Show the total number of years with a month remainder between 2010-12-31 and 2000-01-01.

SELECT (DATE '2010-12-31' - DATE '2000-01-01') YEAR TO MONTH;

(2010-12-31 - 2000-01-01) YEAR TO MONTH -------------------------------------------------------------

10-11

5.) Change one of the dates to make the difference exactly 11 years.

SELECT (DATE '2011-01-01' - DATE '2000-01-01') YEAR TO MONTH;

(2011-01-01 - 2000-01-01) YEAR TO MONTH-------------------------------------------------------------

11-00

Page 701: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-15

Module 4: Lab 1 Solution (2 of 2)

Lab Solutions B - 1

Date Lab Solutions

5.) (Alternate solution)

SELECT (DATE '2010-12-31' - DATE '1999-12-31') YEAR TO MONTH;

(2010-12-31 - 1999-12-31) YEAR TO MONTH -------------------------------------------------------------

11-00

6.) Show the date halfway between 2000-02-15 and 2000-10-15.

SELECT ((DATE '2000-10-15' - DATE '2000-02-15') DAY(3))/2 + DATE '2000-02-15';

(((2000-10-15 - 2000-02-15) DAY/2)+2000-02-15) ------------------------------------------------------------------

00/06/15

7.) Convert the interval 232 months to year/month and display half of the interval.

SELECT CAST(INTERVAL '232' MONTH AS INTERVAL YEAR TO MONTH)/2;

( 232/2) -----------

9-08

8.) Extract the year 9999 days beyond 2000-01-01.

SELECT EXTRACT (YEAR FROM (DATE '2000-01-01' + INTERVAL '9999' DAY));

EXTRACT(YEAR FROM (2000-01-01+ 9999)) -------------------------------------------------------------

2027

Page 702: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-16

Module 4: Lab 2 Solution (1 of 2)

Lab Solutions B - 1

Time Lab Solutions1.) Assume current time is 09:45:10 What time will it be in 900 minutes?

SELECT TIME '09:45:10' + INTERVAL '900' MINUTE;

(09:45:10+ 900) ---------------------

00:45:10

2.) Add 2 hours, 10 minutes and 35.50 seconds to current time (09:45:10) and display the result with 2 point precision.

SELECT CAST((TIME '09:45:10' + INTERVAL '02:10:35.50' HOUR TO SECOND) AS TIME(2));

(09:45:10+ 2:10:35.50)------------------------------

11:55:45.50

3.) How much time is there between the following two times: 10:20:30 and 12:30:45? In hours?

SELECT (TIME '12:30:45' - TIME '10:20:30') HOUR;

(18:30:45 - 10:20:30) HOUR -------------------------------------

2In minutes?

SELECT (TIME '12:30:45' - TIME '10:20:30') MINUTE(3);

(12:30:45 - 10:20:30) MINUTE ---------------------------------------

130In seconds?

SELECT (TIME '12:30:45' - TIME '10:20:30') SECOND(4);

(12:30:45 - 10:20:30) SECOND -----------------------------------------

7815.000000

Page 703: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-17

Module 4: Lab 2 Solution (2 of 2)

Lab Solutions B - 1

Time Lab Solutions4.) How much time is there before midnight tonight? Use actual time.

SELECT (TIME '23:59:59.99' - CURRENT_TIME) HOUR TO MINUTE;

(23:59:59.99 - Current Time) HOUR TO MINUTE ----------------------------------------------------------------

9:18 (Results will vary)

5.) Convert 76 hours to days with an hour remainder.

SELECT CAST((INTERVAL '76' HOUR) AS INTERVAL DAY TO HOUR);

76--------3 04

6.) Convert 1 day, 6 hours and 22 minutes to minutes.

SELECT CAST((INTERVAL '1 06:22' DAY TO MINUTE) AS INTERVAL MINUTE(4));

1 06:22 -----------

1822

7.) What represents the halfway point between 11:30:30 and 18:00:00?

SELECT ((TIME '18:00:00' - TIME '11:30:00') HOUR TO SECOND)/2 + TIME '11:30:00';

(((18:00:00 - 11:30:00) HOUR TO SECOND/2)+11:30:00)------------------------------------------------------------------------------

14:45:00

8.) Extract the hour 1200 minutes from the current time (09:45:10)

SELECT EXTRACT(HOUR FROM (TIME '09:45:10' + INTERVAL '1200' MINUTE));

EXTRACT(HOUR FROM (09:45:10+ 1200))-----------------------------------------------------------

5

Page 704: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-18

Module 5: Lab 1 Solution (1 of 6)

Lab Solutions B - 1

Timestamp Lab Solutions (1 of 6)1.) Assume current timestamp is '2000-06-18 12:30:25’.

Subtract 30 days from this timestamp.

SELECT TIMESTAMP '2003-06-18 12:30:25' - INTERVAL '30' DAY;

(2003-06-18 12:30:25- 30)-----------------------------------

2003-05-19 12:30:25

2.) Subtract one month from this timestamp.

SELECT TIMESTAMP '2003-06-18 12:30:25' - INTERVAL '1' MONTH;

(2003-06-18 12:30:25- 1) ----------------------------------

2003-05-18 12:30:25

3.) Show the elapsed time between the current timestamp '2000-06-18 12:30:25' and a past timestamp '1999-12-25 01:01:01'.

In years?

SELECT (TIMESTAMP '2003-06-18 12:30:25' -TIMESTAMP '2002-12-25 01:01:01') YEAR;

(2003-06-18 12:30:25 - 2002-12-25 01:01:01) YEAR ----------------------------------------------------------------------

0In months?

SELECT (TIMESTAMP '2003-06-18 12:30:25' -TIMESTAMP '2002-12-25 01:01:01') MONTH;

(2003-06-18 12:30:25 - 2002-12-25 01:01:01) MONTH------------------------------------------------------------------------

5

Page 705: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-19

Module 5: Lab 1 Solution (2 of 6)

Lab Solutions B - 2

Timestamp Lab Solutions (2 of 6)

3.) (Cont.)In days?

SELECT (TIMESTAMP '2003-06-18 12:30:25' -TIMESTAMP '2002-12-25 01:01:01') DAY(3);

(2003-06-18 12:30:25 - 2002-12-25 01:01:01) DAY-------------------------------------------------------------------

175

In hours?

SELECT (TIMESTAMP '2003-06-18 12:30:25' -TIMESTAMP '2002-12-25 01:01:01') HOUR(4);

(2003-06-18 12:30:25 - 2002-12-25 01:01:01) HOUR----------------------------------------------------------------------

4211

In days and hours?

SELECT (TIMESTAMP '2003-06-18 12:30:25' -TIMESTAMP '2002-12-25 01:01:01') DAY(3) TO HOUR;

(2003-06-18 12:30:25 - 2002-12-25 01:01:01) DAY TO HOUR----------------------------------------------------------------------------------

175 11

Page 706: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-20

Module 5: Lab 1 Solution (3 of 6)

Lab Solutions B - 3

Timestamp Lab Solutions (3 of 6)

4.) Does 1440 hours after the zero hour of January 1, 2000 take us into Febrary or March? In a separate query, determine which month 1439 hours returns. (Two approaches are shown.)

Using interval addition:

SELECT TIMESTAMP '2000-01-01 00:00:00' + INTERVAL '1440' HOUR;

(2000-01-01 00:00:00+ 1440)----------------------------------------

2000-03-01 00:00:00

SELECT TIMESTAMP '2000-01-01 00:00:00' + INTERVAL '1439' HOUR;

(2000-01-01 00:00:00+ 1439) -----------------------------------------

2000-02-29 23:00:00

Using Extract:

SELECT EXTRACT (MONTH FROM (TIMESTAMP '2000-01-01 00:00:00' + INTERVAL '1440' HOUR));

EXTRACT(MONTH FROM (2000-01-01 00:00:00+ 1440))----------------------------------------------------------------------------

3

SELECT EXTRACT (MONTH FROM (TIMESTAMP '2000-01-01 00:00:00' + INTERVAL '1439' HOUR));

EXTRACT(MONTH FROM (2000-01-01 00:00:00+ 1439))-----------------------------------------------------------------------------

2

Page 707: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-21

Module 5: Lab 1 Solution (4 of 6)

Lab Solutions B - 1

Timestamp Lab Solution (4 of 6)5.) Using the Repair_time table, report a count of how many TV set

repairs were completed in February?

SELECT COUNT(serial_number) FROM Repair_timeWHERE EXTRACT (MONTH FROM end_time) = 2;

Count(serial_number) ------------------------------

6

6.) Produce a report showing each TV by serial number and how long in days, hours and minutes it took to repair the TV?

SELECT serial_number, (end_time - start_time) DAY TO MINUTE AS work_time FROM Repair_time ORDER BY 1;

serial_number work_time ------------------- --------------

100 2 02:50 101 3 03:50 102 1 00:40 103 6 21:20 104 2 23:50 105 2 06:10 106 5 02:50 107 1 20:20 108 2 05:10

7.) What is the average amount of time it takes to repair a TV? Show the answer in days, hours and minutes.

SELECT AVG( (end_time - start_time) day to minute) AS avg_repair_time FROM Repair_time;

avg_repair_time---------------------3 01:40

Page 708: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-22

Module 5: Lab 1 Solution (5 of 6)

Lab Solutions B - 2

Timestamp Lab Solution (5 of 6)8.) Show the serial number and the number of days required for each TV

that took longer than 2 days to repair.

SELECT serial_number, (end_time - start_time) DAY TO MINUTE AS #_DaysHrsMns

FROM Repair_timeWHERE #_DaysHrsMns > INTERVAL '02 00:00' DAY TO MINUTE;

serial_number #_DaysHrsMns-------------------- --------------------

106 5 02:50 101 3 03:50 108 2 05:10 100 2 02:50 104 2 23:50 103 6 21:20 105 2 06:10

7 Rows

Page 709: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-23

Module 5: Lab 1 Solution (6 of 6)

Lab Solutions B - 3

Timestamp Lab Solution (6 of 6)

Optional Lab Exercises (Extra Challenging)

9.) Produce a list which pairs by serial number any two TV’s that were being repaired at the same time.

SELECT a.serial_number, b.serial_numberFROM Repair_time a CROSS JOIN Repair_time bWHERE (a.start_time, a.end_time) OVERLAPS

(b.start_time, b.end_time)AND a.serial_number < b.serial_number;

((100*Count(serial))/cnt) ----------------------------------

33%

10.) What percentage of all TV’s took more than 2 days to repair?

SELECT (100 * COUNT(serial) / cnt) (FORMAT '99%')FROM (SELECT COUNT(*) FROM Repair_time) AS temp1(cnt),(SELECT serial_number, (end_time - start_time) day AS Num_DaysFROM Repair_timeWHERE Num_days > INTERVAL '02' DAY) AS temp2(serial,

Number_days)GROUP BY cnt;

serial_number serial_number ------------------- -------------------

106 107 103 104 104 105

3 Rows

1 Row

Page 710: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-24

Module 5: Lab 2 Solution (1 of 3)

Lab Solutions B - 1

Zone Lab Solutions (1 of 3)1.) Create the following macros:

DATABASE Userid;CREATE MACRO seton AS (ECHO '.SET FOLDLINE ON;';

ECHO '.SET SIDETITLES ON;';); CREATE MACRO setoff AS (ECHO '.SET FOLDLINE OFF;';

ECHO '.SET SIDETITLES OFF;';);

2.) Set the session default time zone to PST (-08:00)SET TIME ZONE INTERVAL -'08:00' HOUR TO MINUTE;

Invoke the seton macro.EXEC seton;

Verify that you time zone is set to -08:00HELP SESSION;

Invoke the setoff macro.EXEC setoff;

3.) Create the following table:CREATE TABLE zone_tbl(loc_id CHAR(3),timestmp_wz TIMESTAMP(0) WITH TIME ZONE);

4.) Insert the following rows into the zone table:Loc-id Timestamp LA 2000-01-01 09:00:00 (default time zone)NY 2000-01-01 09:00:00-05:00LON 2000-01-01 09:00:00+00:00SYD 2000-01-01 09:00:00+10:00LA 2000-01-01 09:30:00-08:00LON 2000-01-01 09:30:00-00:00

INSERT INTO zone_tbl VALUES ('LA', TIMESTAMP '2000-01-01 09:00:00');

INSERT INTO zone_tbl VALUES ('NY', TIMESTAMP '2000-01-01 09:00:00-05:00');

INSERT INTO zone_tbl VALUES ('LON',TIMESTAMP '2000-01-01 09:00:00+00:00');

INSERT INTO zone_tbl VALUES ('SYD',TIMESTAMP '2000-01-01 09:00:00+10:00');

Page 711: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-25

Module 5: Lab 2 Solution (2 of 3)

Lab Solutions B - 2

Zone Lab Solutions (2 of 3)(Solution continued)

INSERT INTO zone_tbl VALUES ('LA',TIMESTAMP '2000-01-01 09:30:00-08:00');

INSERT INTO zone_tbl VALUES ('LON',TIMESTAMP '2000-01-01 09:30:00-00:00');

5.) Select all rows from the table. Order results by the timestamp column. How do the results appear to be sorted?

SELECT * FROM zone_tbl ORDER BY 2;

loc_id timestmp_wz--------- -------------------------------------LON 2000-01-01 09:00:00+00:00 SYD 2000-01-01 09:00:00+10:00 NY 2000-01-01 09:00:00-05:00 LA 2000-01-01 09:00:00-08:00 LON 2000-01-01 09:30:00-00:00 LA 2000-01-01 09:30:00-08:00 (Note: sort ignores zone)

6.) Select the rows of the table again, however, this time cast the timestamp column as a timestamp(0) with no zone. Sort by the recast timestamp column. How do the results appear to be sorted?

SELECT loc_id, cast(timestmp_wz AS TIMESTAMP(0)) FROM zone_tblORDER BY 2;

loc_id timestmp_wz------ ----------------------------SYD 1999-12-31 15:00:00 LON 2000-01-01 01:00:00 LON 2000-01-01 01:30:00 NY 2000-01-01 06:00:00 LA 2000-01-01 09:00:00 LA 2000-01-01 09:30:00 (Note: sort includes implied zone)

)

Page 712: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-26

Module 5: Lab 2 Solution (3 of 3)

Lab Solutions B - 3

Zone Lab Solutions (3 of 3)

7.) Generate the same result as the previous query, but use the AT LOCAL option on the timestamp, instead of casting it.

SELECT loc_id, timestmp_wz AT LOCAL FROM zone_tbl ORDER BY 2;

loc_id timestmp_wz AT LOCAL -------- -----------------------------------------------SYD 1999-12-31 15:00:00-08:00 LON 2000-01-01 01:00:00-08:00 LON 2000-01-01 01:30:00-08:00 NY 2000-01-01 06:00:00-08:00 LA 2000-01-01 09:00:00-08:00 LA 2000-01-01 09:30:00-08:00 (Note: Sort by normalized)

8.) Reset the local session time zone to 00:00.SET TIME ZONE INTERVAL '00:00' HOUR TO MINUTE;

orSET TIME ZONE LOCAL;

9.) Verify the new session default time zone.EXEC seton;HELP SESSION;EXEC setoff;

10.) Resubmit the previous query against the zone table. How are results sorted?

SELECT loc_id, timestmp_wz AT LOCAL FROM zone_tbl ORDER BY 2;

loc_id timestmp_wz AT LOCAL -------- ------------------------------------SYD 1999-12-31 23:00:00+00:00 LON 2000-01-01 09:00:00+00:00 LON 2000-01-01 09:30:00+00:00 NY 2000-01-01 14:00:00+00:00 LA 2000-01-01 17:00:00+00:00 LA 2000-01-01 17:30:00+00:00 (Note: Sort by normalized)

Page 713: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-27

Module 6: Lab 1 Solution (1 of 2)

Lab Solutions B - 1

Date/Time Formatting Lab Solutions

a.) 2003-Apr-01 13:55:18

SELECT CURRENT_TIMESTAMP(FORMAT 'Y4-M3-DDBHH:MI:SS');Current TimeStamp(6)-------------------------------2003-Apr-01 13:55:18

b.) 03-Apr-01 14:33:01.690000SELECT CURRENT_TIMESTAMP(FORMAT 'YY-MMM-DDBHH:MI:SSDS(6)');or SELECT CURRENT_TIMESTAMP(FORMAT 'YY-MMM-DDBHH:MI:SSDS(F)');Current TimeStamp(6)-------------------------------------03-Apr-01 14:33:01.690000

c.) 14:19:59 +00:00SELECT CURRENT_TIMESTAMP(FORMAT 'HH:MI:SSBZ');Current TimeStamp(6)------------------------------

14:19:59 +00:00

1.) Apply the appropriate format to the CURRENT_TIMESTAMP to produce the formatting results displayed. Because current timestamp is used, answer sets values will vary accordingly.

Page 714: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-28

Module 6: Lab 1 Solution (2 of 2)

Lab Solutions B - 2

Date/Time Formatting Lab Solutions

d.) Tuesday, 01 Apr, 2003.

SELECT CURRENT_TIMESTAMP (FORMAT 'E4,BDDBMMM,BY4');Current TimeStamp(6)-------------------------------Tuesday, 01 Apr, 2003

e.) 02:23:49 PM +00:00SELECT CURRENT_TIMESTAMP (FORMAT 'HH:MI:SSBTBZ');Current TimeStamp(6)-------------------------------02:23:49 PM +00:00

f.) +00:00 02h45m28s PMSELECT CURRENT_TIMESTAMP(FORMAT 'ZBHHhMImSSsBT');Current TimeStamp(6)--------------------------------+00:00 02h45m28s PM

Page 715: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-29

Module 7: Lab Solution (1 of 3)

Lab Solutions B - 1

Corr Subqueries Lab Solutions

1.) Use a correlated subquery to produce a report showing employee last name, salary, and department number for all employees whose salary is greater than their department average.Sort by salary within department number.

SELECT last_name,salary_amount,department_number

FROM employee eeWHERE salary_amount >

(SELECT AVG(salary_amount) FROM employee emWHERE ee.department_number =

em.department_number)ORDER BY 3, 2 DESC;

SELECT last_name,salary_amount,department_number

FROM employee eeWHERE salary_amount >

(SELECT AVG(salary_amount) FROM employee emWHERE ee.department_number =

em.department_number)ORDER BY 3, 2 DESC;

13 rows

last_name salary_amount department_number-------------------- -------------------- ---------------------------Morrissey 38750.00 201Kubic 57700.00 301Rogers 46000.00 401Brown 43100.00 401Trader 37850.00 401Johnson 36300.00 401Daly 52500.00 402Villegas 49700.00 403Brown 43700.00 403Charles 39500.00 403Runyon 66000.00 501Ratzlaff 54000.00 501Wilson 53625.00 501

Page 716: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-30

Module 7: Lab Solution (2 of 3)

Lab Solutions B - 2

Corr Subqueries Lab Solutions

SELECT last_name AS last,salary_amount (FORMAT '$,$$$,$99.99') AS sal

,department_number AS dep,avgsal (FORMAT '$,$$$,$99.99')

FROM (SELECT AVG(salary_amount), department_number FROM employee GROUP BY department_number)

my_temp (avgsal, deptno),employee

WHERE sal > avgsalAND dep = deptno

ORDER BY dep, sal DESC;

13 rows

2.) Using a derived table, produce the same report as in Lab 1 but adding an additional column showing the department average after the employee’s salary.

last sal dep avgsal-------------------- ------------------ ----------- -----------------Morrissey $38,750.00 201 $36,725.00Kubic $57,700.00 301 $38,800.00Rogers $46,000.00 401 $35,082.14Brown $43,100.00 401 $35,082.14Trader $37,850.00 401 $35,082.14Johnson $36,300.00 401 $35,082.14Daly $52,500.00 402 $38,500.00Villegas $49,700.00 403 $38,833.33Brown $43,700.00 403 $38,833.33Charles $39,500.00 403 $38,833.33Runyon $66,000.00 501 $50,031.25Ratzlaff $54,000.00 501 $50,031.25Wilson $53,625.00 501 $50,031.25

Page 717: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-31

Module 7: Lab Solution (3 of 3)

Lab Solutions B - 3

Corr Subqueries Lab Solutions

3.) Produce a list of all department numbers which have no employees using NOT IN. Redesign the query using NOT EXISTS to get the same results.

SELECT department_numberFROM departmentWHERE department_number NOT IN (SELECT department_number FROM employee);

*** Query completed. One row found. One column returned.

department_number----------------------------

600

SELECT department_numberFROM department deWHERE NOT EXISTS

(SELECT * from employee EEWHERE ee.department_number = de.department_number);

*** Query completed. One row found. One column returned.

department_number----------------------------

600

Page 718: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-32

Module 7: Lab (Optional) Solution

Lab Solutions B - 1

Corr Subqueries Lab Solutions4.) Modify Lab 1 to produce the same report but adding department name to the listing. Show only the first 13 characters of department name and give the column a title of 'department’. Retain the sort by department number and salary for ease of comparison.

SELECT last_name,salary_amount,e.department_number AS deptno,d.department_name (FORMAT 'X(13) ') AS "department"

FROM employee e INNER JOINdepartment d

ON e.department_number = d.department_numberWHERE salary_amount >

(SELECT AVG(salary_amount) FROM employee emWHERE e.department_number =

em.department_number)ORDER BY 3, 2 DESC;

13 rows

last_name salary_amount deptno department-------------------- ------------------- ------------------ --------------------Morrissey 38750.00 201 technical opeKubic 57700.00 301 research andRogers 46000.00 401 customer suppBrown 43100.00 401 customer suppTrader 37850.00 401 customer suppJohnson 36300.00 401 customer suppDaly 52500.00 402 software suppVillegas 49700.00 403 educationBrown 43700.00 403 educationCharles 39500.00 403 educationRunyon 66000.00 501 marketing salRatzlaff 54000.00 501 marketing salWilson 53625.00 501 marketing sal

Page 719: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-33

Module 8: Lab Solution (1 of 3)

Lab Solutions B - 1

Ranking Functions Lab Solutions

1a.) Rank the top three departments based on their budget_amount. Show department number, budget amount and rank.

SELECT department_number, budget_amount ,RANK( ) OVER (ORDER BY Budget_amount DESC)

FROM departmentQUALIFY RANK(budget_amount) <=3;

department_number budget_amount Rank ---------------------------- ------------------------ -----------

401 982300.00 1 403 932000.00 2 301 465600.00 3

2.) Rank the top three departments in terms of highest total salary amounts. Show department number, total salary amount and rank.

SELECT deptno, salamt,RANK( ) OVER (ORDER BY salamt DESC) AS Rank_Sal

FROM(SELECT department_number, SUM(salary_amount)FROM employee GROUP BY 1) temp(deptno,salamt)

QUALIFY Rank_Sal <= 3;

deptno salamt Rank ----------- --------------- -----------

401 245575.00 1 403 233000.00 2 501 200125.00 3

3 Rows

3 Rows

1b.) Repeat the query using the ROW_NUMBER function in place ofthe RANK function. Is there any difference in the result?

SELECT department_number, budget_amount ,ROW_NUMBER( ) OVER (ORDER BY Budget_amount DESC)

FROM departmentQUALIFY RANK(budget_amount) <=3;(No difference in output)

Page 720: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-34

Module 8: Lab Solution (2 of 3)

Lab Solutions B - 2

Ranking Functions Lab Solutions

SELECT deptno, 'salary' (TITLE ''), salamt AS dollar_amt, RANK( ) OVER (ORDER BY dollar_amt DESC) AS sal_rank

FROM (SELECT department_number, SUM(salary_amount)FROM employee GROUP BY 1) temp(deptno,salamt)

QUALIFY sal_rank <= 3 UNIONSELECT department_number, 'budget', budget_amount,

RANK( ) OVER (ORDER BY budget_amount DESC) AS bud_rankFROM departmentQUALIFY bud_rank <=3ORDER BY 4,2;

4.) Create a merged answer set based on the top three salary departments and the top three budget departments. Use the UNIONoperator to combine the answer sets from labs #2-3 and #2-4.

deptno dollar_amt ranking -------------- --------------- -----------

401 budget 982300.00 1 401 salary 245575.00 1 403 budget 932000.00 2 403 salary 233000.00 2 301 budget 465600.00 3 501 salary 200125.00 3

3.) Rank the bottom three department in terms of total salary_amount. Show department number and total salary amounts.

SELECT deptno, salamt, RANK( ) OVER (ORDER BY salamt DESC) AS Rank

FROM (SELECT department_number, SUM(salary_amount)FROM employee GROUP BY 1) temp(deptno,salamt) QUALIFY RANK( ) OVER (ORDER BY salamt ASC) <= 3;

deptno salamt Rank ----------- ------------ -----------

302 56500.00 8 201 73450.00 7 402 77000.00 6

3 Rows

6 Rows

Page 721: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-35

Module 8: Lab Solution (3 of 3)

Lab Solutions B - 3

Ranking Functions Lab Solutions

5a.) Create a listing of the top three selling products across all stores as found in the 'salestbl'. Show the store id, product id, sales amount, the ranking and the relative ranking in descending order. Use PERCENT_RANK to produce the relative ranking.

SELECT storeid, prodid, sales,RANK() OVER (ORDER BY sales DESC) AS Rank_Sales,PERCENT_RANK() OVER (ORDER BY sales DESC) AS

Pct_Rank_SalesFROM salestblQUALIFY rank_sales <= 3;

storeid prodid sales Rank_Sales Pct_Rank_Sales----------- --------- -------------- ---------------- ----------------------

1001 F 150000.00 1 0.0000001001 A 100000.00 2 0.1000001003 B 65000.00 3 0.200000

5b.) Modify the query in 5a) to produce the same listing in ascending order of relative ranking.

SELECT storeid,prodid,sales ,RANK() OVER (ORDER BY sales DESC) AS Rank_Sales ,PERCENT_RANK() OVER (ORDER BY sales ASC)

AS Pct_Rank_Sales FROM salestblQUALIFY rank_sales <= 3;

storeid prodid sales Rank_Sales Pct_Rank_Sales----------- --------- -------------- ---------------- ----------------------

1001 F 150000.00 1 1.0000001001 A 100000.00 2 0.9000001003 B 65000.00 3 0.800000

3 Rows

3 Rows

Page 722: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-36

Module 9: Lab Solution (1 of 3)

Lab Solutions B - 1

Quantile Lab Solutions

employee_number salary_amount Quantile------------------------ -------------------- ------------

1006 29450.00 19 1008 29250.00 15 1023 26500.00 11 1001 25525.00 7 1014 24500.00 0 1013 24500.00 0

5 Rows

6 Rows

1.) Produce a listing of all employees who are in the top twenty percentileof salary amount. Show the employee number, the salary amount,and quantile.

SELECT employee_number,salary_amount,QUANTILE (100, salary_amount)

FROM employee QUALIFY QUANTILE(100, salary_amount) > 79;

employee_number salary_amount Quantile------------------------- --------------------- -----------

1018 54000.00 80 1016 56500.00 84 1019 57700.00 88 1017 66000.00 92 801 100000.00 96

2.) Modify the query to show the bottom twenty percentile. Order by descending quantile.

SELECT employee_number, salary_amount,QUANTILE (100, salary_amount)

FROM employee QUALIFY QUANTILE(100, salary_amount) < 21ORDER BY 3 DESC;

Page 723: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-37

Module 9: Lab Solution (2 of 3)

Lab Solutions B - 2

Quantile Lab Solutions

SELECT salary_amount,WIDTH_BUCKET(salary_amount,0,50000,5) AS WB

FROM employeeORDER BY 1;

3.) Create a listing of all employee salaries from the employee table, and a partition assignment using the WIDTH_BUCKET function. Use a lower bound of zero(0), and upper bound of 50,000 and a partition count of five(5).

salary_amount WB-------------------- ---------

24500.00 324500.00 325525.00 326500.00 329250.00 329450.00 331000.00 431200.00 432300.00 434700.00 436300.00 437850.00 437900.00 438750.00 439500.00 443100.00 543700.00 546000.00 549700.00 552500.00 653625.00 654000.00 656500.00 657700.00 666000.00 6

100000.00 6

26 Rows

Page 724: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-38

Module 9: Lab Solution (3 of 3)

Lab Solutions B - 3

Quantile Lab Solutions

4.) Produce a distinct list of departments in which employees with the top twenty percentile of salary reside. Do this in a single query.

SELECT DISTINCT(department_number) FROM employee QUALIFY QUANTILE(100, salary_amount) > 79

dept ------------

100 301 302 501

Optional Lab

4 Rows

Page 725: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-39

Module 10: Lab Solution (1 of 5)

Lab Solutions B - 1

Random/Sample Lab Solutions1.) Create a table using the following description:

CREATE TABLE SQL00.empsamp( empno INTEGER, deptno INTEGER, job INTEGER, sampid BYTEINT)PRIMARY INDEX(empno);

INSERT INTO SQL00.empsampSELECT employee_number,

department_number, job_code, SAMPLEID

FROM employee SAMPLE .33, .33, .33;

*** Insert completed. 26 rows added.*** Total elapsed time was 1 second.

SELECT * FROMSQL00.empsamp ORDER BY SAMPID, empno;

empno deptno job sampid----------- ----------- ----------- ---------

1001 401 412101 1 1002 401 413201 1 1003 401 411100 1 1006 301 312101 1 1007 403 432101 1 1011 402 421100 1 1012 403 432101 1 1013 401 412102 1 1022 401 412102 1 801 100 111100 2 1004 401 412101 2 1010 401 412101 2 1015 501 512101 2 1018 501 512101 2 1019 301 311100 2 1020 403 432101 2 1021 201 222101 2 1025 201 211100 2 1005 403 431100 3 1008 301 312102 3 1009 403 432101 3 1014 402 422101 3 1016 302 321100 3 1017 501 511100 3 1023 501 512101 3 1024 403 432101 3

26 Rows

Populate this table with three samples from the employee table, identifiable by sample ids 1, 2 and 3. Let each sample contain 33%of the rows of the table. Select against the popu-lated table and order by results by sampleid.

(Results will vary)

(Results will vary)

Page 726: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-40

Module 10: Lab Solution (2 of 5)

Lab Solutions B - 2

Random/Sample Lab Solutions

empno deptno job sampid----------- ----------- ----------- ---------1002 401 413201 1 1003 401 411100 1 1004 401 412101 1 1007 403 432101 1 1008 301 312102 1 1010 401 412101 1 1011 402 421100 1 1013 401 412102 1 1014 402 422101 1 1015 501 512101 1 1016 302 321100 1 1018 501 512101 1 1019 301 311100 1 1021 201 222101 1 1022 401 412102 1 801 100 111100 2

1001 401 412101 2 1005 403 431100 2 1006 301 312101 2 1009 403 432101 2 1012 403 432101 2 1017 501 511100 2 1020 403 432101 2 1023 501 512101 2 1024 403 432101 21025 201 211100 2

DELETE FROM SQL00.empsamp;

INSERT INTO SQL00.empsampSELECT employee_number, department_number, job_code, SAMPLEIDFROM employee SAMPLE15, 15, 15;

***Insert completed. 26 rows added. ***Warning: 7473 Requested sample is larger than table rows. All rows returned

SELECT * FROM SQL00.empsampORDER BY SAMPID, empno;

26 Rows

2.) Delete the rows from empsamp and repopulate the table with three samples of 15 rows each. Select the resulting table and order by Sample-id to see the results of this sampling.

(Results will vary)

Page 727: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-41

Module 10: Lab Solution (3 of 5)

Lab Solutions B - 3

Random/Sample Lab Solutions

empno deptno job sampid------------- ----------- ------------- ---------

1012 403 432101 11014 402 422101 11018 501 512101 11021 201 222101 11023 501 512101 11008 301 312102 21010 401 412101 21021 201 222101 21022 401 412102 21023 501 512101 21006 301 312101 31007 403 432101 31015 501 512101 31021 201 222101 31022 401 412102 3

DELETE FROM SQL00.empsamp;

INSERT INTO SQL00.empsampSELECT employee_number, department_number, job_code, SAMPLEIDFROM employee SAMPLE WITH REPLACEMENT5, 5, 5;

SELECT * FROM SQL00.empsampORDER BY SAMPID, empno;

15 Rows

2b.) Delete the rows from empsamp and repopulate the table with three samples of 5 rows each using replacement sampling. Select the resulting table and order by Sample-id. Notice any duplicated employees.

(Results will vary)

Page 728: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-42

Module 10: Lab Solution (4 of 5)

Lab Solutions B - 4

Random/Sample Lab Solutions

INSERT INTO test_data SELECT agent_id

,RANDOM(50000,100000) FROM agent_sales SAMPLE .10;

*** Insert completed. 23 rows added. *** Total elapsed time was 1 second.

3.) Create a table as follows:

CREATE TABLE userid.test_data(agent_id INTEGER ,sales_amt DEC(8,2)) ;

Populate the columns as follows:

Agent_id - Select a 10% random sample of the 234 agent_ids in the agent_sales table.

Sales_amt - Generate a random sales amount between $50,000 and $100,000 for each agent.

Hint: This can be done in a single step using both the RANDOM and SAMPLE functions.

Page 729: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-43

Module 10: Lab Solution (5 of 5)

Lab Solutions B - 1

Random/Sample Lab Solutions

SELECT Range, Count(Range) FROM (SELECT CASE WHEN sales_amt BETWEEN 50000 and 59999 THEN '$50,000 - $59,999'WHEN sales_amt BETWEEN 60000 and 69999 THEN '$60,000 - $69,999'WHEN sales_amt BETWEEN 70000 and 79999 THEN '$70,000 - $79,999' WHEN sales_amt BETWEEN 80000 and 89999 THEN '$80,000 - $89,999' WHEN sales_amt BETWEEN 90000 and 100000 THEN '$90,000 - $100,000'

ELSE 'Out of Range' END

FROM test_data) temp (Range)GROUP BY 1ORDER BY 1;

4.) Select the rows of the table to validate your sample. Show how many agents in the sample are in each of the $10,000 ranges.

Range Count (Range)------------------------- -------------------$50,000 - $59,999 4$60,000 - $69,999 6$70,000 - $79,999 3$80,000 - $89,999 6$90,000 - $100,000 4

5 Rows

SELECT * FROM test_data ORDER BY 1;

23 Rows

(Results will vary)

agent_id sales_amt------------- ---------------

5 58241.006 78855.0012 70822.0014 71789.0027 78726.0054 75150.0069 60825.0072 64769.0076 52231.00

103 62392.00: :

Page 730: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-44

Module 11: Lab 1 Solution (1 of 4)

Lab Solutions B - 1

Adding Table Constraints

1.) CREATE an employee table in your own database identical to the employee table in the Customer_Service database. Use the 'SHOW TABLE Customer_Service .employee’ command to assist you in creating the new table in your own database. Call the new tableemp_new. Populate the table using INSERT SELECT using rows from the Customer_Service.employee table.

2a.) Create a department table identical to the department table in the Customer_Service database. Use the same approach as in Lab 5_1,however, assign a primary key constraint on the department_numbercolumn instead of specifying an index. Call the primary key constraint 'primary_1’ and call the new table 'dept_new’. Also, remove the index specified for department_name. Do a SHOW TABLE on 'dept_new’ to see how the constraint is implemented.

DATABASE sqlnn; (where nn is your assigned user number)CREATE TABLE emp_new ,FALLBACK

,NO BEFORE JOURNAL,NO AFTER JOURNAL

(employee_number INTEGER,manager_employee_number INTEGER,department_number INTEGER,job_code INTEGER,last_name CHAR(20) NOT NULL,first_name VARCHAR(30) NOT NULL,hire_date DATE NOT NULL,birthdate DATE NOT NULL,salary_amount DECIMAL(10,2) NOT NULL)

UNIQUE PRIMARY INDEX (employee_number);*** Table has been created.

INSERT INTO emp_new SELECT * FROM Customer_Service.employee;

*** Insert completed. 26 rows added.

CREATE TABLE dept_new, FALLBACK,NO BEFORE JOURNAL, NO AFTER JOURNAL

(department_number SMALLINT NOT NULLCONSTRAINT primary_1 PRIMARY KEY,

department_name CHAR (30) NOT CS NOT NULL,budget_amount DEC (10,2),manager_employee_number INT);

Page 731: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-45

Module 11: Lab 1 Solution (2 of 4)

Lab Solutions B - 1

Adding Table Constraints2a.) Continued

SHOW TABLE dept_new;

*** Text of DDL statement returned.*** Total elapsed time was 1 second.

CREATE SET TABLE SQL00.dept_new ,FALLBACK ,NO BEFORE JOURNAL,NO AFTER JOURNAL(department_number SMALLINT,department_name CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC

NOT NULL,budget_amount DECIMAL(10,2),manager_employee_number INTEGER)

UNIQUE PRIMARY INDEX ( department_number );

2b.) Alter dept_new to add a uniqueness constraint on the department_name column. Name the constraint 'unique_nm’. Do a SHOW TABLE to see how it is implemented.

ALTER TABLE dept_new ADD CONSTRAINT unique_nm UNIQUE (department_name);

SHOW TABLE dept_new;

CREATE SET TABLE SQL00.dept_new ,FALLBACK ,NO BEFORE JOURNAL,NO AFTER JOURNAL(department_number SMALLINT,department_name CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC

NOT NULL,budget_amount DECIMAL(10,2),manager_employee_number INTEGER)

UNIQUE PRIMARY INDEX ( department_number )UNIQUE INDEX unique_nm ( department_name );

Page 732: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-46

Module 11: Lab 1 Solution (3 of 4)

Lab Solutions B - 1

Adding Table Constraints2c.) Alter dept_new to add a check constraint to insure that the department_number is greater than 99. Name the constraint 'hundred_plus’. Do a SHOW TABLE to see how it is implemented.ALTER TABLE dept_newADD CONSTRAINT hundred_plus CHECK (department_number > 99);

SHOW TABLE dept_new;

CREATE SET TABLE SQL00.dept_new ,FALLBACK ,NO BEFORE JOURNAL,

NO AFTER JOURNAL(department_number SMALLINT,department_name CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC

NOT NULL,budget_amount DECIMAL(10,2),manager_employee_number INTEGER,

CONSTRAINT hundred_plus CHECK ( department_number > 99 ))UNIQUE PRIMARY INDEX ( department_number )UNIQUE INDEX unique_nm ( department_name );

2d.) Alter dept_new to add a references constraint on manager_employee_number. Name the constraint 'mgr_ref’ and make it reference the employee_number column of your emp_newtable. Do a SHOW TABLE to see how it is implemented.

ALTER TABLE dept_new ADD CONSTRAINT mgr_ref FOREIGN KEY (manager_employee_number) REFERENCES Customer_Service.employee (employee_number);

SHOW TABLE dept_new;

CREATE SET TABLE SQL00.dept_new1 ,FALLBACK ,NO BEFORE JOURNAL, NO AFTER JOURNAL( department_number SMALLINT NOT NULL,

department_name CHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC NOT NULL,budget_amount DECIMAL(10,2),

manager_employee_number INTEGER,CONSTRAINT hundred_plus CHECK ( department_number > 99 ),CONSTRAINT mgr_ref FOREIGN KEY ( manager_employee_number )

REFERENCES SQL00.EMP_NEW ( employee_number ))UNIQUE PRIMARY INDEX primary_1 ( department_number )UNIQUE INDEX unique_nm ( department_name );

Page 733: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-47

Module 11: Lab 1 Solution (4 of 4)

Lab Solutions B - 1

Adding Table Constraints

3.) Do a HELP CONSTRAINT on each of the named constraints from Lab 2.

.SET FOLDLINE ON; /* Give each column value a new line */

.SET SIDETITLES ON; /* Put titles adjacent to value */

HELP CONSTRAINT dept_new.unique_nm;

*** Help information returned. One row.*** Total elapsed time was 1 second.

Name UNIQUE_NMType UNIQUE

Unique? YIndex Id 4

Column Names DEPARTMENT_NAME

HELP CONSTRAINT dept_new.hundred_plus;

*** Help information returned. One row.*** Total elapsed time was 1 second.

Name HUNDRED_PLUSType CHECKConstraint CONSTRAINT "hundred_plus" CHECK ( "department_number" > 99 )

HELP CONSTRAINT dept_new.mgr_ref;

*** Help information returned. One row.*** Total elapsed time was 1 second.

Name MGR_REFType REFERENCEState VALID

Index Id 0Foreign Key Columns MANAGER_EMPLOYEE_NUMBER

Parent DB Name CUSTOMER_SERVICEParent Table Name EMPLOYEE

Parent Index Id 1Parent Key Columns EMPLOYEE_NUMBER

Page 734: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-48

Module 11: Lab 2 Solution

Lab Solutions B - 1

Adding Table Constraints

4.) Use INSERT SELECT to populate the dept_new table with rows from the department table in Customer_Service. Does it succeed or fail? Why?

INSERT INTO dept_new SELECT * FROM Customer_Service.department; *** Failure 2700 Referential constraint violation: invalid Foreign Key value.

5.) Add a WHERE clause to the INSERT SELECT in the preceding lab which eliminates the row causing the failure.

INSERT INTO dept_new SELECT * FROM department WHERE department.manager_employee_number = employee.employee_number;

*** Insert completed. 8 rows added.

6.) Attempt to add the following rows to the dept_new table.

Dept # dept name budget manager99 ‘new dept’ 900000 1021400 ‘education’ 900000 1021400 ‘new dept’ 900000 1099400 ‘new dept’ 900000 1021

Which succeed, which fail, and why?

INSERT INTO dept_new VALUES (99, 'new dept', 900000,1021);

*** Failure 5317 Check constraint violation:

INSERT INTO dept_new VALUES (400, 'education', 900000,1021);

*** Failure 2803 Secondary index uniqueness violation

INSERT INTO dept_new VALUES (400, 'new dept', 900000,1099);

*** Failure 2700 Referential constraint violation:

INSERT INTO dept_new VALUES (400, 'new dept', 900000,1021);

*** Insert completed. One row added.

7.) Drop any tables you have created during this lab exercise.

Page 735: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-49

Module 11: Lab (Optional) Solution (1 of 4)

Lab Solutions B - 1

Adding Table Constraints

8.) Do a SHOW TABLE on the 'employee_phone’ table in the Customer_Service database. Modify the source code by adding null compression on the 'comment’ column. Submit the modified source to create this table in your own database.Populate the table with the data from the source table. Select all rows from the table which have 'office’ in the comment column and where the extension is not null.

SHOW TABLE Customer_Service.employee_phone;

CREATE SET TABLE Customer_Service.employee_phone ,FALLBACK ,NO BEFORE JOURNAL,NO AFTER JOURNAL,CHECKSUM = DEFAULT(employee_number INTEGER NOT NULL,area_code SMALLINT NOT NULL,phone INTEGER NOT NULL,extension INTEGER,comment_line CHAR(72) CHARACTER SET LATIN NOT CASESPECIFIC)

PRIMARY INDEX ( employee_number );

CREATE SET TABLE employee_phone ,FALLBACK ,NO BEFORE JOURNAL,NO AFTER JOURNAL,CHECKSUM = DEFAULT(employee_number INTEGER NOT NULL,area_code SMALLINT NOT NULL,phone INTEGER NOT NULL,extension INTEGER,comment_line CHAR(72) CHARACTER SET LATIN NOT CASESPECIFIC

COMPRESS)PRIMARY INDEX ( employee_number );

INSERT INTO employee_phone SELECT * FROM Customer_Service.employee_phone;

*** Insert completed. 78 rows added.

Optional Lab

Page 736: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-50

Module 11: Lab (Optional) Solution (2 of 4)

Lab Solutions B - 1

Adding Table Constraints

8.) Cont.

SELECT * FROM employee_phoneWHERE comment_line = 'office'AND extension IS NOT NULL;

*** Query completed. 20 rows found. 5 columns returned.*** Total elapsed time was 1 second.

employee_number area_code phone extension comment_line------------------------- ---------------- ----------- ------------- ------------------------

1021 213 8278777 428 office1007 213 8278777 440 office1012 213 8278777 418 office1019 213 8278777 418 office1016 213 8278777 412 office1014 213 8278777 442 office1009 213 8278777 413 office1024 213 8278777 417 office1008 213 8278777 429 office1013 213 8278777 411 office1006 213 8278777 410 office1003 213 8278777 401 office1015 213 8278777 436 office1005 213 8278777 415 office1025 213 8278777 429 office1011 213 8278777 422 office1022 213 8278777 416 office1002 213 8278777 439 office1020 213 8278777 433 office1017 213 8278777 425 office

Page 737: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-51

Module 11: Lab (Optional) Solution (3 of 4)

Lab Solutions B - 1

Adding Table Constraints

Optional Lab

9a.) Alter your employee_phone table to compress the values 'office’ and 'residence’ in the comment column. What steps are necessary to make this change? After the modification is complete, do a SHOW TABLE to validate the table definition.

ALTER TABLE employee_phone ADD comment_line1 CHAR(72) COMPRESS ('office', 'residence');

*** Table has been modified.

UPDATE employee_phone SET comment_line1= comment_line;

*** Update completed. 78 rows changed.

ALTER TABLE employee_phone DROP comment_line;

*** Table has been modified.

ALTER TABLE employee_phone RENAME comment_line1 TO comment_line;

*** Table has been modified.

SHOW TABLE employee_phone;

CREATE SET TABLE SQL100.employee_phone ,FALLBACK ,NO BEFORE JOURNAL,NO AFTER JOURNAL,CHECKSUM = DEFAULT( employee_number INTEGER NOT NULL,area_code SMALLINT NOT NULL,phone INTEGER NOT NULL,extension INTEGER,comment_line CHAR(72) CHARACTER SET LATIN NOT CASESPECIFIC

COMPRESS('office=','residence '))PRIMARY INDEX ( employee_number );

ALTER TABLE employee_phone ADD comment_line COMPRESS ('office','residence');*** Failure 3558 Cannot alter the specified attribute(s) for COMMENT_LINE.

Page 738: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-52

Module 11: Lab (Optional) Solution (4 of 4)

Lab Solutions B - 1

Adding Table Constraints

9b.) Resubmit the query to select all rows which have 'office’ in the comment column and where the extension is not null.Compare the results to lab 5_4 and verify that the table contents have not changed.

SELECT * FROM employee_phoneWHERE comment_line = 'office'AND extension IS NOT NULL;

*** Query completed. 20 rows found. 5 columns returned.*** Total elapsed time was 1 second.

employee_number area_code phone extension comment_line------------------------- ---------------- ----------- ------------- ------------------------

1021 213 8278777 428 office1007 213 8278777 440 office1012 213 8278777 418 office1019 213 8278777 418 office1016 213 8278777 412 office1014 213 8278777 442 office1009 213 8278777 413 office1024 213 8278777 417 office1008 213 8278777 429 office1013 213 8278777 411 office1006 213 8278777 410 office1003 213 8278777 401 office1015 213 8278777 436 office1005 213 8278777 415 office1025 213 8278777 429 office1011 213 8278777 422 office1022 213 8278777 416 office1002 213 8278777 439 office1020 213 8278777 433 office1017 213 8278777 425 office

Page 739: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-53

Module 12: Lab Solution (1 of 7)

Lab Solutions B - 1

Temp Tables Lab Solutions

1.) Create a temporary table called gt_deptsal, which preserves rows on commit and which does not use the LOG option. The table should follow the specifications as follows:

deptno smallint (as Unique Primary Index)avgsal dec(9,2)maxsal dec(9,2)minsal dec(9,2)sumsal dec(9,2)empcnt smallint)

CREATE GLOBAL TEMPORARY TABLE SQL00.gt_deptsal, NO LOG(deptno SMALLINT,avgsal DEC(9,2),maxsal DEC(9,2),minsal DEC(9,2),sumsal DEC(9,2),empcnt SMALLINT)ON COMMIT PRESERVE ROWS;

2.) Populate the table by insert selecting from the ‘emp’ view in the CS_Views database to create the appropriate aggregates.

INSERT INTO SQL00.gt_deptsal SELECT dept, AVG(sal) ,MAX(sal) ,MIN(sal) ,SUM(sal) ,COUNT(emp)FROM emp GROUP BY 1;(8 Rows inserted)

Page 740: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-54

Module 12: Lab Solution (2 of 7)

Lab Solutions B - 1

Temp Tables Lab Solutions

3.) Create a volatile table as follows:

CREATE VOLATILE TABLE SQL00.vt_emp_job_dept, NO LOG(empno INT,deptname CHAR(15),job_desc CHAR(15))UNIQUE PRIMARY INDEX (empno)ON COMMIT PRESERVE ROWS;

4.) Populate this denormalized table by insert selecting from an outer join of the job table, the department table and the emp view. Make sure there is a row for each employee in the table.

INSERT INTO SQL00.vt_emp_job_deptSELECT emp, department_name, descriptionFROM (emp LEFT OUTER JOIN departmentON dept = department_number)LEFT OUTER JOIN jobON job = job_code;(26 rows inserted)

Page 741: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-55

Module 12: Lab Solution (3 of 7)

Lab Solutions B - 1

Temp Tables Lab Solutions5.) Show the department name and the job description of the highest

paid person in each department. Do not show employee id or salary. Use both the temporary and the volatile tables to solve this. Order results by department name.

SELECT deptname, job_descFROM SQL00.vt_emp_job_dept INNER JOIN empON emp = empnoINNER JOIN SQL00.gt_deptsalON dept = deptno AND sal = maxsalORDER BY 1; deptname job_desc

---------------------- -----------------------customer suppor Field Engineer education Instructor marketing sales Manager - Markepresident Corporate Presiproduct plannin Manager - Produresearch and de Manager - Reseasoftware suppor Manager - Softwtechnical opera System Analyst

SELECT department_name, avgsal, empcntFROM department INNER JOIN SQL00.gt_deptsalON department_number = deptnoAND empcnt < 6 QUALIFY QUANTILE (100, avgsal) >= 50;

(Alternate solution)SELECT department_name, avgsal, empcntFROM department INNER JOIN SQL00.gt_deptsalON department_number = deptnoWHERE empcnt < 6 QUALIFY QUANTILE (100, avgsal) >= 50;

department_name avgsal empcnt-------------------------- ----------- ----------marketing sales 51274.37 4 product planning 56500.00 1 president 100000.00 1

6.) Show the departments in the top 50% of average salary with head counts of less than six. Show department name, average salary and head count

8 Rows

3 Rows

Page 742: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-56

Module 12: Lab Solution (4 of 7)

Lab Solutions B - 1

Temp Tables Lab Solutions7a.) Put an index on the avgsal column of materialized gt_deptsal.

CREATE INDEX (avgsal) ON TEMPORARY SQL00.gt_deptsal;

b.) Confirm that the index was created.

SHOW TEMPORARY TABLE SQL00.gt_deptsal;

or

HELP TEMPORARY INDEX SQL00.gt_deptsal;

c.) Confirm that the index does not exist on the base definition

SHOW TABLE SQL00.gt_deptsal;or

HELP INDEX SQL00.gt_deptsal;

CREATE SET GLOBAL TEMPORARY TABLE gt_deptsal ,NO FALLBACK , NO LOG

(deptno SMALLINT, avgsal DECIMAL(9,2), maxsal DECIMAL(9,2), minsal DECIMAL(9,2), sumsal DECIMAL(9,2), empcnt SMALLINT)

PRIMARY INDEX ( deptno ) INDEX ( avgsal )ON COMMIT PRESERVE ROWS;

Primary or Unique? Secondary Column Names ----------- --------------- --------------------N P deptnoN S avgsal

CREATE SET GLOBAL TEMPORARY TABLE gt_deptsal ,NO FALLBACK , NO LOG

(deptno SMALLINT, avgsal DECIMAL(9,2), maxsal DECIMAL(9,2), minsal DECIMAL(9,2), sumsal DECIMAL(9,2), empcnt SMALLINT)

PRIMARY INDEX ( deptno ) ON COMMIT PRESERVE ROWS;

Primary or Unique? Secondary Column Names ----------- --------------- --------------------N P deptno

Page 743: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-57

Module 12: Lab Solution (5 of 7)

Lab Solutions B - 1

Temp Tables Lab Solutions7d.) Alter the global table to delete rows on commit. What happens?

ALTER TABLE SQL00.gt_deptsal, ON COMMIT DELETE ROWS;

*** Failure 5343 This statement is not allowed when the target temporary table is already materialized

e.) Collect statistics on both indexes of your materialized table.

COLLECT STATISTICS ON TEMPORARY SQL00.gt_deptsal INDEX (deptno);COLLECT STATISTICS ON TEMPORARY SQL00.gt_deptsal INDEX (avgsal);

f.) Look at the statistics.

HELP TEMPORARY STATISTICS SQL00.gt_deptsal;

g.) Drop the statistics on the avgsal index.

DROP STATISTICS ON TEMPORARY SQL00.gt_deptsal INDEX (avgsal);

h.) Look at the statistics.

HELP TEMPORARY STATISTICS SQL00.gt_deptsal;

i.) Set up the base table so that future instances will be defined for statistics on the deptno index. What happens?

COLLECT STATISTICS ON SQL00.gt_deptsal INDEX (deptno);

*** Failure 5343 This statement is not allowed when the target temporary table is already materialized

Date Time Unique Values Column Names ----------- ------------ -------------------- ---------------------02/03/20 16:31:51 8 deptno02/03/20 16:31:48 8 avgsal

Date Time Unique Values Column Names ----------- ------------ -------------------- ---------------------02/03/20 16:31:51 8 deptno

Page 744: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-58

Module 12: Lab Solution (6 of 7)

Lab Solutions B - 1

Temp Tables Lab Solutions

m.) Materialize a new instance of the table (insert select).

INSERT INTO SQL00.gt_deptsal SELECT dept ,AVG(sal) ,MAX(sal) ,MIN(sal) ,SUM(sal) ,COUNT(emp)FROM emp GROUP BY 1;(8 rows inserted)

n.) See if the materialized columns are defined for statistics collection.

HELP TEMPORARY STATISTICS SQL00.gt_deptsal;

o.) Collect statistics on the materialized table.

COLLECT STATISTICS ON TEMPORARY SQL00. gt_deptsal;

p.) Verify that they were collected.

HELP TEMPORARY STATISTICS SQL00.gt_deptsal;Date Time Unique Values Column Names ------------ ----------- -------------------- ---------------------02/03/20 16:38:32 8 deptno

7j.) Drop the materialized table.

DROP TEMPORARY TABLE SQL00.gt_deptsal;

k.) Attempt again to set up the base table according to step i.

COLLECT STATISTICS ON SQL00.gt_deptsal index (deptno);

l.) Verify that the base table is defined for statistics collecting.

HELP STATISTICS SQL00.gt_deptsal;Date Time Unique Values Column Names ----------- ------------ -------------------- ----------------------02/03/20 16:35:14 0 deptno

***Failure 3624 There are no statistics defined for the table.

Page 745: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-59

Module 12: Lab Solution (7 of 7)

Lab Solutions B - 1

Temp Tables Lab Solutions

9.) Show the dept name and the job description of the 6 lowest salaried people in the company. Do not show employee id or salary. Order by department name.

SELECT deptname, job_descFROM emp INNER JOIN

SQL00.gt_deptsalON dept = deptnoINNER JOIN SQL00.vt_emp_job_deptON emp = empnoQUALIFY RANK( ) OVER (ORDER BY sal ASC) <= 6ORDER BY 1; (6 rows selected)

deptname job_desc---------------------- ---------------customer suppor Product Special customer suppor Field Engineer marketing sales Sales Rep research and de Software Engine research and de Hardware Engine software suppor Software Analys

8.) Show the department name and job description of the people in the 75 percentile of salaries in the company. Also show salary amount and quantile but do not show employee id. Use the temporary and volatile tables previously created as well as the emp view. Use default sequencing.

SELECT deptname, job_desc,sal, quantile(100, sal)

FROM emp INNER JOIN SQL00.gt_deptsalON dept = deptnoINNER JOIN SQL00.vt_emp_job_deptON emp = empnoQUALIFY QUANTILE(100,sal) >= 75;

deptname job_desc sal Quantile--------------------- --------------- ---------------- -----------marketing sales Sales Rep 54000.00 76 product plannin Manager - Produ 56500.00 80 research and de Manager - Resea 57700.00 84 marketing sales Sales Rep 58597.48 88 marketing sales Manager - Marke 56000.00 92 president Corporate Presi 100000.00 96

Optional Lab Exercises

6 Rows

6 Rows

Page 746: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-60

Module 13: Lab Solution (1 of 5)

Lab Solutions B - 1

CREATE TABLE Lab Solutions

DATABASE sqlxxxx;

CREATE TABLE emp_sal(empno UNIQUE NOT NULL,lastnm,weekly_sal)

AS (SELECT employee_number (SMALLINT),last_name (CHAR(10))

,salary_amount/52FROM Customer_Service.employeeWHERE department_number = 403) WITH DATA;

SHOW TABLE emp_sal;

CREATE SET TABLE sql00.emp_sal ,NO FALLBACK ,NO BEFORE JOURNAL,NO AFTER JOURNAL(empno SMALLINT NOT NULL,lastnm CHAR(10) CHARACTER SET LATIN NOT CASESPECIFIC,weekly_sal DECIMAL(10,2))

UNIQUE PRIMARY INDEX ( empno );

1.) Create and populate a table based on the 'employee' table containing rows for the employees in department 403 only, and consisting of the following three columns.

• Employee number - renamed as 'empno', converted to a SMALLINT and with a UNIQUE constraint

• Employee last name - renamed as 'lastnm', converted to a CHAR(10) • Employee weekly salary - renamed as 'weekly_sal'

After successful table creation, SHOW the table and SELECT all rows, ordering by employee number.

Page 747: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-61

Module 13: Lab Solution (2 of 5)

Lab Solutions B - 1

CREATE TABLE Lab Solutions

empno lastnm weekly_sal---------- -------------- ---------------

1005 Ryan 600.001007 Villegas 955.771009 Lombardo 596.151012 Hopkins 728.851020 Charles 759.621024 Brown 840.38

6 Rows

1) ContinuedSELECT * FROM emp_sal ORDER BY 1;

Page 748: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-62

Module 13: Lab Solution (3 of 5)

Lab Solutions B - 1

CREATE TABLE emp_max_sals(dept,maxsal TITLE 'Max//Sal')

AS (SELECT department_number,MAX(salary_amount)

FROM Customer_Service.employeeGROUP BY 1) WITH DATA;

SHOW TABLE emp_max_sals;

CREATE SET TABLE sql00.emp_max_sals ,NO FALLBACK ,NO BEFORE JOURNAL,NO AFTER JOURNAL(dept INTEGER,maxsal DECIMAL(10,2) TITLE 'Max//Sal')

PRIMARY INDEX ( dept );

SELECT * FROM emp_max_sals ORDER BY 1;

CREATE TABLE Lab Solutions

2.) Create and populate a table which shows the highest salary in each department. Rename the department number as 'dept' and the maximum salary as 'maxsal'. Also, specify a default stacked title of 'Max//Sal'. This may be accomplished with a single statement which selects from the 'employee' table. SHOW the table and SELECT all rows to verify your results.

dept Max Sal------ --------------100 100000.00201 38750.00301 57700.00302 56500.00401 46000.00402 52500.00403 49700.00501 66000.00

8 Rows

Page 749: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-63

Module 13: Lab Solution (4 of 5)

Lab Solutions B - 1

CREATE TABLE Lab Solutions

CREATE TABLE emp_age(empno,deptname,age)

AS (SELECT e.employee_number,d.department_name,(DATE - e.birthdate) YEAR

FROM Customer_Service.employee e INNER JOIN Customer_Service.department dON e.department_number = d.department_number) WITH DATA;

SHOW TABLE emp_age;

CREATE SET TABLE sql00.emp_age ,NO FALLBACK ,NO BEFORE JOURNAL,NO AFTER JOURNAL

(empno INTEGER,deptname CHAR(30) CHARACTER SET LATIN NOT

CASESPECIFIC NOT NULL,age INTERVAL YEAR(2))

PRIMARY INDEX ( empno );

3.) Create and populate a table called 'emp_age' which contains the following columns:

Employee number (named 'empno')Department name (named 'deptname') Employee's age named 'age’

To create this table, a join between the 'employee' and 'department' table will be needed. Upon successful creation, SHOW the table and SELECT all employees whose age is greater than 50. Order the results by employee number.

Page 750: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-64

Module 13: Lab Solution (5 of 5)

Lab Solutions B - 1

CREATE TABLE Lab Solutions

3) ContinuedSELECT * FROM emp_age WHERE age > INTERVAL '50’ YEARORDER BY 1;

empno deptname age----------- ---------------------------------- ----

801 president 561001 customer support 521002 customer support 571003 customer support 541004 customer support 551007 education 651009 education 561010 customer support 661011 software support 521012 education 601019 research and development 591020 education 521021 technical operations 581025 technical operations 54

Results will vary over time

Page 751: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-65

Module 14: Lab 1 Solution (1 of 2)

Lab Solutions B - 1

Extended Grouping Functions

1a.) Create the equivalent of the following query using GROUPINGSETS syntax. Verify that the output is identical.

SELECT department_number,job_code,SUM(salary_amount)

FROM employeeWHERE department_number < 402GROUP BY ROLLUP (department_number, job_code)ORDER BY 1,2;

SELECT department_number,job_code,SUM(salary_amount)FROM employeeWHERE department_number < 402GROUP BY GROUPING SETS ((department_number, job_code), ( ), department_number)ORDER BY 1,2;

*** Query completed. 17 rows found. 3 columns returned.

department_number job_code Sum(salary_amount)--------------------------- --------------- --------------------------------

? ? 591925.00100 ? 100000.00100 111100 100000.00201 ? 73450.00201 211100 34700.00201 222101 38750.00301 ? 116400.00301 311100 57700.00301 312101 29450.00301 312102 29250.00302 ? 56500.00302 321100 56500.00401 ? 245575.00401 411100 37850.00401 412101 107825.00401 412102 56800.00401 413201 43100.00

Page 752: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-66

Module 14: Lab 1 Solution (2 of 2)

Lab Solutions B - 1

Extended Grouping Functions

SELECT CASE GROUPING (department_number)WHEN 1 THEN ’All Depts' ELSE (COALESCE (department_number,'Null Dept') ) END AS "Dept Number"

,CASE GROUPING (job_code)WHEN 1 THEN ’All Jobs' ELSE (COALESCE (job_code,'Null Job') ) END AS "Job Code"

,SUM(salary_amount) AS "Sum Salaries"FROM employeeWHERE department_number < 402GROUP BY GROUPING SETS ((department_number, job_code), ( ), department_number)ORDER BY 1,2;

*** Query completed. 17 rows found. 3 columns returned. Dept Number Job Code Sum Salaries------------------ ------------- ------------------

100 111100 100000.00100 All Jobs 100000.00201 211100 34700.00201 222101 38750.00201 All Jobs 73450.00301 311100 57700.00301 312101 29450.00301 312102 29250.00301 All Jobs 116400.00302 321100 56500.00302 All Jobs 56500.00401 411100 37850.00401 412101 107825.00401 412102 56800.00401 413201 43100.00401 All Jobs 245575.00

All Depts All Jobs 591925.00

1b.) Using the GROUPING and COALESCE functions, remove all ‘?’s in this report.

Page 753: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-67

Module 14: Lab 2 Solution (1 of 3)

Lab Solutions B - 1

Extended Grouping Functions

SELECT department_number,job_code,SUM(salary_amount)FROM employeeWHERE department_number < 302GROUP BY GROUPING SETS ((department_number, job_code), ( ), department_number, job_code)ORDER BY 1,2;

2a.) Create the equivalent of the following query using GROUPING SETS syntax. Verify that the output is identical.

SELECT department_number,job_code,SUM(salary_amount)FROM employeeWHERE department_number < 302GROUP BY CUBE (department_number,job_code)ORDER BY 1,2;

*** Query completed. 16 rows found. 3 columns returned.

department_number job_code Sum(salary_amount)--------------------------- ------------- ----------------------------

? ? 289850.00? 111100 100000.00? 211100 34700.00? 222101 38750.00? 311100 57700.00? 312101 29450.00? 312102 29250.00100 ? 100000.00100 111100 100000.00201 ? 73450.00201 211100 34700.00201 222101 38750.00301 ? 116400.00301 311100 57700.00301 312101 29450.00301 312102 29250.00

Page 754: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-68

Module 14: Lab 2 Solution (2 of 3)

Lab Solutions B - 1

Extended Grouping Functions

SELECT CASE GROUPING (department_number)WHEN 1 THEN 'All Depts' ELSE (COALESCE (department_number,'Null Dept') ) END AS "Dept Number"

,CASE GROUPING (job_code)WHEN 1 THEN 'All Jobs' ELSE (COALESCE (job_code,'Null Job') ) END AS "Job Code"

,SUM(salary_amount) AS "Sum Salaries"FROM employeeWHERE department_number < 302GROUP BY CUBE (department_number,job_code)ORDER BY 1,2;

*** Query completed. 16 rows found. 3 columns returned. Dept Number Job Code Sum Salaries------------------ ------------- --------------------

100 111100 100000.00100 All Jobs 100000.00201 211100 34700.00201 222101 38750.00201 All Jobs 73450.00301 311100 57700.00301 312101 29450.00301 312102 29250.00301 All Jobs 116400.00

All Depts 111100 100000.00All Depts 211100 34700.00All Depts 222101 38750.00All Depts 311100 57700.00All Depts 312101 29450.00All Depts 312102 29250.00All Depts All Jobs 289850.00

2b.) Using the GROUPING and COALESCE functions, remove all ‘?’s in this report.

Page 755: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-69

Module 14: Lab 2 Solution (3 of 3)

Lab Solutions B - 1

Extended Grouping Functions

3.) What is the equivalent of the following GROUP BY clause? Express it in a single GROUPING SETS clause.

GROUP BY GROUPING SETS (department_number),GROUPING SETS (job_code, manager_employee_number),GROUPING SETS ( )

GROUP BY GROUPING SETS (department_number, job_code ), (department_number, manager_employee_number)

Page 756: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-70

Module 15: Lab 1 Solution (1 of 2)

Lab Solutions B - 1

Merge INTO

1.) Create a copy of this table and its data in your own database. Be sure that you have set your default database to your own 'wbtnnn' database id. Following the creation and populating of the table, drop the unique secondary index on the 'description' column. CREATE TABLE job AS Customer_Service.job WITH DATA;

DROP INDEX (description) ON job;

The following table exists in the Customer Service database and is used in this lab.

CREATE SET TABLE job(job_code INTEGER,description VARCHAR(40) NOT NULL,hourly_billing_rate DECIMAL(6,2),hourly_cost_rate DECIMAL(6,2))UNIQUE PRIMARY INDEX ( job_code )UNIQUE INDEX ( description );

Page 757: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-71

Module 15: Lab 1 Solution (2 of 2)

Lab Solutions B - 1

Merge INTO

2.) Create a macro called 'job_merge' which does the following: • Accepts input parameters: job INT, bill_rate DEC(6,2), cost_rateDEC(6,2)

• If the input job code exists, update the bill and cost rates using the input parameter values.

• If the input job code does not exist, insert a new row for this job code using the input parameter values and specifying a job description of 'new job'.

• Selects the row that has been updated or inserted for display.

CREATE MACRO job_merge (job INT, bill_rate DEC(6,2), cost_rate DEC(6,2)) AS (MERGE INTO jobUSING VALUES (:job, :bill_rate, :cost_rate) AS job_temp (job, bill, cost)ON job.job_code = job_temp.jobWHEN MATCHED THEN UPDATESET hourly_billing_rate= job_temp.bill,hourly_cost_rate = job_temp.costWHEN NOT MATCHED THEN INSERTVALUES (job_temp.job, 'new job',job_temp.bill,job_temp.cost);

SELECT job_code AS Job, description (CHAR(10))AS JobName, hourly_billing_rate AS Bill_Rate, hourly_cost_rate AS Cost_RateFROM jobWHERE job_code = :job;);

Page 758: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-72

Module 15: Lab 2 Solution

Lab Solutions B - 1

Merge INTO

3.) Using the macro, add a new row with the following input values:

job = 104200bill_rate = 26.00cost_rate = 13.00

Using the macro, update an existing row with the following inputvalues:

job = 104201bill_rate = 24.00cost_rate = 12.00

(Note: Teradata SQL Assistant will indicate that tworows have been processed for each request.)

EXEC job_merge (104200, 26.00, 13.00);Job JobName Bill_Rate Cost_Rate

----------- ---------- --------- ---------104200 new job 26.00 13.00

EXEC job_merge ( 104201, 24.00, 12.00);

Job JobName Bill_Rate Cost_Rate----------- ---------- --------- ---------

104201 Electronic 24.00 12.00

4.) Select all rows from the table, ordering by job code.

SELECT * FROM job ORDER BY 1;

Verify the number of rows in the table and the number rows in each of the following ranges:

100000's - 4 rows200000's - 1 rows300000's - 5 rows400000's - 9 rows500000's - 2 rows--------------------------

21 rows

Page 759: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-73

Module 15: Lab 3 Solution (1 of 3)

Lab Solutions B - 1

Merge INTO

(BTEQ method)

5.) Set up an export script to extract all job codes from this table into a file.

.EXPORT data file = job_dataSELECT job_code FROM job;.EXPORT RESET ;

Note: If for any reason you need to recreate this file, you may do so by using this command to remove the file while in BTEQ:

.OS rm job_data ;

(Teradata SQL Assistant method)

5.) Set up an the export export feature to extract all job codes from this table into a file.

Click on File --> Export Results

Submit the following query.SELECT job_code FROM job;When prompted, enter the filename ‘job_data’ for the export file.

Once the file has been exported, you will need to remove the first row of the file, which is the column title ‘job_code’. You can do this using Notepad.

Click on File--> Export Results again to reset Export mode.

Page 760: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-74

Module 15: Lab 3 Solution (2 of 3)

Lab Solutions B - 1

Merge INTO

CREATE MACRO job_merge_2 (job_in INT) AS (MERGE INTO jobUSING VALUES ( :job_in) AS job_temp (job)ON job.job_code = job_temp.jobWHEN MATCHED THEN UPDATESET hourly_billing_rate= hourly_billing_rate + 1.25,hourly_cost_rate =hourly_cost_rate + 1.25WHEN NOT MATCHED THEN INSERTVALUES (job_temp.job, 'new job',20.00,20.00););

6.) Create a macro called 'job_merge_2' which does the following:

• Accepts a single input parameter:( job INTEGER).

• If the input job code exists, update the bill and cost rates byadding 1.25 to the existing rate.

• If the input job code does not exist, insert a new row for this job code using the input parameter job code, specifying a job description of 'new job' and providing bill and cost rates of 20.00.

7.) Delete rows from the job table with job codes between 400000 and 499999.

DELETE FROM job WHERE job_code BETWEEN 400000 and 499999;(9 rows deleted)

Create a script which reads each job code from the job_data file and inputs it to the macro.

(BTEQ Method)

.IMPORT DATA FILE = job_data;

.REPEAT *USING (jobnum INT) EXEC job_merge_2 (:jobnum);

Page 761: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-75

Module 15: Lab 3 Solution (3 of 3)

Lab Solutions B - 1

Merge INTO7.) (Cont.)

(Teradata SQL Assistant Method)Click on File --> Import Data

Submit the following query.EXEC job_merge_2 (?);

When prompted, enter the filename ‘job_data’ for the import file.

Click on File--> Import Data again to reset Import mode.

8.) Verify the results of the script execution. You should have reinserted the 9 rows previously deleted, and each of the existing rows should have its rate updated.

SELECT * FROM job ORDER BY 1;

job_code description hourly_billing_rate----------- ---------------------------------- -------------------

104200 new job 27.25104201 Electronic Assembler 25.25104202 Mechanical Assembler 1.25111100 Corporate President 1.25222101 System Analyst 1.25311100 Manager - Research and Development 1.25312101 Software Engineer 1.25312102 Hardware Engineer 1.25321100 Manager - Product Planning 1.25322101 Planning Specialist 1.25411100 new job 20.00412101 new job 20.00412102 new job 20.00412103 new job 20.00413201 new job 20.00421100 new job 20.00422101 new job 20.00431100 new job 20.00432101 new job 20.00511100 Manager - Marketing Sales 1.25512101 Sales Rep 1.25

Page 762: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-76

Module 16: Lab Solution (1 of 4)

Lab Solutions B - 1

Recursive Queries

1.) Using the employee table, show the reporting structure undermanager 801 using the following guidelines. Show only two levels of report structure, i.e., an employee, his boss and his boss's boss (801).

Ouput four columns of data:The employeeHis first level managerHis second level manager (null is there is none)The number of levels removed (1 = direct report, 2 = two levels above)

Solution a.

WITH RECURSIVE Employee_Boss(Employee,Boss1,Boss2,Levels_Removed) AS(SELECT Employee_number,Manager_employee_number,NULL,1FROM EmployeeWHERE manager_employee_number = 801UNION ALLSELECT Employee.employee_number,Employee.manager_employee_number,801,Employee_Boss.Levels_Removed + 1FROM Employee_Boss, EmployeeWHERE Employee_Boss.Employee =Employee.manager_employee_numberAND Employee.manager_employee_number NOT = 801 AND employee_Boss.Levels_Removed < 2)SELECT * FROM Employee_BossORDER BY 3,2,1;

(Output on next page.)

Page 763: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-77

Module 16: Lab Solution (2 of 4)

Lab Solutions B - 1

Recursive Queries

Employee Boss1 Boss2 Levels_Removed----------- ----- ------ --------------

801 801 ? 11003 801 ? 11005 801 ? 11011 801 ? 11016 801 ? 11017 801 ? 11019 801 ? 11025 801 ? 11001 1003 801 21002 1003 801 21004 1003 801 21010 1003 801 21013 1003 801 21022 1003 801 21007 1005 801 21009 1005 801 21012 1005 801 21020 1005 801 21024 1005 801 21014 1011 801 21015 1017 801 21018 1017 801 21023 1017 801 21006 1019 801 21008 1019 801 21021 1025 801 2

Page 764: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-78

Module 16: Lab Solution (3 of 4)

Lab Solutions B - 1

Recursive Queries

Things to notice about this lab solution:

• It is first populated with a SELECT of all rows from the Employee table whose boss is 801.

• Each of these employees is one level removed from 801. • These employees are also managers with (in most cases) people working for

them. • Additional rows are added to the WITH table as a result of the UNION ALL

operator. • The second SELECT joins the existing Employee table with the Employee_Boss

table which is currently under construction. • When it matches a manager with an employee, it adds a level 2 row to the

Employee_Boss table. • Each time a new row is added to Employee_Boss, the possibility exists for a new

match and thus a new manager/employee relationship. • It will recursively execute this join operation until it has found all possible

relationships. • For this reason a limit of two levels is used, otherwise the table would grow

infinitely. • Also, manager 801 is removed from further join consideration, since his direct

relationships are already accounted for. • When the WITH table is completed, the final SELECT may execute. • Levels_removed is the value stored in the constructed table. • Thus, the check for < 2 will stop additional iterations at the start of the third

iteration.

Page 765: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-79

Module 16: Lab Solution (4 of 4)

Lab Solutions B - 1

Recursive Queries

Solution b.

WITH RECURSIVE Employee_Boss(Employee,Boss1,Boss2,Levels_Removed) AS(SELECT Employee_number,Manager_employee_number,NULL,1FROM EmployeeWHERE manager_employee_number = 801UNION ALLSELECT Employee.employee_number,Employee.manager_employee_number,801,Employee_Boss.Levels_Removed + 1FROM Employee_Boss INNER JOIN EmployeeON Employee_Boss.Employee =Employee.manager_employee_numberAND Employee.manager_employee_number NOT = 801 AND employee_Boss.Levels_Removed < 2)SELECT * FROM Employee_BossORDER BY 3,2,1;

Page 766: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Lab Solutions B-80

Notes

Page 767: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Appendix C

Review Exercise Answers

Answers to Review Exercises C-1

Page 768: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Notes

Answers to Review Exercises C-2

Page 769: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Table of Contents

Module 1: Review Exercise ................................................................ 4

Module 2: Review Exercise ................................................................ 5

Module 3: Review Exercise ................................................................ 6

Module 4: Review Exercise ................................................................ 7

Module 5: Review Exercise ................................................................ 8

Module 6: Review Exercise ................................................................ 9

Module 9: Review Exercise .............................................................. 10

Module 10: Review Exercise ............................................................ 11

Module 11: Create Table Exercise ................................................... 12

Module 11: Review Exercise ............................................................ 13

Module 12: Volatile Table Quiz ........................................................ 14

Module 12: Review Exercise ............................................................ 15

Module 14: Grouping Exercise ......................................................... 16

Module 16: Review Exercise ............................................................ 17

Answers to Review Exercises C-3

Page 770: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 1: Review Exercise

Review Exercise

List 1

A.) UNBOUNDED PRECEDING

B.) UNBOUNDED FOLLOWING

C.) BOTH

D.) NEITHER

List 2

A.) PARTITION BY

B.) ORDER BY

C.) NEITHER

For each function listed below, pick two appropriate coding choices, one each from List 1 and List 2.

List 1 List 2

Cumulative Sum __A_____ ___B___

Moving Sum __D______ ___B___

Moving Average __D______ ___B___

Moving Difference __D______ ___C___

Final Totals __C______ ___C___

Sub Totals __C______ ___A___

Answers to Review Exercises C-4

Page 771: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 2: Review Exercise

Review Exercise

ChoicesUP - UNBOUNDED PRECEDING

UF - UNBOUNDED FOLLOWING

UB - UNBOUNDED

For each type of function listed below, identify which coding choices, by their absence or presence, will activate the function.

Present Present Absent

Cumulative Window ___UP___ ________ ___UF___

Moving Window ________ ________ ___UB___

Group Window ___UP___ ___UF___ ________

Remaining Window ___UF___ ________ ___UP___

Answers to Review Exercises C-5

Page 772: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 3: Review Exercise

Review Exercise

Match the appropriate FORMAT character with its definition.

G - GROUP SEPARATOR

D - RADIX

F - Fractional portion of numeric data.

I - INTEGER portion of numeric data.

Z - Zero-suppressed decimal digit.

L - Fixed currency symbol.

O - DUAL CURRENCY symbol.

N - FULL CURRENCY NAME.

A - FULL DUAL CURRENCY NAME.

C - ISO currency symbol.

U - Dual ISO currency symbol.

A

C

D

F

G

I

L

O

N

U

Z

Answers to Review Exercises C-6

Page 773: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 4: Review Exercise

Review Exercises

1.) What time will it be in 3 hours and 20 minutes? _H___

2.) What hour will it be in 3 hours and 20 minutes? _H,G_

3.) What will the date be in 6 months? _B___

4.) How much time before midnight? _I___

5.) What's the date in 10 days? _C___

6.) How many years are there in 500 months? _A___

7.) What year will it be in 500 months? _C,G_

8.) Combine the time spent on the two projects. _F___

9.) How many days till Christmas? _D___

10.) What's the halfway point of the project timeline? _E___

Match the technique from the lettered list with the appropriate question from the numbered list.

a.) CAST

b.) ADD_MONTHS

c.) DATE INTERVAL

d.) DATE - DATE

e.) INTERVAL */ n

f.) INTERVAL INTERVAL

g.) EXTRACT

h.) TIME INTERVAL

i.) TIME - TIME

_+

_+

_+

Answers to Review Exercises C-7

Page 774: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 5: Review Exercise

Review Exercises

a.) TIMESTAMP INTERVAL

b.) TIMESTAMP - TIMESTAMP

c.) CAST

d.) CREATE/MODIFY USER

e.) SET TIME ZONE

f.) EXTRACT

g.) HELP SESSION

h.) TIMESTAMP

i.) TIMESTAMP WITH TIME ZONE

j.) OVERLAPS

1.) Do two time intervals include the same date?_J__

2.) How many hours passed since this timestamp was taken?_B___

3.) What is the default time zone setting for this session?__G__

4.) Show me the timestamps with zone implicit in the time.__C,H__

5.) What will be the date in 1000 hours?__A__

6.) Set the time zone to be EST.__D,E__

7.) Show me the local time and time zone for each entry.__F,I__

8.) Display a list of all time zones for which we have activity.__F,I__

Match the appropriate technique from the lettered list with the appropriate question from the numbered list. Some questions may have more than one answer.

Answers to Review Exercises C-8

Page 775: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 6: Review Exercise

Review Exercise

Using the formatting options seen on the Summary page, create the FORMAT string which would result in a timestamp being output as seen here:

(Note: a maximum of 30 characters may be used in a format string.)

TUESDAY, JANUARY 20,2004 05:45:57.000000 PM +00:00

(FORMAT 'E4,BM4BDD,Y4BHH:MI:SSDS(F)BTBZ')(CHAR(50), UC)

Answers to Review Exercises C-9

Page 776: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 9: Review Exercise

Review Exercises

CSUM RANKMSUM QUANTILEMDIFF PERCENT_RANKMAVG WIDTH_BUCKET

Pick the appropriate function(s) w hich would be needed to solve the following problems:

1. What are the three highest moving averages during February? MAVG, RANK2. What percentage of stores took in less than $25 million last year? QUANTILE3. Show sales figures for March incremented daily with a final total.CSUM4. Show the disparity between sales on Saturday’s in Q1. MDIFF5. What percent of stores rank ahead of those who make $1 million per day?

PERCENT_RANK6. Given a selection of six products, what is the moving average of their product sales

for the twelve months of 2003? MAVG7. Divide our stores into 8 partitions based on their yearly sales. WIDTH_BUCKET

Answers to Review Exercises C-10

Page 777: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 10: Review Exercise

Review Exercise

1. True Sampling may be specified as a number or rows or as a percentage of rows.

2. True The specified sampling percentage, multiplied by the number of candidate rows, must equal at least .50 for a sample row to be returned.

3. True SAMPLE output can produce more rows than there are in the source table.

4. False The RANDOM (0.0 , 20000) is a legal expression

5. True Duplicate value likelihood may be reduced by increasing the size of the RANDOM interval relative to the size of the table.

6. False Randomized allocation means that random numbers are generated system-wide, and not AMP proportional.

7. True SAMPLE may be done using replacement or not, whereas RANDOM is always done with replacement.

8. True RANDOM may be used in Updating, Inserting or Deleting rows.

The following are True/False questions:

Answers to Review Exercises C-11

Page 778: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 11: Create Table Exercise

Create Table Exercise

CREATE TABLE abc(C1 INT NOT NULL PRIMARY KEY C1 = UPI,C2 INT); C2 =

CREATE TABLE abc(C1 INT NOT NULL PRIMARY KEY C1 = USI,C2 INT) C2 = UPI

UNIQUE PRIMARY INDEX (C2);

CREATE TABLE abc(C1 INT NOT NULL UNIQUE C1 = UPI,C2 INT); C2 =

CREATE TABLE abc(C1 INT NOT NULL PRIMARY KEY C1 = UPI,C2 INT NOT NULL UNIQUE); C2 = USI

CREATE TABLE abc(C1 INT C1 = NUSI,C2 INT); C2 =

CREATE TABLE abc(C1 INT NOT NULL C1 = NUPI,C2 INT NOT NULL UNIQUE) C2 = USI

PRIMARY INDEX (C1);

In the following examples, each CREATE TABLE statement uses varying syntax choices to define the table indexes. Choose which type of index will be assigned to columns C1 and C2 based on how the table is defined. (UPI, NUPI, USI or None)

Index Assigned

Answers to Review Exercises C-12

Page 779: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 11: Review Exercise

Review Questions

1. The COMPRESS attribute will permit compression of both nulls and values.

2. The two benefits associated with column compression are storage costs and system performance.

3. How many values can be compressed for a single column? 255

4. Constraints involving multiple columns must be defined at the table level.

5. In order to issue a HELP CONSTRAINT command, a constraint must be named. True

6. When defining a REFERENCES constraint at the table level, the FOREIGN KEY keywords must be used. True

7. All PRIMARY KEY and FOREIGN KEY constraints are implemented as indexes. False

8. A UNIQUE constraint can be implemented as a UPI or a USI. True

9. A FOREIGN KEY constraint must reference a column or columns that are defined using a PRIMARY KEY or a UNIQUE constraint. False

10. When an ALTER TABLE command is used to add a referencing constraint to a table, the command is successful only if there are no inconsistencies in the existing table data. False

Answers to Review Exercises C-13

Page 780: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 12: Volatile Table Quiz

Volatile Table Quiz

In the following exercise, User A has two parallel sessions. Assuming the given sequence of events, which will succeed, which will fail, and why?(Note: CVT = Create Volatile Table; CPT = Create Permanent Table.)

User A

Session 1 Session 2 Success/Failure/Reason

CVT T1 CVT T1

CPT T2

CVT T2

CVT T3

CPT T3

INSERT T3

CVT T3

INSERT T3

DROP T3

SEL * FROM T3

Success - each gets local instance_

Success - adds perm T2 to Sess 1

Failure - T2 already exists as perm

Success - add VT T3 to Sess 1

Success - add perm T3 to Sess 2

Success - inserts into VT T3

Failure - T3 exists as perm table

Success - inserts into perm T3

Success - Drops VT T3

Success - Selects from perm T3

Answers to Review Exercises C-14

Page 781: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 12: Review Exercise

Review Exercises

Which of the three choices below would be most suitable for each of the numbered hypothetical situations:

DT - Derived tableVT - Volatile tableGT - Global Temp table

1.) “When end-of-month queries are run this weekend, make sure that the scripts are set up to populate the necessary temporary tables without having to create them.”

2.) “We’ve just been handed an ad-hoc request for information which will require us to run a bunch of queries against a summary of last years sales.”

3.) “The boss says he doesn’t care if it’s extra work. He wants that information at the end of this week and if he likes it he’s going to want it every week.”

4.) “This query requires me to denormalize information in three different tables and then join the results. Fortunately, I onlyneed to do it this one time.”

5.) “Get me the top ten products by revenue and the top ten by profitabilty do an outer join on them.”

6.) “We need to agree with the marketing people on a single table layout so that our numbers can be compared with theirs at week’s end.”

(GT)

(VT)

(GT)

(DT)

(DT)

(GT)

Answers to Review Exercises C-15

Page 782: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 14: Grouping Exercise

Grouping Exercise

Consider the following query with three GROUPING SETS.

SELECT department_number,manager_employee_number,SUM(salary_amount)FROM employeeWHERE department_number < 402GROUP BY GROUPING SETS (department_number),GROUPING SETS (manager_employee_number),GROUPING SETS (())ORDER BY 1,2;

What result sets are produced by this construct?Using the multiplier effect, we have (a) (b) ( ).

This is equivalent to (a) (b) (1) or (a,b). The extra ( ) is really superfluous in this case.

The result sets will include: All department/manager combos

Recreate the query using only a single GROUPING SETS clause.

The GROUP BY clause could have been written as:

GROUP BY GROUPING SETS (department_number, manager_employee_number)

Answers to Review Exercises C-16

Page 783: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Module 16: Review Exercise

Review Exercise

1. A WITH table may be defined once and referenced multiple times throughout the query.

xa. Trueb. False

2. If a query depth is not specified in a recursive query, the query is unprotected from infinite looping.xa. Trueb. False

3. Which one is not a feature of WITH clause processing?a. Non-recursive derived tables.b. Recursive INSERT/SELECTsc. Recursive views

xd. Iterative control statements4. Which two of the following are permitted as part of a WITH RECURSIVE

statement.xa. UNION ALLb.) OUTER JOIN

xc. INNER JOINd. GROUP BY

5. WITH RECURSIVE is an ANSI standard feature of Teradata SQL.xa. Trueb. False

Answers to Review Exercises C-17

Page 784: Teradata Division Training - Online Tutorials ( Books ...dbmanagement.info/Books/MIX/SQL_Advance_TeraData.pdf · Teradata Division Training Objectives and Prerequisites Recommended

Notes

Answers to Review Exercises C-18