Teradata SQL Alchemy
Teradata SQL Alchemy
By
B M Kumar
Agenda
• Basic RDBMS Concepts and Teradata SQL
• Data Definitions
• Data Manipulations
• Joins
• Sub queries
• Set Operators
SELECT 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
402 308000.00
501 308000.00
201 293800.00
302 226000.00
600 ?
Note: Null sorts following the valid budget
amounts.
Teradata COE - Alchemy 180
Solutions
TOP N
TOP 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.
Stratified sampling:
the ability to generate different sized samples from different groupings of the data.
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