Teradata Lab - Self Study 3
Teradata Lab - Self Study 3
CONTENTS:
1) Math Functions
2) OLAP Functions
3) Substrings and Positioning Functions
4) Temporal Tables Create function
5) Temporary Tables
Teradata CoE Teradata Lab Course
Math Functions
Teradata CoE Teradata Lab Course
Math Functions
Teradata CoE Teradata Lab Course
OLAP Functions
Teradata CoE Teradata Lab Course
OLAP Functions
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
4
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
"
Product :>ate Ja;:!l
I:> 10/03/2000
Sales 64300.00
1000 10/01/2000 54850.29
2000 10/04/2000 54553.10
1000
1000
2000
1000
2000
09/29/2000
09/30/2000
09/28/2000
09/29/2000
54500.22
49850.03
48850.40
48000.00
"l
1
2000 10/03/2000 ,3200.18
2000 09/28/2000 41888.88
1000 10/01/2000 ,0200.43
2000 10/02/2000 36021.
1000 09/30/2000 93 4
2000 10/04/2000 36000.07
5
1000 3::800.5
10/02/2000
6
0 7
Is the query above in ASC mode or DESC mode32800.50
for sorting? 8
9
RANK() OVER and PARTITION BY 10
11
12
SELECT Product ID ,Sale Date, Daily Sales, 13
RANK() OVER (PARTITIO!" BY Product ID 13
ORDER BY Oaily_Sales OESC) AS
Rankl fROM Sales Tab:e
WHERE Product ID IN (1000,
2000)
Product Sale Date Da�l� Sales Ran kl
I:> 10/03/2000 6,300.00 1
1000 10/04/2000 5,553.10 2
1000 09/29/2000 54500.22 3
1000 09/28/2000 48850.40 4
1000 10/01/2000 40200.43 5
1000 09/30/2000 36000.07 6
1000 10/02/2000 32800.50 7
::ooo
1000 10/01/2000 54850.29 l
The above example finds the value of a column in the next row for Daily_Sales. Notice it is partitioned, so
there is a Null value at the end of each Product_lD.
The above example finds the value of a column in the next row for Sale_Date. Notice it is partitioned, so there
is a Null
Teradata CoE Teradata Lab Course
Row Nbr Product ID Sale Date Next Start. :)t Dail� Sa1es To Date Revenue
1 1000 09/28/2000 09/29/2000 48850.40 �8850.40
2 1000 09/29/2000 09/30/2000 5,500.22 103350.62
3 1000 09/30/2000 10/01/2000 36000.07 139350.69
1 2000 09/28/2000 09/29/2000 41888. 88 ,1888.88
2 2000 09/29/2000 09/30/2000 48000.00 89888.88
3 2000 09/30/2000 10/01/2000 49850.03 139738.9
1 3000 09/28/2000 09/29/2000 61301.77 1
2 3000 09/29/2000 09/30/2000 3,509.13 61301.77
3 3000 09/30/2000 10/01/2000 43868.86 95810.90
139679.76
The above example shows the cumulative SUM for the Daily_Sales for the first three days for each of our
Product_lDs.
Quiz - Fill in the Blank
Hare:ison 8
Jones 5
Larkins 7
Reilly 6
Smith 5
Smythe 6
Strickling !
O
The TRIM command will trim off any spaces before and after the
Last_name.
CHARACTERS and CHARACTER_LENGTH equivalent
Query2
These two queries will get you the SAME EXACT answer set in your
report
OCTET_LENGTH
Query
1
SELECT First Name
,CH�..RACTER(First_Name) AS C_Lengt
FROM Em lo ee Table;
Query2
Query3
You can also use the OCTET LENGTH command. These three queries get the same exact answer sets! Query 2 and 3
are ANSI Standard.
Query
1
SELECT Last Name
,Trirn(Last Name) AS No_Space
FROM Em lo-ee Table
Query2
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
CHAR(20)
Lsi:i!. ,
es
HAm� �t:t�·
Jones
�
Smi-ch
Smythe th
Harr:.so he
n on
Chambers zs
Stricklin,i
ng
Reil:y ly
Coffing
ng
:,ark:.ns ns
The SOL above brings back the last two letters of each Last_Name, even though the last names are of different
length. We first trimmed the spaces off of Last_Name. Then we counted the characters in the Last_Name. Then we
subtracted two from the Last_Name character length, and then passed it to our substring as the starting position.
Since we didn't give an ending position in our substring it defaulted to the end.
Query
1
SELECT First
SUBS7RING(Fi;st Name FRO� 2 for3) AS Qui
Name,
FROM Em lo cc 7ablc ;
Query2
SE:..ECT Dept No
,Jepartment Name as Jepty
,SU3STR(JeptJ,1 , ?OSIT:OK(' ' IN Jcpartreent_Nane) -1) as
Wordl
FROM Department Table;
SE:..ECT Dept_Xo
,Jepa=tment Name as Jepty
,SUBSTR:NG(J;pty ?ROM 1 ;-OR ?OSITION(' ' IN Jepart�ent_Na.ne) -1) as Wo=dl
FROM Department Table;
Dept No Depty
200 Research and Develop Research
100 Marketing �a=ket�ng
What was the starting position of the Substring in the above query? It was one. The ending position (FOR
length) was calculated to look for the first space and then subtract 1. So for "Research and Develop" the starting
position was one and for 9-1 = 8.
SE�ECT Dept_No
,Department �a�e as Depty
,SUBSTRIXG(Jepty PROM 1 c-OR POSlT:ON(' ' IN Jepartoent_Nane) -1) as Wo=dl
FROM Department Table;
Dept No Deoty Wordl
200 Research and �evelop Research
100 Marketing Customer Marketing
400 Support Sales Custome=
300 Human Resources Sales
500 :3.uman
Marketing E'O
(FRO!-'. R (FROM
Research and Jevelop FOR
Sales (FROM FOR )
Customer Support FOR )
(;RO!-'.
Human Resources (PROM� POR -i-
Fill in the number for the FROM and the FOR numbers above for each row.
Next page!
The POSITION Command is brilliant with SUBSTRING
SELECT Dept_Xo
,�epartment Name as �epty
,SUBSTR(Jepty,l , ?OSlT:ON(' ' IN Depart�ent_Naoe) -1) as Wordl
FROM Department_Table;
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
See those II? Those represent concatenation. That allows you to combine multiple columns into one column.
The II (Pipe
Symbol) on your keyboard is just above the ENTER key. Don't put a space in between, but just put two Pipe
Symbols
together.
In this example,and
Concatenation we have combined the first name, then a single space, and then the last name to get a new
column
SUBSTRINGcalled 'Full name', like Squiggy Jones.
•
a
.Last �pace
Name
. Substring(First_Name. 1. 1) JI '. ' II Last Name
as Full Name
FROM Employee_Table
WHEREFirst_Name = 'Squiggy'
Of the three items being concatenated together, what is the first item of concatenation in the example above?
It is the first initial of the First_Name. Then we concatenated a literal space and a period. Then we
concatenated the Last_Name. Notice that the report shows only three columns.
� I I
J
SELECT First Name VAR.HAR
.Last Xame
,TRD.l(Last_Kmue) '' Substri11g(First_Name, l, 1) ' AS Last Kame l st
FROM Employee Table
WHERE First_Kame = 'Squiggy' :
ERROR
What happened above to cause the error? Can you see it? The Pipe Symbols II have a space between them
like 11, when
it should be 11- It is a tough one to spot so be careful.
Capt?,Pl 1 lil ti
Teradata CoE Teradata Lab Course
Temporary Tables
Teradata CoE Teradata Lab Course
Temporary Tables
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course
Teradata CoE Teradata Lab Course