46797994 teradata sql alchemy
TRANSCRIPT
TERADATA SQL
By
B M Kumar
Teradata COE - Alchemy
Solutions
2
Agenda
• Basic RDBMS Concepts and Teradata SQL
• Teradata Extensions ( HELP & SHOW)
• Data Types and Conversions
• Logical & Conditional Expressions, Attribute & String Functions
• Data Definitions
• Data Manipulations
• Views and Macros
• Joins
• Sub queries
• Set Operators
• Aggregations, Totals and Subtotals
• Rankings, Samplings, and Built-In Functions
• BTEQ ( Basic Teradata Query)
Teradata COE - Alchemy
Solutions
3
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
4
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
5
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
6
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
7
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
8
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
9
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
10
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
11
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
12
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
13
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
14
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
15
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
16
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
17
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
18
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
19
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
20
Basic RDBMS Concepts and Teradata SQL
Teradata COE - Alchemy
Solutions
21
Teradata Extensions
Teradata COE - Alchemy
Solutions
22
Teradata Extensions
Teradata COE - Alchemy
Solutions
23
Teradata Extensions
Teradata COE - Alchemy
Solutions
24
Teradata Extensions
Teradata COE - Alchemy
Solutions
25
Teradata Extensions
Teradata COE - Alchemy
Solutions
26
Teradata Extensions
Teradata COE - Alchemy
Solutions
27
Teradata Extensions
Teradata COE - Alchemy
Solutions
28
Teradata Extensions
Teradata COE - Alchemy
Solutions
29
Data Types and Conversions
Teradata COE - Alchemy
Solutions
30
Data Types and Conversions
Teradata COE - Alchemy
Solutions
31
Data Types and Conversions
Teradata COE - Alchemy
Solutions
32
Data Types and Conversions
Teradata COE - Alchemy
Solutions
33
Data Types and Conversions
Teradata COE - Alchemy
Solutions
34
Data Types and Conversions
Teradata COE - Alchemy
Solutions
35
Data Types and Conversions
Teradata COE - Alchemy
Solutions
36
Data Types and Conversions
Teradata COE - Alchemy
Solutions
37
Data Types and Conversions
Teradata COE - Alchemy
Solutions
38
Data Types and Conversions
Teradata COE - Alchemy
Solutions
39
Data Types and Conversions
Teradata COE - Alchemy
Solutions
40
Data Types and Conversions
Teradata COE - Alchemy
Solutions
41
Data Types and Conversions
Teradata COE - Alchemy
Solutions
42
Data Types and Conversions
Teradata COE - Alchemy
Solutions
43
Data Types and Conversions
Teradata COE - Alchemy
Solutions
44
Data Types and Conversions
Teradata COE - Alchemy
Solutions
45
Data Types and Conversions
Teradata COE - Alchemy
Solutions
46
Logical & Conditional ExpressionsAnd Output Attributes
Teradata COE - Alchemy
Solutions
47
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
48
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
49
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
50
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
51
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
52
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
53
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
54
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
55
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
56
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
57
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
58
Logical and Conditional Expressions
Teradata COE - Alchemy
Solutions
59
Output Attributes
Teradata COE - Alchemy
Solutions
60
Output Attributes
Teradata COE - Alchemy
Solutions
61
Output Attributes
Teradata COE - Alchemy
Solutions
62
Output Attributes
Teradata COE - Alchemy
Solutions
63
Output Attributes
Teradata COE - Alchemy
Solutions
64
Output Attributes
Teradata COE - Alchemy
Solutions
65
Output Attributes
Teradata COE - Alchemy
Solutions
66
Output Attributes
Teradata COE - Alchemy
Solutions
67
Output Attributes
Teradata COE - Alchemy
Solutions
68
Output Attributes
Teradata COE - Alchemy
Solutions
69
Output Attributes
Teradata COE - Alchemy
Solutions
70
Output Attributes
Teradata COE - Alchemy
Solutions
71
Data Definition
Teradata COE - Alchemy
Solutions
72
Data Definition
Teradata COE - Alchemy
Solutions
73
Data Definition
Teradata COE - Alchemy
Solutions
74
Data Definition
Teradata COE - Alchemy
Solutions
75
Data Definition
Teradata COE - Alchemy
Solutions
76
Data Definition
Teradata COE - Alchemy
Solutions
77
Data Definition
Teradata COE - Alchemy
Solutions
78
Data Definition
Teradata COE - Alchemy
Solutions
79
Data Definition
Teradata COE - Alchemy
Solutions
80
Data Definition
Teradata COE - Alchemy
Solutions
81
Data Definition
Teradata COE - Alchemy
Solutions
82
Data Definition
Teradata COE - Alchemy
Solutions
83
Data Definition
Teradata COE - Alchemy
Solutions
84
Data Definition
Teradata COE - Alchemy
Solutions
85
Data Definition
Teradata COE - Alchemy
Solutions
86
Data Definition
Teradata COE - Alchemy
Solutions
87
Data Definition
Teradata COE - Alchemy
Solutions
88
Data Definition
Teradata COE - Alchemy
Solutions
89
Data Definition
Teradata COE - Alchemy
Solutions
90
Data Definition
Teradata COE - Alchemy
Solutions
91
Data Manipulation
Teradata COE - Alchemy
Solutions
92
Data Manipulation
Teradata COE - Alchemy
Solutions
93
Data Manipulation
Teradata COE - Alchemy
Solutions
94
Data Manipulation
Teradata COE - Alchemy
Solutions
95
Data Manipulation
Teradata COE - Alchemy
Solutions
96
Data Manipulation
Teradata COE - Alchemy
Solutions
97
Data Manipulation
Teradata COE - Alchemy
Solutions
98
Data Manipulation
Teradata COE - Alchemy
Solutions
99
Data Manipulation
Teradata COE - Alchemy
Solutions
100
Data Manipulation
Teradata COE - Alchemy
Solutions
101
Data Manipulation
Teradata COE - Alchemy
Solutions
102
Data Manipulation
Teradata COE - Alchemy
Solutions
103
Views and Macros
Teradata COE - Alchemy
Solutions
104
Views and Macros
Teradata COE - Alchemy
Solutions
105
Views and Macros
Teradata COE - Alchemy
Solutions
106
Views and Macros
Teradata COE - Alchemy
Solutions
107
Views and Macros
Teradata COE - Alchemy
Solutions
108
Views and Macros
Teradata COE - Alchemy
Solutions
109
Views and Macros
Teradata COE - Alchemy
Solutions
110
Views and Macros
Teradata COE - Alchemy
Solutions
111
Views and Macros
Teradata COE - Alchemy
Solutions
112
Views and Macros
Teradata COE - Alchemy
Solutions
113
Views and Macros
Teradata COE - Alchemy
Solutions
114
Views and Macros
Teradata COE - Alchemy
Solutions
115
Views and Macros
Teradata COE - Alchemy
Solutions
116
Views and Macros
Teradata COE - Alchemy
Solutions
117
Views and Macros
Teradata COE - Alchemy
Solutions
118
Views and Macros
Teradata COE - Alchemy
Solutions
119
Views and Macros
Teradata COE - Alchemy
Solutions
120
JOINS
Teradata COE - Alchemy
Solutions
121
Joins
Teradata COE - Alchemy
Solutions
122
Joins
Teradata COE - Alchemy
Solutions
123
Joins
Teradata COE - Alchemy
Solutions
124
Joins
Teradata COE - Alchemy
Solutions
125
Joins
Teradata COE - Alchemy
Solutions
126
Joins
Teradata COE - Alchemy
Solutions
127
Joins
Teradata COE - Alchemy
Solutions
128
Joins
Teradata COE - Alchemy
Solutions
129
Joins
Teradata COE - Alchemy
Solutions
130
Joins
Teradata COE - Alchemy
Solutions
131
Joins
Teradata COE - Alchemy
Solutions
132
Joins
Teradata COE - Alchemy
Solutions
133
Joins
Teradata COE - Alchemy
Solutions
134
Joins
Teradata COE - Alchemy
Solutions
135
Joins
Teradata COE - Alchemy
Solutions
136
Joins
Teradata COE - Alchemy
Solutions
137
Joins
Teradata COE - Alchemy
Solutions
138
Joins
Teradata COE - Alchemy
Solutions
139
Joins
Teradata COE - Alchemy
Solutions
140
Joins
Teradata COE - Alchemy
Solutions
141
Joins
Teradata COE - Alchemy
Solutions
142
Joins
Teradata COE - Alchemy
Solutions
143
Sub Queries
Teradata COE - Alchemy
Solutions
144
Sub Queries
Teradata COE - Alchemy
Solutions
145
Sub Queries
Teradata COE - Alchemy
Solutions
146
Sub Queries
Teradata COE - Alchemy
Solutions
147
Sub Queries
Teradata COE - Alchemy
Solutions
148
Sub Queries
Teradata COE - Alchemy
Solutions
149
Set Operators
Teradata COE - Alchemy
Solutions
150
Set Operators
Teradata COE - Alchemy
Solutions
151
Set Operators
Teradata COE - Alchemy
Solutions
152
Set Operators
Teradata COE - Alchemy
Solutions
153
Set Operators
Teradata COE - Alchemy
Solutions
154
Set Operators
Teradata COE - Alchemy
Solutions
155
Set Operators
Teradata COE - Alchemy
Solutions
156
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
157
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
158
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
159
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
160
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
161
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
162
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
163
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
164
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
165
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
166
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
167
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
168
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
169
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
170
Aggregations, Totals and Subtotals
Teradata COE - Alchemy
Solutions
171
Rankings, Samplings, and Built-In Functions
Teradata COE - Alchemy
Solutions
172
Rankings
Teradata COE - Alchemy
Solutions
173
Rankings
Teradata COE - Alchemy
Solutions
174
Rankings
Teradata COE - Alchemy
Solutions
175
Rankings
Teradata COE - Alchemy
Solutions
176
Rankings
Teradata COE - Alchemy
Solutions
177
Rankings
Teradata COE - Alchemy
Solutions
178
Rankings with Partition BYSELECT storeid ,prodid
,sales,RANK( ) OVER (PARTITION BY storeid, prodid
ORDER BY sales DESC) AS "Ranking"FROM salestblQUALIFY Ranking <= 3;storeid prodid sales Ranking----------- ------ ----------- -----------1001 A 100000.00 11001 C 60000.00 11001 D 35000.00 11001 F 150000.00 11002 A 40000.00 11002 C 35000.00 11002 D 25000.00 11003 A 30000.00 11003 B 65000.00 11003 C 20000.00 11003 D 50000.00 1 Things To Notice•An additional PARTITION BY clause is added. •In this case, the scope is sales by product within store. •Each product exists only once in each store. •Thus, each row gets a rank of 1. each store.
Teradata COE - Alchemy
Solutions
179
TOP N Overview
The TOP N function provides the ability to produce:
•The top (or bottom) N rows of results based on specified criteria.
•The top (or bottom) N percentage of rows based on specified criteria.
•These results either with or without ties (more than one row has identical criteria
values).
•Sample rows arbitrarily without regard to rankings or order.
This improvement has been achieved by implementing the following new
syntax to the SQL lexicon:
TOP {decimal | integer} [PERCENT] [WITH TIES]
The following meaning is associated with these options:
•TOP 10 – Return the top ten rows according to criteria
•TOP 15 PERCENT – Return the top 15% of rows according to criteria
•TOP 10 WITH TIES – If more that one row has the same criteria value, return all
•TOP 15 PERCENT WITH TIES – If more than one row has the same criteria
value, return all
Teradata COE - Alchemy
Solutions
180
TOP N• Show all department budgets ordered by budget amount descending. To
answer this query, the TOP N feature is not needed.
SELECT department_number , budget_amount
FROM department ORDER BY 2 DESC;
Note: Null sorts following the valid budget amounts.
department_number budget_amount
----------------- -------------
401 982300.00
403 932000.00
301 465600.00
100 400000.00
402 308000.00
501 308000.00
201 293800.00
302 226000.00
600 ?
Teradata COE - Alchemy
Solutions
181
TOP NTOP N With and Without TIES
SELECT TOP 5
department_number
, budget_amount
FROM department
ORDER BY 2 DESC;
department_number budget_amount
----------------- -------------
401 982300.00
403 932000.00
301 465600.00
100 400000.00
501 308000.00
Things to notice:
ORDER BY defines the sequencing of the result set.
It therefore defines the ranking criteria.
To get the TOP highest amounts, you must use ORDER with DESC.
TOP N where N is an integer up to 18 digits in length.
SELECT TOP 5 WITH TIES
department_number
, budget_amount
FROM department
ORDER BY 2 DESC;
department_number budget_amount
----------------- -------------
401 982300.00
403 932000.00
301 465600.00
100 400000.00
501 308000.00
402 308000.00
Things to notice:
Even though TOP 5 is specified, six rows are returned.
Because there is a tie for the fifth position, both rows are returned.
This only occurs when WITH TIES is specified.
WITH TIES returns multiple tied rows when there is a tie for the 'last' position.
It will return all rows containing the 'tied' value, but it will only count it as one row.
Tied rows which are not in the last position, are each counted separately toward the N total.
Teradata COE - Alchemy
Solutions
182
SAMPLEThe 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.
Stratified sampling:
the ability to generate different sized samples from different groupings of the data.
Consider the distribution of departments within the employee table.
SELECT department_number
,COUNT (*)
FROM employee
GROUP BY 1
ORDER BY 1;
• department_number Count(*)----------------- -----------
100 1
201 2
301 3
302 1
401 7
402 2
403 6
501 4
Teradata COE - Alchemy
Solutions
183
SAMPLECreate 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 employee
GROUP BY 1 ORDER BY 1;
Group_Name Count(*)
---------- -----------
GROUP 1 1
GROUP 2 2
GROUP 3 4
GROUP 4 15
GROUP 5 4
SELECT last_name (CHAR(10)) ,department_number (FORMAT '999') AS Deptno , SAMPLEID FROM employee SAMPLE
WHEN department_number BETWEEN 100 AND 199 THEN .25
WHEN department_number BETWEEN 200 AND 299 THEN .25
WHEN department_number BETWEEN 300 AND 399 THEN .25
WHEN department_number BETWEEN 400 AND 499 THEN .25
WHEN department_number BETWEEN 500 AND 599 THEN .25
END ORDER BY 2;
last_name Deptno SampleId
---------- ------ -----------
Short 201 2 (.25 * 2 = .50 ROUNDING TO 1)
Stein 301 3 (.25 * 4 = 1)
Phillips 401 4 (.25 * 15 = 3.75 ROUNDING TO 4)
Rogers 401 4
Villegas 403 4
Hopkins 403 4
Ratzlaff 501 5 (.25 * 4 = 1)
Things to notice:
•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.
Teradata COE - Alchemy
Solutions
184
Built-In Functions
Teradata COE - Alchemy
Solutions
185
String Functions
Teradata COE - Alchemy
Solutions
186
String Functions
Teradata COE - Alchemy
Solutions
187
String Functions
Teradata COE - Alchemy
Solutions
188
String Functions
Teradata COE - Alchemy
Solutions
189
String Functions
Teradata COE - Alchemy
Solutions
190
String Functions
Teradata COE - Alchemy
Solutions
191
String Functions
Teradata COE - Alchemy
Solutions
192
String Functions
Teradata COE - Alchemy
Solutions
193
BTEQ (Basic Teradata Query)
Teradata COE - Alchemy
Solutions
194
BTEQ
Teradata COE - Alchemy
Solutions
195
BTEQ
Teradata COE - Alchemy
Solutions
196
BTEQ
Teradata COE - Alchemy
Solutions
197
BTEQ
Teradata COE - Alchemy
Solutions
198
BTEQ
Teradata COE - Alchemy
Solutions
199
Questions