Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Teradata SQL GLOBAL Temporary Vs VOLATILE Temporary Vs Derived tables WITH and WITH BY Special Index function Trigger Online Analytical Function.

Similar presentations


Presentation on theme: "Advanced Teradata SQL GLOBAL Temporary Vs VOLATILE Temporary Vs Derived tables WITH and WITH BY Special Index function Trigger Online Analytical Function."— Presentation transcript:

1 Advanced Teradata SQL GLOBAL Temporary Vs VOLATILE Temporary Vs Derived tables WITH and WITH BY Special Index function Trigger Online Analytical Function Ordered Analytical function

2 Global and Volatile Temporary Tables
Temporary tables are an additional tool that assist us in performing SQL operations that either: Might not be possible against a normalized table, or May require multiple SQL statements to complete. There are three types of temporary tables implemented in Teradata: Global Volatile Derived

3 Derived Tables Some characteristics of a derived table include:
Local to the query - it exists for the duration of the query. When the query is done the table is discarded. Incorporated into SQL query syntax. Spool rows are also discarded when query finishes. There is no data dictionary involvement - less system overhead. Example SELECT t.prodid, t.sumsales, RANK(t.sumsales) FROM (SELECT prodid, SUM(sales) FROM salestbl GROUP BY 1) AS t(prodid, sumsales) QUALIFY RANK(sumsales)<=3;

4 Volatile Temporary Tables
Volatile tables have a lot of the advantages of derived tables, and additional benefits such as: Local to a session - it exists throughout the entire session, not just a single query. It must be explicitly created using the CREATE VOLATILE TABLE syntax. It is discarded automatically at the end of the session. There is no data dictionary involvement. Example CREATE VOLATILE TABLE vt_deptsal, LOG (deptno SMALLINT ,avgsal DEC(9,2) ,maxsal DEC(9,2) ,sumsal DEC(9,2) ,empcnt SMALLINT) ON COMMIT PRESERVE ROWS;

5 Global Temporary Tables
The major difference between a global temporary table and a volatile temporary table is that the global table has a definition in the data dictionary, thus the definition may be shared by many users. Each user session can materialize its own local instance of the table. Attributes of a global temporary table include: Local to a session, however each user session may have its own instance. Uses CREATE GLOBAL TEMPORARY TABLE syntax. Materialized instance of table discarded at session end. Creates and keeps table definition in data dictionary. Example CREATE SET GLOBAL TEMPORARY TABLE PED.gt_deptsal ,NO FALLBACK,LOG ( deptno SMALLINT , avgsal DECIMAL(9,2)) PRIMARY INDEX (deptno ) INDEX ( empcnt) ON COMMIT PRESERVE ROWS;

6 Exercise Can we COLLECT STATISTICS on GLOBAL TEMP Table, volatile temp table, derived tables.

7 WITH...BY for Subtotals The WITH...BY clause is a Teradata extension that creates subtotal lines for a detailed list. The WITH...BY clause allows subtotal "breaks" on more than one column and generates an automatic sort on all "BY" columns. Creating a Report Using WITH..BY Problem To display employee name and salary with subtotals by department. Solution SELECT last_name AS NAME ,salary_amount AS SALARY ,department_number AS DEPT FROM employee WITH SUM(salary) BY DEPT ;

8 Result NAME SALARY DEPT Stein Kanieski Sum(Salary) Johnson Trader Sum(Salary) Villegas Ryan Sum(Salary) NOTE: "WITH BY" is a non-ANSI Teradata extension. It is supported by BTEQ, but not supported by ODBC clients.

9 Creating Final Totals Using WITH
The WITH clause without 'BY' is used to create a grand totals. Problem Display employee numbers and salary amounts for department 301 and a final total for the department. Solution SELECT employee_number ,salary_amount FROM employee WHERE department_number = 301 WITH SUM(salary_amount) (TITLE 'GRAND TOTAL') ORDER BY employee_number ; Result employee_number    salary_amount   GRAND TOTAL NOTE:WITH is a non-ANSI Teradata extension and is not supported by ODBC clients.

10 DISTINCT Modifier Use the DISTINCT modifier is used in conjuction with the COUNT aggregate to prevent the same value from being counted more than once. Problem Count the number of managers for employees numbered between 1003 and 1008. Solution SELECT employee_number ,department_number ,manager_employee_number AS manager FROM employee WHERE employee_number BETWEEN 1003 AND 1008 WITH COUNT (DISTINCT manager) (TITLE 'TOTAL MANAGERS') ; Result employee_number   department_number   manager    TOTAL MANAGERS 4 This gives you an actual count for the number of managers.

11 Legal Example: SELECT COUNT(DISTINCT(job_code))        ,COUNT(DISTINCT(employee_number)).....
Illegal Example: SELECT COUNT(DISTINCT(job_code, employee_number))

12 Combining WITH and WITH BY
WITH and WITH BY may be combined within the same query. Problem Show the salary for each employee with subtotals by department, a final total, and results sorted by employee name.  Solution SELECT last_name AS NAME ,salary_amount AS SALARY ,department_number AS DEPT FROM employee WHERE employee_number BETWEEN 1003 and 1008 WITH SUM (SALARY) BY DEPT WITH SUM (SALARY) (TITLE 'GRAND TOTAL') ORDER BY NAME ;

13 Results NAME  SALARY     DEPT Kanieski     Stein        301      Sum (Salary)             Johnson Trader                  Sum (Salary)             Ryan Villegas   403   Sum (Salary)               GRAND TOTAL      

14 Summary of WITH...BY and WITH
Will provide subtotals, subcounts, and subaverages, and also show detail rows. Summarylist can specify more than one column. Breaklist can specify more than one column. Implied ORDER BY on the breaklist columns. WITH...BY determines the major sort key(s). ORDER BY specifies any additional minor sorts. An SQL statement can have several WITH...BY clauses. The highest level of sort is the last specified WITH...BY clause. Is not supported by ANSI standard or ODBC clients. WITH : The WITH clause produces grand total results for the entire answer set. This clause is typically used when you need to produce final totals and also wish to see the detail rows. Will provide a final or grand total, count or average. Summary list may specify more than one column. Only a single WITH is allowed in a query. WITH must follow any WITH...BY syntax. Not supported by ANSI standard or ODBC clients.

15 Special Index Functions
Create an 'employee' table with a NUSI on the job code. CREATE SET TABLE employee ,FALLBACK , ( employee_number INTEGER, manager_employee_number INTEGER, department_number INTEGER, job_code INTEGER, last_name CHAR(20) NOT NULL, first_name VARCHAR(30) NOT NULL, hire_date DATE FORMAT 'YY/MM/DD' NOT NULL, birthdate DATE FORMAT 'YY/MM/DD' NOT NULL, salary_amount DECIMAL(10,2) NOT NULL) UNIQUE PRIMARY INDEX ( employee_number ) INDEX (job_code); CREATE INDEX (job_code) ON employee;

16 Drop the NUSI on the job code column of the 'employee' table.
DROP INDEX (job_code) ON employee; Value Ordered NUSI's CREATE SET TABLE employee ,FALLBACK , ( employee_number INTEGER, manager_employee_number INTEGER, department_number INTEGER, job_code INTEGER, last_name CHAR(20) NOT NULL, first_name VARCHAR(30) NOT NULL, hire_date DATE FORMAT 'YY/MM/DD' NOT NULL, birthdate DATE FORMAT 'YY/MM/DD' NOT NULL, salary_amount DECIMAL(10,2) NOT NULL) UNIQUE PRIMARY INDEX ( employee_number ) INDEX (job_code) ORDER BY VALUES (job_code);

17 Create a value-ordered NUSI on the job code column of existing 'employee' table.
CREATE INDEX (job_code) ORDER BY VALUES (job_code) ON employee; Limitations of Value-Ordered NUSI's A column defined as a value-ordered index column must be: A single column A column which is a part of or all of the index definition A numeric column – non-numeric are not allowed No greater than four bytes in length – INT, SMALLINT, BYTEINT, DATE, DEC are valid

18 Triggers A trigger is an object in a database, like a macro or view. A trigger is created with a CREATE TRIGGER statement and defines events that will happen when some other event, called a triggering event, occurs. A trigger consists of one or more SQL statements which are associated with a table and which are executed when the trigger is 'fired'. All of the following statements are valid with triggers: CREATE TRIGGER DROP TRIGGER SHOW TRIGGER ALTER TRIGGER RENAME TRIGGER REPLACE TRIGGER HELP TRIGGER NOTE: Join indexes are never permitted on tables which have defined triggers.

19 Triggered and Triggering Statements
A trigger is said to ‘fire’ when the triggering event occurs and various conditions are met. When a trigger fires, it causes other events, called triggered events to occur. A triggered event consists of one or more triggered statements. A triggering statement is an SQL statement which causes a trigger to fire. It is the 'launching' statement.

20

21 Online Analytical Functions
OLAP stands for On-Line Analytical Processing. The new OLAP functions available with Teradata V2R5 permit data mining on a database via SQL. These functions include: RANK - (Rankings) QUANTILE - (Quantiles) CSUM - (Cumulation) MAVG - (Moving Averages) MSUM - (Moving Sums) MDIFF - (Moving Differences) MLINREG - (Moving Linear Regression) OLAP functions are similar to aggregate functions in that they: Operate on groups of rows (like the GROUP BY clause) Can filter groups using QUALIFY (like the HAVING clause) OLAP functions are unlike aggregate functions because they: Return a data value for each qualifying row - not group May not be performed within subqueries OLAP functions may be performed on the following database objects or activities: Tables (Perm, Temp, Derived) Views INSERT/SELECT populations

22 Cumulative Sum Cumulative sum (CSUM) computes a running or cumulative total of a column’s value. The syntax is: CSUM(colname, sort list) The 'daily_sales' table is used in many of the subsequent examples and its definition is repeated here for convenience. CREATE SET TABLE daily_sales ,NO FALLBACK ,NO BEFORE JOURNAL ,NO AFTER JOURNAL ( itemid INTEGER ,salesdate DATE FORMAT 'YY/MM/DD' ,sales DECIMAL(9,2)) PRIMARY INDEX ( itemid );

23 Problem Create a running daily total for item 10 for Jan and Feb 1998.
Solution SELECT salesdate, sales, CSUM(sales, salesdate) FROM daily_sales WHERE salesdate BETWEEN AND AND itemid = 10; Result salesdate sales Csum 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ 98/02/ 98/02/ 98/02/ 98/02/ 98/02/ 98/02/

24 Moving Averages The Moving Average (MAVG) function permits the calculation of a moving average on a specified column, based on a defined number of rows known as the query width. The sortlist column provides the column(s) for the ascending sort of the final result. This function computes the moving AVG of a column based on some number of preceding rows. The syntax for Moving Averages is: MAVG(colname, n, sortlist) colname = the column on which the moving average is computed n = the number of rows (< 4096) which will be used in the calculation including the current row ('n' is also refered to as the 'width' of the average) sortlist = the column(s) which determine the sequencing of the rows

25 Problem Show a moving average on a 7 day window for sales of item 10.
SELECT salesdate,itemid, sales, MAVG(sales, 7, salesdate) WHERE itemid = 10 FROM daily_sales; Result salesdate itemid sales MAvg 98/01/ 98/01/ Avg of the first 2 rows 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ Avg of the first 7 rows 98/01/ 98/02/ 98/02/

26 Moving Sums The syntax for using Moving Sums is:
MSUM(colname, n, sortlist) Problem Show a moving sum on a three day window for sales of item 10. Solution SELECT salesdate, itemid, sales, MSUM(sales, 3, salesdate) WHERE itemid = 10 FROM daily_sales; Result salesdate itemid sales MSum 98/01/ 98/01/ Sum of 2 rows 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ Sum of 3 rows 98/01/ 98/02/

27 Moving Differences The Moving Difference (MDIFF) function permits a calculation of a moving difference of a specified column, based on a defined query width (n). The syntax for MDIFF is: MDIFF(colname, n, sortlist) Problem Show a moving difference on a three day window for sales of item 10. Solution SELECT salesdate, itemid, sales, MDIFF(sales, 3, salesdate) FROM daily_sales; Result salesdate itemid sales MDiff 98/01/ ? 98/01/ ? 98/01/ ? 98/01/ Difference of 2 rows 98/01/ 98/01/ 98/01/ 98/01/ 98/02/ OO 98/02/ Difference of 2 rows 98/02/

28 Cumulative Sum Cumulative sum (CSUM) computes a running or cumulative total of a column’s value. The syntax is: CSUM(colname, sort list) The 'daily_sales' table is used in many of the subsequent examples and its definition is repeated here for convenience. CREATE SET TABLE daily_sales ,NO FALLBACK ,NO BEFORE JOURNAL ,NO AFTER JOURNAL ( itemid INTEGER ,salesdate DATE FORMAT 'YY/MM/DD' ,sales DECIMAL(9,2)) PRIMARY INDEX ( itemid );

29 Problem Create a running daily total for item 10 for Jan and Feb 1998.
Solution SELECT salesdate, sales, CSUM(sales, salesdate) FROM daily_sales WHERE salesdate BETWEEN AND AND itemid = 10; Result salesdate sales Csum 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ 98/02/ 98/02/ 98/02/ 98/02/ 98/02/ 98/02/

30 Moving Averages The Moving Average (MAVG) function permits the calculation of a moving average on a specified column, based on a defined number of rows known as the query width. The sortlist column provides the column(s) for the ascending sort of the final result. This function computes the moving AVG of a column based on some number of preceding rows. The syntax for Moving Averages is: MAVG(colname, n, sortlist) colname = the column on which the moving average is computed n = the number of rows (< 4096) which will be used in the calculation including the current row ('n' is also refered to as the 'width' of the average) sortlist = the column(s) which determine the sequencing of the rows

31 Problem Show a moving average on a 7 day window for sales of item 10.
SELECT salesdate,itemid, sales, MAVG(sales, 7, salesdate) WHERE itemid = 10 FROM daily_sales; Result salesdate itemid sales MAvg 98/01/ 98/01/ Avg of the first 2 rows 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ Avg of the first 7 rows 98/01/ 98/02/ 98/02/

32 Moving Sums The syntax for using Moving Sums is:
MSUM(colname, n, sortlist) Problem Show a moving sum on a three day window for sales of item 10. Solution SELECT salesdate, itemid, sales, MSUM(sales, 3, salesdate) WHERE itemid = 10 FROM daily_sales; Result salesdate itemid sales MSum 98/01/ 98/01/ Sum of 2 rows 98/01/ 98/01/ 98/01/ 98/01/ 98/01/ Sum of 3 rows 98/01/ 98/02/

33 Moving Differences The Moving Difference (MDIFF) function permits a calculation of a moving difference of a specified column, based on a defined query width (n). The syntax for MDIFF is: MDIFF(colname, n, sortlist) Problem Show a moving difference on a three day window for sales of item 10. Solution SELECT salesdate, itemid, sales, MDIFF(sales, 3, salesdate) FROM daily_sales; Result salesdate itemid sales MDiff 98/01/ ? 98/01/ ? 98/01/ ? 98/01/ Difference of 2 rows 98/01/ 98/01/ 98/01/ 98/01/ 98/02/ OO 98/02/ Difference of 2 rows 98/02/

34 Ordered Analytic Functions
ROW_NUMBER Function The ROW_NUMBER function returns the sequential row number of a group starting with the number one. ROW_NUMBER: is the same as ANSI RANK function except in the event of a tie. doesn't report duplicate values, unlike RANK.

35 Example SELECT storeid, prodid, sales, ROW_NUMBER() OVER (PARTITION BY storeid ORDER BY sales DESC)AS rank_sales FROM salestbl QUALIFY rank_sales <= 3; storeid prodid sales rank_sales F A C A C D B D A

36 SUM Window Group Function
SELECT storeid, prodid, sales, SUM(sales) OVER (PARTITION BY prodid ORDER BY sales DESC) FROM salestbl ; storeid prodid sales Group Sum(sales) A A A B C C C D D D F

37 SUM Window Cumulative Function
SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESC ROWS UNBOUNDED PRECEDING) FROM salestbl ; storeid prodid sales Cumulative Sum(sales) F A B C D A D C A D C

38 SUM Window Moving Function
SELECT storeid, prodid, sales, SUM(sales) OVER (ORDER BY sales DESC ROWS 2 PRECEDING) FROM salestbl; storeid prodid sales Moving Sum(sales) F A B C D A D C A D C


Download ppt "Advanced Teradata SQL GLOBAL Temporary Vs VOLATILE Temporary Vs Derived tables WITH and WITH BY Special Index function Trigger Online Analytical Function."

Similar presentations


Ads by Google