Teradata Advanced OLAP

functions

RANK function
The RANK function is a Teradata specific function which assigns a
ranking order to rows in a qualified answer set
Example:
Arrange the employees according to their employee number. Assign ranks to
them based to the same.

empno

ename

1

vidhya

2

badri

3

ravikanth

4

priti

5

anjan

6

francis

7

srikanth

Query:
select empno, ename,rank() OVER ( ORDER BY empno ) AS RN
from emp
The ORDER BY clause in this construct is associated with the RANK function. A
rank by definition implies an ordering on a particular column or expression. The
ORDER BY clause tells on what is being ranked

Output:

empno

ename

RN

1

vidhya

1

2

badri

2

3

ravikanth

3

4

priti

4

5

anjan

5

6

francis

6

7

srikanth

7

ROW_NUMBER function
The ROW_NUMBER function returns the sequential row number of a group
starting with the number one
The ROW_NUMBER function returns the sequential row number of a group
starting with the number one.
The ROW_NUMBER function:
is the same as the ANSI RANK function except in the event of a tie.
doesn't report duplicate values like RANK

Trianz, Inc. Confidential

4

Query: select empno.ROW_NUMBER() OVER (ORDER BY empno DESC) AS RN from emp 9 8 7 6 5 4 3 2 1 srikanth badri srikanth francis anjan priti ravikanth badri vidhya 1 2 3 4 5 6 7 8 9 . ename.

00 14 . sales.00 11 10 2004-02-01 150. § SELECT itemid. § §  itemid salesdate sales Rank(sales) ----------.00 3 10 2004-02-06 350. salesdate.00 11 10 2004-01-01 150.00 11 10 2004-01-31 100.00 4 10 2004-01-05 350.00 9 10 2004-01-21 150.00 1 10 2004-02-20 450.----------10 2004-01-10 550.00 4 10 2004-02-27 350.00 1 10 2004-02-17 550.00 4 10 2004-01-03 250.00 7 10 2004-01-25 200. RANK() OVER (ORDER BY sales DESC) WHERE salesdate BETWEEN DATE '2004-01-01' AND DATE '2004-03-01‘ § AND itemid = 10 FROM daily_sales_2004 .Rank sales of item 10 by day between the two specified dates using the Row_Number function.00 9 10 2004-01-02 200.00 7 10 2004-02-03 250.-------------------.

00 9 10 2004-01-02 200.00 7 10 2004-02-03 250. ROW_NUMBER() OVER (ORDER BY sales DESC) WHERE salesdate BETWEEN DATE '2004-01-01' AND DATE '2004-03-01' AND itemid = 10 FROM daily_sales_2004.00 4 10 2004-02-27 350.---------.00 8 10 2004-01-25 200.00 11 10 2004-02-01 150.00 12 10 2004-01-01 150. sales.00 1 10 2004-02-17 550.00 5 10 2004-01-05 350. salesdate.-----------10 2004-01-10 550.  SELECT itemid.Rank sales of item 10 by day between the two specified dates using the Row_Number function.00 13 10 2004-01-31 100.----------.00 3 10 2004-02-06 350.00 6 10 2004-01-03 250.00 2 10 2004-02-20 450.00 14 .    § itemid salesdate sales Row_Number() ----------.00 10 10 2004-01-21 150.

Query: select empno. ename. 5 anjan 1 2 badri 1 8 badri 2 6 francis 1 4 priti 1 3 ravikanth 1 7 srikanth 1 9 srikanth 2 1 vidhya 1 .rank() OVER (partition by ename ORDER BY empno ) AS RN from emp So within each non-unique names. the rank will be calculated and arranged By employee number.RANK OVER PARTITION Find out the rank by partitioning by employee names and order by emp no.

empno 5 2 6 4 3 7 1 ename anjan badri francis priti ravikanth srikanth vidhya RN 1 1 1 1 1 1 1 . ename.rank() OVER ( partition by ename ORDER BY empno ) AS RN from emp qualify RN <2 Qualify is supposed to be used instead of where function.Qualify function: select empno.

2) Insert values as below empno 7 9 5 3 1 8 6 4 2 ename srikanth srikanth anjan ravikanth vidhya badri francis priti badri salary 4000 4000 2000 4000 4000 1000 3000 2000 1000 .Introducing another column salary in the table by altering the table alter table emp add salary decimal (10.

rank() OVER (ORDER BY salary ) AS RN from emp Output: empno 8 2 4 5 6 3 9 1 7 ename badri badri priti anjan francis ravikanth srikanth vidhya srikanth salar y1000 1000 2000 2000 3000 4000 4000 4000 4000 RN 1 1 3 3 5 6 6 6 6 . select empno.salary. ename.Rank the employees based on salary.

Rank the employees within each salary group.salary.rank() OVER (partition by salary ORDER BY ename desc ) AS RN from emp empno 2 8 4 5 6 1 7 9 3 ename badri badri priti anjan francis vidhya srikanth srikanth ravikanth salary 1000 1000 2000 2000 3000 4000 4000 4000 4000 RN 1 1 1 2 1 1 2 2 4 .Sort by arranging in descending employee number. Query: select empno. ename.

NO FALLBACK. CHECKSUM = DEFAULT. actual_sale INTEGER ) PRIMARY INDEX ( store_no ) . It will be useful in understanding the coming functions. projected_sale INTEGER. prod_code CHAR (20) CHARACTER SET LATIN NOT CASESPECIFIC.Create the following table and insert values as given in the next slide. LOG ( store_no INTEGER. sale_month DATE. CREATE SET TABLE sales.

INSERT INTO sales values (80. INSERT INTO sales values (30.12500).'Banana'.'2007-08-01'.10500).'2007-02-01'. INSERT INTO sales values (30.'Mango'.'2007-04-01'.'2007-06-01'.15000. INSERT INTO sales values (10. INSERT INTO sales values (90.'Apricot'.'Apricot'.12000.'2007-07-01'. INSERT INTO sales values (50. INSERT INTO sales values (40.12500.'Apple'. INSERT INTO sales values (60.12500).11500).14500.11500).'2007-04-01'. INSERT INTO sales values (50.'Banana'.'Guava'.'Orange'.15500). INSERT INTO sales values (20. INSERT INTO sales values (70.'Grapes'.'Mango'.16000.15000.'2007-06-01'.'2007-05-01'.12500).15500).'2007-05-01'.11000.15000.12500).INSERT INTO sales values (10.'Apple'.13000).'2007-03-01'.'2007-02-01'.11500).'2007-03-01'. INSERT INTO sales values (20. INSERT INTO sales values (40.15000.'2007-01-01'.'Strawberry'.10500).'Grapes'. .14000.'2007-01-01'.'Guava'.13500).'2007-02-01'.12000.15500.15500.10000).'Orange'. INSERT INTO sales values (30.11000).12000.10000.

Query: SELECT store_no. Use Rows 1 preceding option. . sale_month.Find the moving average of actual sales within each store. projected_sale. AVG(actual_sale) OVER (PARTITION BY store_no ORDER BY sale_month ROWS 1 PRECEDING) FROM sales. actual_sale.

Inc.13500+13000/2 store_ no 10 10 20 20 30 30 30 40 40 50 50 60 70 80 90 sale_month actual_s ale 1/1/2007 13000 5/1/2007 13500 2/1/2007 11000 4/1/2007 12500 2/1/2007 10000 3/1/2007 10500 3/1/2007 12500 2/1/2007 11500 4/1/2007 12500 1/1/2007 10500 5/1/2007 12500 6/1/2007 11500 7/1/2007 11500 8/1/2007 15500 6/1/2007 15500 projected_ sale 15000 14500 10000 15500 12000 11000 15000 14000 12500 12000 15000 12000 15500 15000 16000 Trianz. Confidential Moving Avg ( actual_sale ) 13000 13250 11000 11750 10000 10250 11500 11500 12000 10500 11500 11500 11500 15500 15500 16 .

actual_sale. o ale ed_sale Avg ( actual_sale ) 10 10 20 20 30 30 30 40 40 50 50 60 70 80 90 5/1/2007 1/1/2007 2/1/2007 4/1/2007 3/1/2007 2/1/2007 3/1/2007 4/1/2007 2/1/2007 1/1/2007 5/1/2007 6/1/2007 7/1/2007 8/1/2007 6/1/2007 13500 13000 11000 12500 10500 10000 12500 12500 11500 10500 12500 11500 11500 15500 15500 14500 15000 10000 15500 11000 12000 15000 12500 14000 12000 15000 12000 15500 15000 16000 13500 13250 11000 11750 10500 10250 11250 12500 12000 10500 11500 11500 11500 15500 15500 . projected_sale. sale_month.SELECT store_no. AVG(actual_sale) OVER (PARTITION BY store_no ORDER BY projected_sale ROWS 1 PRECEDING) store_n sale_month actual_s project Moving FROM sales.

SELECT store_no. . projected_sale. min(actual_sale) OVER (PARTITION BY store_no ORDER BY sale_month ROWS 2 PRECEDING) FROM sales.Find the minimum of actual sales within each store within every three sale months. actual_sale. sale_month.

OUTPUT: store_no sale_month 10 10 20 20 30 30 30 40 40 50 50 60 70 80 90 1/1/2007 5/1/2007 2/1/2007 4/1/2007 2/1/2007 3/1/2007 3/1/2007 2/1/2007 4/1/2007 1/1/2007 5/1/2007 6/1/2007 7/1/2007 8/1/2007 6/1/2007 actual_s projected_s ale ale 13000 15000 13500 14500 11000 10000 12500 15500 10000 IS MIN 10000 12000 10500 11000 12500 15000 11500 14000 12500 12500 10500 12000 12500 15000 11500 12000 11500 15500 15500 15000 15500 16000 Trianz. Confidential Moving Min ( actual_sale ) 13000 13000 11000 11000 10000 10000 10000 11500 11500 10500 10500 11500 11500 15500 15500 19 . Inc.

.Find out the total actual sales in stores for current and previous month. sale_month. projected_sale. (Here previous need not be the immediate previous month) SELECT store_no. sum(actual_sale) OVER (PARTITION BY store_no ORDER BY sale_month ROWS 1 PRECEDING) FROM sales. actual_sale.

store_n sale_mon o th 10 1/1/2007 10 5/1/2007 20 2/1/2007 20 4/1/2007 30 2/1/2007 30 3/1/2007 30 3/1/2007 40 2/1/2007 40 4/1/2007 50 1/1/2007 50 5/1/2007 60 6/1/2007 70 7/1/2007 80 8/1/2007 90 6/1/2007 actua l_sal 13000 e 13500 11000 12500 10000 10500 12500 11500 12500 10500 12500 11500 11500 15500 15500 project Moving ed_sale Sum ( actual_sal 15000 13000 e) 14500 26500 10000 11000 15500 23500 12000 10000 11000 20500 15000 23000 14000 11500 12500 24000 12000 10500 15000 23000 12000 11500 15500 11500 15000 15500 16000 15500 Trianz. Confidential 21 . Inc.

sale_month. min(actual_sale) OVER (PARTITION BY store_no ORDER BY sale_month ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING ) FROM sales.ASSIGNING THE PREVIOUS VALUE FOR THE NEXT ONE. projected_sale. SELECT store_no. actual_sale. store_no sale_month 10 10 20 20 30 30 30 40 40 50 50 60 70 80 90 1/1/2007 5/1/2007 2/1/2007 4/1/2007 2/1/2007 3/1/2007 3/1/2007 2/1/2007 4/1/2007 1/1/2007 5/1/2007 6/1/2007 7/1/2007 8/1/2007 6/1/2007 actual_s ale 13000 13500 11000 12500 10000 10500 12500 11500 12500 10500 12500 11500 11500 15500 15500 projecte d_sale 15000 14500 10000 15500 12000 11000 15000 14000 12500 12000 15000 12000 15500 15000 16000 Moving Avg ( actual_sale ) ? 13000 ? 11000 ? 10000 10500 ? 11500 ? 10500 ? ? ? ? .

projected_sale.SELECT store_no. sale_month. AVG(actual_sale) OVER (PARTITION BY store_no ORDER BY sale_month ROWS BETWEEN 2 PRECEDING AND 2 PRECEDING ) FROM sales. actual_sale. store_no sale_month actual_s projecte Moving 10 10 20 20 30 30 30 40 40 50 50 60 70 80 90 1/1/2007 5/1/2007 2/1/2007 4/1/2007 2/1/2007 3/1/2007 3/1/2007 2/1/2007 4/1/2007 1/1/2007 5/1/2007 6/1/2007 7/1/2007 8/1/2007 6/1/2007 ale 13000 13500 11000 12500 10000 10500 12500 11500 12500 10500 12500 11500 11500 15500 15500 d_sale 15000 14500 10000 15500 12000 11000 15000 14000 12500 12000 15000 12000 15500 15000 16000 ?Avg ( actual_sale ) ? ? ? ? ? 10000 ? ? ? ? ? ? ? ? .

'2007-04-01'.'2007-04-01'.16000) Now the table looks like the below given one.14000.INSERT INTO sales values (30. store_no sale_month prod_code projected_sale actual_sale 40 4/1/2007 Apricot 12500 12500 30 3/1/2007 Orange 11000 10500 40 2/1/2007 Guava 14000 11500 30 3/1/2007 Mango 15000 12500 80 8/1/2007 Grapes 15000 15500 30 2/1/2007 Strawberry 12000 10000 20 2/1/2007 Banana 10000 11000 30 4/1/2007 pear 14000 15000 20 4/1/2007 Mango 15500 12500 30 4/1/2007 lichi 14500 16000 60 6/1/2007 Apple 12000 11500 70 7/1/2007 Banana 15500 11500 90 6/1/2007 Orange 16000 15500 10 1/1/2007 Apple 15000 13000 10 5/1/2007 Apricot 14500 13500 50 5/1/2007 Grapes 15000 12500 50 1/1/2007 Guava 12000 10500 .'lichi'.'pear'.15000) INSERT INTO sales values (30.14500.

sale_month.Show average sales of all products in all stores with a moving average using the two preceding rows but partitioning by store number. actual_sale. projected_sale. SELECT store_no. 10000+10500+1250 store_n sale_month o actual_s project Moving ale ed_sale Avg ( actual_sale ) 30 30 30 30 30 10000 10500 12500 16000 15000 2/1/2007 3/1/2007 3/1/2007 4/1/2007 4/1/2007 12000 11000 15000 14500 14000 10000 10250 11000 13000 14500 . AVG(actual_sale) OVER (PARTITION BY store_no ORDER BY sale_month ROWS BETWEEN 2 PRECEDING AND CURRENT ROW ) where store_no=30 FROM sales.

avg(actual_sale) OVER (PARTITION BY store_no ORDER BY sale_month ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING ) where store_no=30 FROM sales. actual_sale. projected_sale. sale_month.SELECT store_no. Here the first row=10000+10500/2 store_n sale_month actual_s project Moving o ale ed_sale Avg ( actual_sale ) 30 2/1/2007 10000 12000 10250 30 3/1/2007 10500 11000 11500 30 3/1/2007 12500 15000 14250 30 4/1/2007 16000 14500 15500 30 4/1/2007 15000 14000 15000 .

Calculate the total actual sales of fruits by category per each store: SELECT store_no. prod_code. actual_sale. 10 Apricot 10 Apple 20 Mango 20 Banana 30 Strawberry 30 lichi 30 pear 30 Mango average is taken for each partition and 30 written across Orange 40 Guava 40 Apricot 50 Grapes 50 Guava 60 Apple 70 Banana 80 Grapes 90 Orange e 13500 13000 12500 11000 10000 16000 15000 12500 10500 11500 12500 12500 10500 11500 11500 15500 15500 al 26500 26500 23500 23500 64000 64000 64000 64000 64000 24000 24000 23000 23000 11500 11500 15500 15500 . SUM (actual_sale) OVER (PARTITION BY store_no ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) as FruitTotal store_no prod_code actual_sal FruitTot FROM sales.

sale_month.Calculate the cumulative actual sales per store ordered by sale month: SELECT store_no. 10 1/1/2007 o 10 20 20 30 30 30 30 30 40 40 50 50 60 70 80 90 5/1/2007 2/1/2007 4/1/2007 2/1/2007 3/1/2007 3/1/2007 4/1/2007 4/1/2007 2/1/2007 4/1/2007 1/1/2007 5/1/2007 6/1/2007 7/1/2007 8/1/2007 6/1/2007 actual_s StoreTotal 13000 13000 ale 13500 11000 12500 10000 10500 12500 16000 15000 11500 12500 10500 12500 11500 11500 15500 15500 26500 11000 23500 10000 20500 33000 49000 64000 11500 24000 10500 23000 11500 11500 15500 15500 . SUM (actual_sale) OVER (PARTITION BY store_no ORDER BY sale_month ROWS UNBOUNDED PRECEDING) as StoreTotal store_n sale_month FROM sales. actual_sale.

20) store_no 10 10 20 20 30 30 30 30 30 sale_month 1/1/2007 5/1/2007 2/1/2007 4/1/2007 2/1/2007 3/1/2007 3/1/2007 4/1/2007 4/1/2007 Within each month. SUM (actual_sale) OVER (PARTITION BY store_no.30. sale_month. SELECT store_no.Calculate the moving actual sales per month per store.moving sales is ca actual_sa le 13000 13500 11000 12500 10000 10500 12500 15000 16000 SaleTren d 13000 13500 11000 12500 10000 10500 23000 15000 31000 . sale_month ORDER BY actual_sale ROWS 1 PRECEDING) as SaleTrend FROM sales where store_no in (10. actual_sale.

Find the rank over the stores by their actual sales. RANK () OVER (PARTITION BY store_no ORDER BY actual_sale) as SaleRank FROM sales where store_no in (10. actual_sale.30) store_n sale_month o 10 1/1/2007 actual_sa SaleRan le k 13000 1 10 5/1/2007 13500 2 30 2/1/2007 10000 1 30 3/1/2007 10500 2 30 3/1/2007 12500 3 30 4/1/2007 15000 4 30 4/1/2007 16000 5 . SELECT store_no. sale_month.

00 100000.00 25000.00 25000.00 30000.00 25000. prodid.00 65000.00 35000.00 20000.00 35000.00 20000.00 25000.00 40000.00 20000.00 150000.Show sales of all products in all stores partitioned by store with a remaining minimum using the current and following rows.00 35000.00 60000.00 35000.-----1001 D 1001 C 1001 A 1001 F 1002 D 1002 C 1002 A 1003 C 1003 A 1003 D 1003 B sales Remaining Min(sales) -----------------------------35000.00 35000.00 50000.   SELECT storeid. MIN(sales) OVER (PARTITION BY storeid ORDER BY sales DESC ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM salestbl .00 20000.00 20000.00 .   storeid prodid ----------. sales.

The ORDER BY clause tells what is being ranked (i.00 3 Rank_Sales -----------  „ The ORDER BY clause in this construct is associated with the RANK function. „  SELECT storeid.Advanced Ranking Functions RANK Review   §  The RANK function is a Teradata specific function which assigns a ranking order to rows in a qualified answer set Example Rank the top three products across all stores by revenue. sales) and the DESC options starts the ranking with the highest sales value. Use ANSI RANK syntax to accomplish this. storeid ----------1001 F 1001 A 1003 B prodid sales -----.e.00 1 100000. . sales. RANK() OVER (ORDER BY sales DESC) AS Rank_Sales FROM salestbl QUALIFY rank_sales <= 3.----------150000.00 2 65000. A rank by definition implies an ordering on a particular column or expression. prodid.

 SELECT storeid.---------  1001 F 150000.00 1  1002 C 35000. sales.-------. prodid. RANK() OVER (PARTITION BY storeid ORDER BY sales DESC)AS Rank_Sales FROM salestbl QUALIFY Rank_Sales <= 3.-------.00 1  1003 D 50000.00 2  1002 D 25000.00 3  1003 B 65000.00 1  1001 A 100000.00 3  1002 A 40000.Qualified Ranking § Example „ Show a ranking of the top three products in each store and the revenue generated by them in each store.00 2  1001 C 60000.   storeid  ------.00 3  prodid sales Rank_Sales .00 2  1003 A 30000.

RANK With Aggregation  Low Order Ranking  § A low-order ranking is accomplished by changing the ORDER BY clause of the RANK function from descending to ascending.  .

sale_month. actual_sale.SELECT store_no. RANK () OVER (PARTITION BY store_no ORDER BY actual_sale) as SaleRank FROM sales qualify Salerank = 5 store_no sale_month actual_sa SaleRank le 30 16000 4/1/2007 5 .

Inc. sale_month.store_no) FROM sales § Trianz. csum(actual_sale. providing the current values of the SUM on each row §Syntax: store_no sale_month actual_sale CSum ( actu al_sale . Confidential 1/1/2007 5/1/2007 2/1/2007 4/1/2007 2/1/2007 4/1/2007 4/1/2007 3/1/2007 3/1/2007 2/1/2007 4/1/2007 1/1/2007 5/1/2007 6/1/2007 7/1/2007 8/1/2007 6/1/2007 13000 26500 37500 50000 60000 76000 91000 101500 114000 125500 138000 148500 161000 172500 184000 199500 215000 36 . actual_sale.CSUM-Cumulative Sum §Accumulates a sum over an ordered set of rows. sort_expression_list) § SELECT store_no. s tore_no ) 10 10 20 20 30 30 30 30 30 40 40 50 50 60 70 80 90 13000 13500 11000 12500 10000 16000 15000 10500 12500 11500 12500 10500 12500 11500 11500 15500 15500 §CSum(value_expression.

store_no) FROM sales Trianz.2. §Syntax: §MSum(value_expression. Inc. sale_month. sort_expression_list) § store_n sale_month actual_s MSum ( actual_sale . o ale 2 .MSUM-Moving Sum §Description §Computes the moving sum of a column using the current row and the preceding n-1 row. msum(actual_sale. Confidential 1/1/2007 5/1/2007 4/1/2007 2/1/2007 4/1/2007 4/1/2007 2/1/2007 3/1/2007 3/1/2007 4/1/2007 2/1/2007 1/1/2007 5/1/2007 6/1/2007 7/1/2007 8/1/2007 6/1/2007 13000 13500 12500 11000 15000 16000 10000 10500 12500 12500 11500 10500 12500 11500 11500 15500 15500 13000 26500 26000 23500 26000 31000 26000 20500 23000 25000 24000 22000 23000 24000 23000 27000 31000 37 . store_no ) SELECT store_no. 10 10 20 20 30 30 30 30 30 40 40 50 50 60 70 80 90 actual_sale. width.

§The moving difference is a common business metric used to compare activity for some variable in a current time period to the activity for the same variable in another time period at a fixed distance in the past.1. sale_month. mdiff(actual_sale. store_no ) 500 -1000 -1500 4000 1000 -6000 500 2000 0 -1000 -1000 2000 -1000 0 4000 0 38 .store_no) FROM sales store_n o10 10 20 20 30 30 30 30 30 40 40 50 50 60 70 80 90 Trianz. width.?1 . Syntax: MDiff(value_expression. Inc. actual_sale. sort_expression_list) § SELECT store_no. Confidential sale_month 1/1/2007 5/1/2007 4/1/2007 2/1/2007 4/1/2007 4/1/2007 2/1/2007 3/1/2007 3/1/2007 4/1/2007 2/1/2007 1/1/2007 5/1/2007 6/1/2007 7/1/2007 8/1/2007 6/1/2007 actual_s ale 13000 13500 12500 11000 15000 16000 10000 10500 12500 12500 11500 10500 12500 11500 11500 15500 15500 MDiff ( actual_sale .MSUM-Moving Sum §Returns the moving difference between the current row-column value and the preceding nth value.

  THANK YOU!!!! .

Inc. Confidential 40 .Trianz.

Sign up to vote on this title
UsefulNot useful