46797994 Teradata SQL Alchemy

199
TERADATA SQL By B M Kumar

Transcript of 46797994 Teradata SQL Alchemy

Page 1: 46797994 Teradata SQL Alchemy

TERADATA SQL

By

B M Kumar

Page 2: 46797994 Teradata SQL Alchemy

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)

Page 3: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

3

Basic RDBMS Concepts and Teradata SQL

Page 4: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

4

Basic RDBMS Concepts and Teradata SQL

Page 5: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

5

Basic RDBMS Concepts and Teradata SQL

Page 6: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

6

Basic RDBMS Concepts and Teradata SQL

Page 7: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

7

Basic RDBMS Concepts and Teradata SQL

Page 8: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

8

Basic RDBMS Concepts and Teradata SQL

Page 9: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

9

Basic RDBMS Concepts and Teradata SQL

Page 10: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

10

Basic RDBMS Concepts and Teradata SQL

Page 11: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

11

Basic RDBMS Concepts and Teradata SQL

Page 12: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

12

Basic RDBMS Concepts and Teradata SQL

Page 13: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

13

Basic RDBMS Concepts and Teradata SQL

Page 14: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

14

Basic RDBMS Concepts and Teradata SQL

Page 15: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

15

Basic RDBMS Concepts and Teradata SQL

Page 16: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

16

Basic RDBMS Concepts and Teradata SQL

Page 17: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

17

Basic RDBMS Concepts and Teradata SQL

Page 18: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

18

Basic RDBMS Concepts and Teradata SQL

Page 19: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

19

Basic RDBMS Concepts and Teradata SQL

Page 20: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

20

Basic RDBMS Concepts and Teradata SQL

Page 21: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

21

Teradata Extensions

Page 22: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

22

Teradata Extensions

Page 23: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

23

Teradata Extensions

Page 24: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

24

Teradata Extensions

Page 25: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

25

Teradata Extensions

Page 26: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

26

Teradata Extensions

Page 27: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

27

Teradata Extensions

Page 28: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

28

Teradata Extensions

Page 29: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

29

Data Types and Conversions

Page 30: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

30

Data Types and Conversions

Page 31: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

31

Data Types and Conversions

Page 32: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

32

Data Types and Conversions

Page 33: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

33

Data Types and Conversions

Page 34: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

34

Data Types and Conversions

Page 35: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

35

Data Types and Conversions

Page 36: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

36

Data Types and Conversions

Page 37: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

37

Data Types and Conversions

Page 38: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

38

Data Types and Conversions

Page 39: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

39

Data Types and Conversions

Page 40: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

40

Data Types and Conversions

Page 41: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

41

Data Types and Conversions

Page 42: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

42

Data Types and Conversions

Page 43: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

43

Data Types and Conversions

Page 44: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

44

Data Types and Conversions

Page 45: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

45

Data Types and Conversions

Page 46: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

46

Logical & Conditional ExpressionsAnd Output Attributes

Page 47: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

47

Logical and Conditional Expressions

Page 48: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

48

Logical and Conditional Expressions

Page 49: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

49

Logical and Conditional Expressions

Page 50: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

50

Logical and Conditional Expressions

Page 51: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

51

Logical and Conditional Expressions

Page 52: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

52

Logical and Conditional Expressions

Page 53: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

53

Logical and Conditional Expressions

Page 54: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

54

Logical and Conditional Expressions

Page 55: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

55

Logical and Conditional Expressions

Page 56: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

56

Logical and Conditional Expressions

Page 57: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

57

Logical and Conditional Expressions

Page 58: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

58

Logical and Conditional Expressions

Page 59: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

59

Output Attributes

Page 60: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

60

Output Attributes

Page 61: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

61

Output Attributes

Page 62: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

62

Output Attributes

Page 63: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

63

Output Attributes

Page 64: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

64

Output Attributes

Page 65: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

65

Output Attributes

Page 66: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

66

Output Attributes

Page 67: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

67

Output Attributes

Page 68: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

68

Output Attributes

Page 69: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

69

Output Attributes

Page 70: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

70

Output Attributes

Page 71: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

71

Data Definition

Page 72: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

72

Data Definition

Page 73: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

73

Data Definition

Page 74: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

74

Data Definition

Page 75: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

75

Data Definition

Page 76: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

76

Data Definition

Page 77: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

77

Data Definition

Page 78: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

78

Data Definition

Page 79: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

79

Data Definition

Page 80: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

80

Data Definition

Page 81: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

81

Data Definition

Page 82: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

82

Data Definition

Page 83: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

83

Data Definition

Page 84: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

84

Data Definition

Page 85: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

85

Data Definition

Page 86: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

86

Data Definition

Page 87: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

87

Data Definition

Page 88: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

88

Data Definition

Page 89: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

89

Data Definition

Page 90: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

90

Data Definition

Page 91: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

91

Data Manipulation

Page 92: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

92

Data Manipulation

Page 93: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

93

Data Manipulation

Page 94: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

94

Data Manipulation

Page 95: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

95

Data Manipulation

Page 96: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

96

Data Manipulation

Page 97: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

97

Data Manipulation

Page 98: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

98

Data Manipulation

Page 99: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

99

Data Manipulation

Page 100: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

100

Data Manipulation

Page 101: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

101

Data Manipulation

Page 102: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

102

Data Manipulation

Page 103: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

103

Views and Macros

Page 104: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

104

Views and Macros

Page 105: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

105

Views and Macros

Page 106: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

106

Views and Macros

Page 107: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

107

Views and Macros

Page 108: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

108

Views and Macros

Page 109: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

109

Views and Macros

Page 110: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

110

Views and Macros

Page 111: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

111

Views and Macros

Page 112: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

112

Views and Macros

Page 113: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

113

Views and Macros

Page 114: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

114

Views and Macros

Page 115: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

115

Views and Macros

Page 116: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

116

Views and Macros

Page 117: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

117

Views and Macros

Page 118: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

118

Views and Macros

Page 119: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

119

Views and Macros

Page 120: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

120

JOINS

Page 121: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

121

Joins

Page 122: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

122

Joins

Page 123: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

123

Joins

Page 124: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

124

Joins

Page 125: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

125

Joins

Page 126: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

126

Joins

Page 127: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

127

Joins

Page 128: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

128

Joins

Page 129: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

129

Joins

Page 130: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

130

Joins

Page 131: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

131

Joins

Page 132: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

132

Joins

Page 133: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

133

Joins

Page 134: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

134

Joins

Page 135: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

135

Joins

Page 136: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

136

Joins

Page 137: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

137

Joins

Page 138: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

138

Joins

Page 139: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

139

Joins

Page 140: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

140

Joins

Page 141: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

141

Joins

Page 142: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

142

Joins

Page 143: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

143

Sub Queries

Page 144: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

144

Sub Queries

Page 145: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

145

Sub Queries

Page 146: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

146

Sub Queries

Page 147: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

147

Sub Queries

Page 148: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

148

Sub Queries

Page 149: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

149

Set Operators

Page 150: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

150

Set Operators

Page 151: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

151

Set Operators

Page 152: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

152

Set Operators

Page 153: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

153

Set Operators

Page 154: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

154

Set Operators

Page 155: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

155

Set Operators

Page 156: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

156

Aggregations, Totals and Subtotals

Page 157: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

157

Aggregations, Totals and Subtotals

Page 158: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

158

Aggregations, Totals and Subtotals

Page 159: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

159

Aggregations, Totals and Subtotals

Page 160: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

160

Aggregations, Totals and Subtotals

Page 161: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

161

Aggregations, Totals and Subtotals

Page 162: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

162

Aggregations, Totals and Subtotals

Page 163: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

163

Aggregations, Totals and Subtotals

Page 164: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

164

Aggregations, Totals and Subtotals

Page 165: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

165

Aggregations, Totals and Subtotals

Page 166: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

166

Aggregations, Totals and Subtotals

Page 167: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

167

Aggregations, Totals and Subtotals

Page 168: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

168

Aggregations, Totals and Subtotals

Page 169: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

169

Aggregations, Totals and Subtotals

Page 170: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

170

Aggregations, Totals and Subtotals

Page 171: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

171

Rankings, Samplings, and Built-In Functions

Page 172: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

172

Rankings

Page 173: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

173

Rankings

Page 174: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

174

Rankings

Page 175: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

175

Rankings

Page 176: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

176

Rankings

Page 177: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

177

Rankings

Page 178: 46797994 Teradata SQL Alchemy

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.

Page 179: 46797994 Teradata SQL Alchemy

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

Page 180: 46797994 Teradata SQL Alchemy

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 ?

Page 181: 46797994 Teradata SQL Alchemy

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.

Page 182: 46797994 Teradata SQL Alchemy

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

Page 183: 46797994 Teradata SQL Alchemy

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.

Page 184: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

184

Built-In Functions

Page 185: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

185

String Functions

Page 186: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

186

String Functions

Page 187: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

187

String Functions

Page 188: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

188

String Functions

Page 189: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

189

String Functions

Page 190: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

190

String Functions

Page 191: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

191

String Functions

Page 192: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

192

String Functions

Page 193: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

193

BTEQ (Basic Teradata Query)

Page 194: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

194

BTEQ

Page 195: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

195

BTEQ

Page 196: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

196

BTEQ

Page 197: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

197

BTEQ

Page 198: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

198

BTEQ

Page 199: 46797994 Teradata SQL Alchemy

Teradata COE - Alchemy

Solutions

199

Questions