Social Icons

Top SQL Commands

TERADATA SQL COMMNADS
SQL to changing the default Database
DATABASE EMP_DATA_BASE;

SQL to find Information about a Database
HELP DATABASE EMP_DATA_BASE;

SQL to get Sample number of rows   
   
SELECT * FROM EMP_TBL SAMPLE 10;

SQL to get a sample Percentage of rows
SELECT * FROM EMP_TBL SAMPLE .50;

SQL to find information about a Table
SHOW TABLE EMP_TBL;

SQL to Use an Access Locked Table
LOCKING ROW FOR ACCESS SELECT * FROM EMP_TBL;

SQL Keywords that describe you

SELECT DATABASE, USER, SESSION,
ACCOUNT,
PROFILE,
ROLE;"
"SELECT DATE,
CURRENT_DATE,
TIME,
CURRENT_TIME,
CURRENT_TIMESTAMP;


SQL to Use Aggregates functions

SELECT TOP 10  STUDENT_NO, FIRST_NAME, LAST_NAME, CLASS_CODE
FROM STUDENT_TBL
ORDER BY GRADE DESC;"
"SELECT DEPT_NO
,MAX(SALARY) AS ""MAXIMUM""
,MIN(SALARY) AS ""MINIMUM""
,AVG(SALARY) AS ""AVERAGE""
,SUM(SALARY) AS ""SUM""
,COUNT(*) AS ""COUNT""
FROM EMP_TBL
GROUP BY DEPT_NO
ORDER BY DEPT_NO;


SQL to Select TOP Rows in a Rank Order
SELECT TOP 10  STUDENT_NO
,FIRST_NAME
,LAST_NAME
,CLASS_CODE
FROM STUDENT_TBL
ORDER BY GRADE DESC;


SQL Using Date, Time and Timestamp
SELECT
CALENDAR_DATE                
,DAY_OF_WEEK                  
,DAY_OF_MONTH                 
,DAY_OF_YEAR                  
,DAY_OF_CALENDAR              
,WEEKDAY_OF_MONTH             
,WEEK_OF_MONTH                
,WEEK_OF_YEAR                 
,WEEK_OF_CALENDAR             
,MONTH_OF_QUARTER             
,MONTH_OF_YEAR                
,MONTH_OF_CALENDAR            
,QUARTER_OF_YEAR              
,QUARTER_OF_CALENDAR          
,YEAR_OF_CALENDAR             
FROM SYS_CALENDAR.CALENDAR;



SQL to Find out how much Space a USER have
SELECT
USERNAME                     
,CREATORNAME                  
,PERMSPACE                    
,SPOOLSPACE                   
,TEMPSPACE                    
,LASTALTERNAME                
,LASTALTERTIMESTAMP           
FROM DBC.USERS
WHERE USERNAME='USER';


SQL to find how much Space left Per AMP in database

SELECT
VPROC                        
,DATABASENAME                 
,ACCOUNTNAME                  
,MAXPERM
,MAXSPOOL                     
,MAXTEMP                      
 FROM DBC.DISKSPACE
WHERE DATABASENAME='EMP_DB'   ;    
    

SQL to finding USER Space
SELECT
 MAX(MAXPERM)
,MAX(MAXSPOOL)
,MAX(MAXTEMP)
 FROM DBC.DISKSPACE
WHERE DATABASENAME='USER' ;


SQL to find Space Skew in Tables in a Database
SELECT VPROC
,CAST(TABLENAME AS CHAR(20))
,CURRENTPERM
,PEAKPERM
FROM DBC.TABLESIZEV
WHERE DATABASENAME='USER'
ORDER BY TABLENAME, VPROC;


SQL to Find Table Skew
SELECT
TABLENAME,
SUM(CURRENTPERM) /(1024*1024) AS CURRENTPERM,
(100 - (AVG(CURRENTPERM)/MAX(CURRENTPERM)*100)) AS SKEWFACTOR
FROM
DBC.TABLESIZE
WHERE DATABASENAME=
AND TABLENAME =
GROUP BY 1;


SQL to Find AMP Skew
SELECT DATABASENAME
,TABLENAME
,VPROC
,CURRENTPERM
,PEAKPERM
FROM DBC.TABLESIZE
WHERE
DATABASENAME=
AND
TABLENAME=
ORDER BY VPROC ;


SQL to find number of rows per AMP for a Column
SELECT HASHAMP(HASHBUCKET( HASHROW(EMP_NO))) AS ""AMP"" , COUNT(*)
FROM EMP_TABLE
GROUP BY 1
ORDER BY 1;


SQL to Identify  duplicate records
SELECT COLUMN1, COLUMN2, COLUMN3, COUNT(*)
FROM
DATABASE.TABLE
GROUP BY COLUMN1, COLUMN2, COLUMN3
HAVING COUNT(*) >1;


SQL to Delete Duplicate records
CREATE TABLE TABLE1_BACKUP AS (SELECT * FROM TABLE1 QUALIFY ROW_NUMBER() OVER (PARTITION BY COLUMN1 ORDER BY COLUMN1 DESC )=1) WITH DATA;
DELETE FROM TABLE1;
INSERT INTO TABLE1 SELECT * FROM TABLE_BACKUP;


SQL below to find TOP Databases by space occupied

SELECT
DatabaseName
,MAX(CurrentPerm) * (HASHAMP()+1)/1024/1024 AS USEDSPACE_IN_MB
FROM DBC.DiskSpace
GROUP BY DatabaseName
ORDER BY USEDSPACE_IN_MB DESC;


SQL to find TOP Tables by space occupied
SELECT DATABASENAME
,TABLENAME
,SUM(CurrentPerm)/1024/1024 AS TABLESIZE_IN_MB
FROM DBC.TableSize
GROUP BY DATABASENAME,TABLENAME
ORDER BY TABLESIZE_IN_MB DESC;


SQL to find out list of nodes
SELECT DISTINCT NODEID FROM DBC.RESUSAGESPMA;

SQL to find Account Information
SELECT * FROM DBC.AccountInfoV ORDER BY 1;


Continue Reading...

COLLECT STATISTICS

teradata Collect Statistics
The Purpose of  COLLECT STATISTICS is to gather and store demographic data for one or more columns or indices of a table or join index.In this process it collects data and stores the summary in the Data Dictionary (DD) inside USER DBC.
The optimizer uses this synopsis data to generate efficient table access and join plans.

Below are the statistics will collect
  • The number of rows in the table
  • The average row size
  • Information on all Indexes in which statistics were collected
  • The range of values for the column(s) in which statistics were collected
  • The number of rows per value for the column(s) in which statistics were collected
  • The number of NULLs for the column(s) in which statistics were collected
What to COLLECT STATISTICS On?

Do NOT COLLECT Statistics on all columns and indexes because it takes too much space.
  • Primary Index of a Join Index
  • Secondary Indexes defined on any join index
  • Non-indexed columns used in joins
  • The Unique Primary Index of small tables (less than 1,000 rows per AMP)
  • All Non-Unique Primary Indexes and All Non-Unique Secondary Indexes
  • Join index columns that frequently appear on any additional join index columns that frequently appear in WHERE search conditions
  • The Unique Primary Index of small tables (less than 1,000 rows per AMP)
  • Columns that frequently appear in WHERE search conditions or in the WHERE clause of joins
Syntax for COLLECT STATISTICS

COLLECT STATISTICS on COLUMN ;
COLLECT STATISTICS on INDEX ();


Examples for a better Understanding

COLLECT STATISTICS on Emp_Table ;
COLLECT STATISTICS on Emp_Table COLUMN Dept_no ;
COLLECT STATISTICS on Emp_Table COLUMN(Emp_no, Dept_no);
COLLECT STATISTICS on Emp_Table INDEX Emp_no ;
COLLECT STATISTICS on Emp_Table INDEX (First_name, Last_name);


The New Teradata V14 Way to Collect Statistics


COLLECT STATISTICS COLUMN(First_Name, Last_Name) 
                                         ,COLUMN(First_Name)
                                         ,COLUMN(Dept_No) ON Employee_Table;


With the latest approach, Only a single table scan is required instead multiple table scan like old approach.

Teradata keeps all Collect Statistics information in DBC in the Data Dictionary tables. Those are
  1. DBC.Indexes (for multi-column indexes only)
  2. DBC.TVFields (for all columns and single column indexes)
  3. DBC.StatsTbl (Teradata V14 and beyond)
Collect Statistics on a PPI Table on the Partition
COLLECT STATISTICS on Order_Table_PPI COLUMN PARTITION

The main reasons to collect stats on Partiton are
  • PE will give better plan for PPI tables
  • It helps on  Partition Elimination on Range Queries.
  • Mainly helpful when a table has a lot of empty partitions.
Teradata V14 Statistics Enhancements

1) There is a SUMMARY option to collect table-level statistics
Table-level statistics known as "summary statistics" are collected whenever column or index statistics are collected.

SHOW SUMMARY STATISTICS VALUES ON Employee_Table;

One critical advantage is that the optimizer now uses summary stats to get the most up-to-date row count from the table in order to provide more accurate extrapolations

2) SYSTEM SAMPLE option allows the system to determine the sampled system percentage.
SAMPLE n PERCENT allows you to specify sampling at the individual statistics collection level rather than at the system level
COLLECT STATISTICS    USING Sample 20 Percent   COLUMN (Last_Name) ON Employee_Table;

3) Statistics are stored in DBC.StatsTbl to reduce access contention and improve performance.

4) New views DBC.StatsV, DBC.ColumnStatsV, DBC.MultiColumnStatsV, and IndexStatsV.

5) SHOW STATISTICS statement reports detailed statistics in plain text or XML formatting.

6) MaxValueLength
Teradata only placed the first 16 bytes in the statistics before Version 14 but now the default is 25 bytes.
COLLECT STATISTICS USING MAXVALUELENGTH 50  COLUMN (Item_Name)  ON Shipping_Table  
7) MaxIntervals 
Whenever you collected statistics, Teradata did a full table scan on the values, sorted them, and then placed them into 200 intervals. Now, the default is 250 intervals

8)Expressions can now be specified in COLLECT STATS statements

COLLECT STATISTICS USING SYSTEM SAMPLE COLUMN (EXTRACT(MONTH FROM Order_Date), Order_Total) AS Month_Plus_Total ON Order_Table;
Also use Substr(), MOD, CONCATENATION, Format Conversion expressions like Uppercase, Lowercase, Data type Conversions, CASE-Expressions, BEGIN and END expressions of PERIOD types.

9)AutoStats feature
Teradata V14.10 Autostats identifies and collects missing statistics needed and detects stale statistics for refreshing. If statistics are not being used by optimizer, they will be removed.
A new repository of a system supplied database named TDSTATS stores metadata for all stats collections. This is created by a new process called DIPSTATS.

10) External stored procedures (XSPs) perform the stats management process.

These are the latest enhancements of COLLECT STATISTICS.
Continue Reading...

HASH Functions

There are four types of hashing functions are available in Teradata.
  1. HASHROW      
  2. HASHAMP
  3. HASHBUCKET   
  4. HASHBAKAMP
HASHROW Function 
Basic Synatx for HASHROW is
HASHROW( [ < data-column-value > […, < data-column-value >…] ] )

Example

SELECT    HASHROW (NULL)     AS NULL_HASH
                 ,HASHROW('Anil')        AS Name_HASH
                 ,HASHROW()                 AS NO_HASH ;







  • The Query will give same results if you ran again and again.
  • The HASHROW function produces the 32-bit binary Row Hash that is stored as part of the data row.
  • It returns maximum of 4,294,967,295 unique values.
  • The values range from 0 to FFFFFFFF.
General example is
SELECT HASHROW(Emp_no) AS HASHROW_VALUE FROM Emp_table;

Result:
HASHROW_VALUE
------------
717A8724

HASHBUCKET Function

Basic Syntax for HASHBUCKET is

HASHBUCKET( [ ] )

Example
SELECT HASHBUCKET(NULL) AS NULL_BUCKET
                ,HASHBUCKET()    AS NO_BUCKET;






  • The HASHBUCKET function produces 16bit binary Hash Bucket  that is used with the Hash Map to determine the AMP that should store and retrieve the data row
  • The values range from 0 to 1,048,575,not counting the NULL as a potential result.

General Example
SELECT COUNT(*) AS NBR_ROWS
               ,HASHBUCKET(HASHROW (Emp_No)) AS Bucket_No
               FROM Emp_Table
                GROUP BY 2 ;


NBR_ROWS Bucket_No
 1   388963
1   1560021
1   683238
1   53025
1   1877998
1   795490
1   410221
1   1048185
1   804626
1   317398


HASHAMP Function
Basic Synatx for HASHAMP is
HASHAMP( )

Example
SELECT HASHAMP(NULL) AS NULL_BUCKET
                ,HASHAMP ()    AS NO_BUCKET;
  • The HASHAMP function returns the identification number of the primary AMP for any Hash Bucket number.
  • When no value is passed to the HASHAMP function, it returns a number that is one less than the number of AMPs in the current system configuration
 General Example
SELECT Emp_no   
       ,HASHBUCKET(HASHROW(Emp_no)) AS Bucket_No
       ,HASHAMP(HASHBUCKET(HASHROW(Emp_no))) AS AMP_No
       FROM Emp_Table
       ORDER BY 1;


Emp_no   Bucket_No   AMP_No
1777   973955   120
4972   93325   41
7524   540321   56
21048   957958   69
21048   957958   69
24402   707988   65
26346   9707   71
27470   471190   15
29157   613408   25
29775   452863   120


HASHBAKAMP Function
Basic Syntax for HASHBAKAMP is 
HASHBAKAMP ( )

ExampleSELECT HASHBAKAMP (NULL) AS NULL_BUCKET
               ,HASHBAKAMP () AS NO_BUCKET;









The HASHBAKAMP function returns the identification number of the Fallback AMP for any Hash Bucket number

General Example
SELECT Emp_no    
       ,HASHBUCKET(HASHROW(Emp_no)) AS Bucket_No
       ,HASHBAKAMP(HASHAMP(HASHBUCKET(HASHROW(Emp_no)))) AS BAK_AMP_No
       FROM Emp_Table
       ORDER BY 1;


Emp_noBucket_NoBAK_AMP_No
222643993188
646797764594
2411522255892
2440270798894
2531627721894
2549929077195
2940552862893
4011594403895
4011789260286
4056441001485
Continue Reading...

Explain Plan

Teradata_explain_plan
What is EXPLAIN?
EXPLAIN statement is the Parsing Engines (PE’s) plan to the AMPs.A step-by-step
analysis of the queries being executed in the database.
The EXPLAIN facility provides an "English" translation of the plan the SQL Optimizer develops to service a request.

The EXPLAIN is an excellent facility for the following activities:
• Determining Access Paths
• Validating the use of indexes
• To determine locking profiles
• Showing Triggers and Join Index access
• Estimate the query runtime

Sample Syntax for Explain is.
EXPLAIN Select Emp_ID, Emp_Name from Emp_Table ;

If we pass an explain command in front of a query statement or Simply Press F6 key, there are a several estimated confidence messages that the optimizer will relay to the user is

High Confidence - Statistics available on an Index or Column.
Low Confidence - Random sampling of INDEX, or Statistics available butAND/OR condition exists.
No Confidence - Random sampling based on AMP row count. No statistics are collected.

Understanding the EXPLAIN Terminology you should know the Keywords.

KeywordExplanation
Locking Pseudo TableSerial lock on a symbolic table. Every table has one. Used to prevent deadlocks situations between users.
Locking table forIndicates that an ACCESS, READ, WRITE, or EXCLUSIVE lock has been placed on the table
Locking rows for Indicates that an ACCESS, READ, or WRITE, lock is placed on rows read or written
Do an ABORT testGuarantees a transaction is not in progress for this user
All AMPs retrieveAll AMPs are receiving the AMP steps and are involved in providing the answer set
By way of an all rows scanRows are read sequentially on all AMPs
By way of primary indexRows are read using the Primary index column(s)
By way of index numberRows are read using the Secondary index – number from HELP INDEX
BMSMSBit Map Set Manipulation Step, alternative direct access technique when multiple NUSI columns are referenced in the WHERE clause
Residual conditionsWHERE clause conditions, other than those of a join
Eliminating duplicate rowsProviding unique values, normally result of DISTINCT, GROUP BY or subquery
Where unknown comparison will be ignoredIndicates that NULL values will not compare to a TRUE or FALSE. Seen in a subquery using NOT IN or NOT = ALL because no rows will be returned on ignored comparison.
Nested joinThe fastest join possible. It uses a UPI to retrieve a single row after using a UPI or a USI in the WHERE to reduce the join to a single row.
Merge joinRows of one table are matched to the other table on common domain columns after being sorted into the same sequence, normally Row Hash
Product joinRows of one table are matched to all rows of another table with no concern for domain match
ROWID joinA very fast join. It uses the ROWID of a UPI to retrieve a single row after using a UPI or a USI in the WHERE to reduce the join to a single row.
Duplicated on all AMPsParticipating rows for the table (normally smaller table) of a join are duplicated on all AMPS
Hash redistributed on all AMPsParticipating rows of a join are hashed on the join column and sent to the same AMP that stores the matching row of the table to join
SMSSet Manipulation Step, result of an INTERSECT, UNION, EXCEPT or MINUS operation
Last useSPOOL file is no longer needed after the step and space is released
Built locally on the AMPsAs rows are read, they are put into SPOOL on the same AMP
Aggregate Intermediate Results computed locallyThe aggregation values are all on the same AMP and therefore no need to redistribute them to work with rows on other AMPs
Aggregate Intermediate Results computed globallyThe aggregation values are not all on the same AMP and must be redistributed on one AMP, to accompany the same value with from the other AMPs


Below are the some examples

Example for FULL TABLE SCAN    

EXPLAIN SELECT * FROM Emp_Table;

1. First, we lock a distinct SQL_CLASS."pseudo table" for read on a RowHash to prevent global deadlock for SQL_CLASS.Emp_Table.

2. Next, we lock SQL_CLASS.Emp_Table for read.

3. We do an all-AMPs RETRIEVE step from SQL_CLASS.Emp_Table by way of an all-rows scan with no residual conditions into Spool 1 (group_amps), which is built locally on the AMPs. The size of Spool 1 is estimated with low confidence to be 6 rows (342 bytes). The estimated time for this step is 0.03 seconds.

4. Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request.
-> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0.03 seconds.

If you see all-AMPs RETRIEVE by way of an all-rows scan in your plan, that means teradata is doing a Full Table Scan. So it is reading every row in the table.


Explain Example – Unique Primary Index (UPI)

 EXPLAIN SELECT * FROM Emp_Table WHERE Emp_No = 123000;

 1. First, we do a single-AMP RETRIEVE step from SQL_CLASS.Emp_Table by way of the unique primary index "SQL_CLASS.Emp_Table.Emp_No = 123000" with no residual conditions. The estimated time for this step is 0.01 seconds.
-> The row is sent directly back to the user as the result of statement 1. The total estimated time is 0.01 seconds.


In the Emp_table, Emp_no is the Unique Primary Index. So USI used in where clause. So it gives Single-AMP retrieve by way of the Unique Primary Index. This is the fastest query!

Explain Example – Non-Unique Primary Index (NUPI)
 EXPLAIN SELECT * FROM Sales_Table WHERE Product_ID = 1000;

 1. First, we do a single-AMP RETRIEVE step from SQL_CLASS.Sales_Table by way of the primary index "SQL_CLASS.Sales_Table.Product_ID = 1000" with no residual conditions into Spool 1 (one-amp), which is built locally on that AMP. The size of Spool 1 is estimated with low confidence to be 2 rows (66 bytes). The estimated time for this step is 0.02 seconds.
-> The contents of Spool 1 are sent back to the user as the result of statement 1. The total estimated time is 0.02 seconds.


Explain Example – Unique Seconday Index (USI)
Explain SELECT * FROM Emp_Table WHERE
First_name="Sachin" and
Last_name=""tendulkar"

1. First, we do a two-AMP RETRIEVE step from SQL_CLASS.Emp_Table by way of unique index # 12 "SQL_CLASS.Emp_Table.Last_name = 'tendulkar ' , SQL_CLASS.Emp_Table.First_name = 'Sachin'" with no residual conditions. The estimated time for this step is 0.01 seconds.
->The row is sent directly back to the user as the result of statement 1. The total estimated time is 0.01 seconds.


Explain Example – Redistributed to All-AMPs
 
  EXPLAIN SELECT Emp.*, Dept**
         FROM Emp_Table as Emp
           INNER JOIN
          Department_Table as Dept
          ON Emp.Dept_No = Dept.Dept_No;


 4. We do an all-AMPs RETRIEVE step from SQL_CLASS.Emp by way of an all-rows scan with a condition of ("NOT (SQL_CLASS.Emp.Dept_No IS NULL)") into Spool 2 (all_amps), which is redistributed by the hash code of (SQL_CLASS.Emp.Dept_No) to all AMPs. Then we do a SORT to order Spool 2 by row hash. The size of Spool 2 is estimated with low confidence to be 6 rows (294 bytes). The estimated time for this step is 0.01 seconds.
 

5 We do an all-AMPs JOIN step from SQL_CLASS.Dept by way of a RowHash match scan, which is joined to Spool 2 (Last Use) by way of a RowHash match scan. SQL_CLASS.Dept and Spool 2 are joined using a merge join, with a join condition of ("Dept_No = SQL_CLASS.Dept.Dept_No").

 Hope you understood about EXPLAIN.
Continue Reading...

CUBE Function

Teradata Cube Function
I hope you all familiar with ROLLUP Function now. If not please read before get into CUBE Function.
  • The CUBE function can be called as Superset of RollUP function.
  • Because It returns the same result set as ROLLUP along with provides some additional information.
  • Instead producing a 'hierarchy' of results, CUBE looks at all permutations of the specified dimensions.
Let take an example , Produce a 'CUBE' report showing employee salaries across the dimensions of department and manager.

SELECT DEPT_NO AS DEPTNO
 ,MGR_EMP_NO AS MANAGER
 ,SUM(SALARY_AMOUNT) AS SUMSAL
 FROM EMPLOYEE
 WHERE DEPT_NO < 402
GROUP BY CUBE (DEPT_NO,MGR_EMP_NO)
 ORDER BY 1,2;


DEPTNOMANAGERSUMSAL
??591925(all depts, all managers)
?801286750(all depts, one manager)
?1003207725
?101958700
?102538750
100?100000(one dept, all managers)
100801100000(one dept, one manager)
201?73450(one dept, all managers)
20180134700
201102538750
301?116400
30180157700
301101958700
302?56500
30280156500
401?245575
40180137850
4011003207725

If you observe there are four additional rows (green) are included in the output.
These four rows represent the totals by manager.This report shows all possible permutations of the two specified dimensions.

 Reversing CUBE Parameters:
 What happens when we flip the sequence of the CUBE parameters? Consider the following:

 SELECT DEPT_NO AS DEPTNO
 ,MGR_EMP_NO AS MANAGER
 ,SUM(SALARY_AMOUNT) AS SUMSAL
 FROM EMPLOYEE
 WHERE DEPT_NO < 402
GROUP BY CUBE (MGR_EMP_NO,DEPT_NO)
 ORDER BY 1,2;

Yes, The output is exactly  returns same.There is no difference in output becacuse CUBE function looks at all permutations of the specified parameters, order of the arguments doesn't matter - unlike ROLLUP where it does matter.

 we can use the GROUPING and COALESCE functions to make the report more readable.

 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
 ,SUM(salary_amount) AS SumSal
 FROM employee
 WHERE department_number < 402
 GROUP BY CUBE (manager_employee_number,department_number)
 ORDER BY 1,2;
 

DeptnoMngrSumSal
100801100000
100All Managers100000
20180134700
201102538750
201All Managers73450
30180157700
301101958700
301All Managers116400
30280156500
302All Managers56500
40180137850
4011003207725
401All Managers245575
All Depts801286750
All Depts1003207725
All Depts101958700
All Depts102538750
All DeptsAll Managers591925

As more dimensions are added to the cube, more permutations and thus more rows will result.

 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
 etc.
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;


DeptnoMngrJobSumSal
100801111100100000
100801All Job Codes100000
100All ManagersAll Job Codes100000
100All Managers111100100000
20180121110034700
201801All Job Codes34700
2011025All Job Codes38750
201102522210138750
201All Managers21110034700
201All ManagersAll Job Codes73450
201All Managers22210138750
301801All Job Codes57700
30180131110057700
3011019All Job Codes58700
301101931210129450
301101931210229250
301All Managers31210229250
301All Managers31110057700
301All ManagersAll Job Codes116400
301All Managers31210129450
All Depts801All Job Codes192400
All Depts801111100100000
All Depts80121110034700
All Depts80131110057700
All Depts101931210129450
All Depts1019All Job Codes58700
All Depts101931210229250
All Depts1025All Job Codes38750
All Depts102522210138750
All DeptsAll ManagersAll Job Codes289850
All DeptsAll Managers31110057700
All DeptsAll Managers21110034700
All DeptsAll Managers111100100000
All DeptsAll Managers31210129450
All DeptsAll Managers31210229250
All DeptsAll Managers22210138750
All DeptsAll Managers31210229250
All DeptsAll Managers22210138750

To help interpret the output, use the following color-coding scheme:

(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

Continue Reading...

Teradata Facts


Below are some of the hard facts about Teradata Database and Limitations between versions.

    • Each AMP holds a portion of the rows for every table in the system.
    • Each Table has a Primary Index that is Unique or Non-Unique
    • The Hash Map Determines which AMP will own the Row
    • A Unique Primary Index Spreads the Data Evenly
    • The AMP Adds a Uniqueness Value to Create the Row-ID
    • Each AMP Sorts Their Tables by the Row-ID
    • A Non-Unique Primary Index Skews the Data
    • Unique Primary Index Queries are a Single AMP Retrieve
    • A Non-Unique Primary Index is also a Single AMP Retrieve
    • Teradata has a No Primary Index Table called a NoPI Table
    • There are Normal Tables and then There are Partitioned Tables
    • Partitioning is Designed to Eliminate the Full Table Scan
    • A Partition # and Row-ID = Row Key
    • An AMP Stores its Rows Sorted in only Two Different Ways
    • AMPs Moves Their Data Blocks into Memory to Read/Write
    • Most Taxing thing for an AMP is Moving Blocks into Memory
    • Rows are Stored in Data Blocks which are stored in Cylinders
    • Rows for an AMP Stored Inside a Data Block in a Cylinder
    • An AMP's Master Index is Used to Find the Right Cylinder
    • There is One Master Index and Thousands of Cylinder Indexes
    • The Row Reference Array is always in Row-ID order
    • The maximum block size (before a split) was 255 sectors (127.5 K), but now it is 2047 sectors (1 MB).
    • A Block Splits into Two Blocks at Maximum Block Size.
    • Once the block grows to the maximum size, then Teradata will split the single block into two separate blocks. The default for the split is 2047 sectors or 1 MB.
    • The maximum for all Teradata V14 data block splits are 2047 sectors or 1 MB, but the defaults differ based on whether or not it is an enterprise class system, or an appliance
    • Each Table has a 48-bit TableID
      Differences in Teradata vesrions V12, V13 and V14



        Versions
        121314
        Max Vdisk Size1.26 TB1.26 TB7.2 TB
        Max Block Size (sectors)255255255
        Max Table Header Size128 KB1 MB1 MB
        Max Databases/Users4.2 Billion4.2 Billion4.2 Billion
        Max Partitions65,53565,5359.2 * 10 18
        Max Partition Levels151562
        Max spool tables per query204820482048
        Max Tables in a Join64128128
        Max columns in a table204820482048
        Max columns in an index646464
        Max SQL request size1 MB1 MB1 MB
        Max AMPs and PEs16,38416,38432,720
        Continue Reading...

        ROLLUP Function

        Teradata-rollup-function
        The teradata ROLLUP function is used, when ever an aggregation is required across all levels of a hierarchy within a single dimension.

        Lets take geography is a dimension. You might have 3 levels of hierarchy - city, county and state. You may wish to produce sum of revenue amounts across all three levels.

        we can produce below samples 
        • Produce a sum of revenue amounts across all three levels.
        • Aggregate at the lowest level (city) and 'rollup' those aggregations to the next level (county) and finally rollup to the top level (state).
        • Provide a grand total across all three levels.
        All of these are easily done with the ROLLUP feature.

        Example1:
        Using the GROUP BY 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 < 402
        GROUP BY 1
        ORDER BY 1;



        department_number

        Group Sum(salary_amount)

        100

        100000

        201

        73450

        301

        116400

        302

        56500

        401

        245575


        ROLLUP Function - One Level
        Using the ROLLUP function, produce salary totals by department.

        SELECT department_number
        ,SUM(salary_amount)
        FROM employee
        WHERE department_number < 402
        GROUP BY ROLLUP (department_number)
        ORDER BY 1;



        department_number

        Group Sum(salary_amount)

        ?

        591925

        100

        100000

        201

        73450

        301

        116400

        302

        56500

        401

        245575

        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.

        ROLLUP Function - Two Levels

        Produce a hierarchy of total salaries by department within manager.

        SELECT manager_employee_number AS Mgr
        ,department_number AS Dept
        ,SUM(salary_amount) AS SumSal
        FROM employee
        WHERE department_number < 402
        GROUP BY ROLLUP (manager_employee_number,department_number)
        ORDER BY 1,2;




        Mgr

        Dept

        SumSal



        ?

        ?

        591925

        (all mgrs,all depts,all emps)

        801

        ?

        286750

        (mgr 801,all depts, 5 emps)

        801

        100

        100000

        (mgr 801,dept 100, 1 emp)

        801

        201

        34700

        (mgr 801,dept 201, 1 emp)

        801

        301

        57700

        (mgr 801,dept 301, 1 emp)

        801

        302

        56500

        (mgr 801,dept 302, 1 emp)

        801

        401

        37850

        (mgr 801,dept 401, 1 emp)

        1003

        ?

        207725

        (mgr 1003,all depts, 6 emps)

        1003

        401

        207725

        (mgr 1003,dept 401, 6 emps)

        1019

        ?

        58700

        (mgr 1019,all depts, 2 emps)

        1019

        301

        58700

        (mgr 1019,dept 301, 2 emps)

        1025

        ?

        38750

        (mgr 1025,all depts, 1 emp)

        1025

        201

        38750

        (mgr 1025,dept 201, 1 emp)


        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.

        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.

        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);

        SELECT department_number
        ,SUM(salary_amount)
        FROM employee
        GROUP BY ROLLUP (department_number)
        ORDER BY 1;


        department_number

        Group Sum(salary_amount)

        ?

        651925

        ?

        60000

        100

        100000

        201

        73450

        301

        116400

        302

        56500

        401

        245575

        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.

        Continue Reading...