0% found this document useful (0 votes)
438 views

OLAP Functions Part 1

This document discusses OLAP analytics and window functions. It provides examples of typical business questions that can be answered using OLAP instead of traditional SQL. OLAP functions allow computations across groups of rows rather than returning only aggregated values. The document outlines the types of window functions, including group, cumulative, moving, and remaining windows. It also lists the OLAP functions available in Teradata and provides an example to illustrate window functions.

Uploaded by

Bobby Big Balls
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
438 views

OLAP Functions Part 1

This document discusses OLAP analytics and window functions. It provides examples of typical business questions that can be answered using OLAP instead of traditional SQL. OLAP functions allow computations across groups of rows rather than returning only aggregated values. The document outlines the types of window functions, including group, cumulative, moving, and remaining windows. It also lists the OLAP functions available in Teradata and provides an example to illustrate window functions.

Uploaded by

Bobby Big Balls
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

OLAP Analytics in Action

Patrice Bérubé
Technical Solution Architect
Teradata Canada
OLAP Analytics - Agenda
• Business view

• Options & solutions

• Academic examples

• Real Life examples

• Summary

2 pg.2
Typical Business Questions
1. How much revenue did each market have in February, what
percent of total revenue?

2. What are the top 5 and bottom 5 Called Countries by market in


February?

3. What are the top 5 Adjustment Reasons in February by system


and by Consumer vs Business?

4. What is the average revenue and MOU for each decile of the
consumer base in February?

5. Which customers had the biggest increases in their MOU from


January to February?

6. What price plan is the final one for a given day when there are
several changes over the course of the day?

3 pg.3
Business needs beyond SQL (1 of 2)

• Data Warehouse power users know:


• Their business
• Their data
• SQL

• Data Warehouse power users want:


• Reduce SQL coding complexity
• Less “steps”
• Less DERIVED tables
• Do more in Teradata SQL, less in Excel

4 pg.4
Business needs beyond SQL (2 of 2)

• Data Warehouse power users Often turn to Excel:


• Computed figures available along detail rows
• Aggregated figures available along detail rows
• SUM, AVG, COUNT, MIN, MAX
• Target different window of data rows
• All rows
• A specific number of rows
• Row immediately before/after current row
• Row position (7,13) before/after current row
• All rows before/after current row

After all power users count on the Data Warehouse


power and usability to answer…..

5 pg.5
Any options… solutions…

• So!

• Any suggestions?

• Can Teradata help?

6 pg.6
OLAP Analytics - Agenda
• Business view

• Options & solutions

• Academic examples

• Real Life examples

• Summary

7 pg.7
Business question #1 (1 of 3)

How much revenue did each market have in February,


what percent of total revenue?

OLD WAY OLAP Way

SELECT SELECT
bs.bill_mkt_id bs.bill_mkt_id
,DT.CumRev ,SUM(bs.tot_amt) AS MktRev
,SUM(bs.tot_amt) AS MktRev
,MktRev / (DT.CumRev/ 1.0000) AS MktRevPerc ,SUM(MktRev)
FROM bl_stmnt bs
OVER (ROWS BETWEEN UNBOUNDED PRECEDING
CROSS JOIN
AND UNBOUNDED FOLLOWING ) AS CumRev
(
SELECT
,MktRev / (CumRev / 1.0000) AS MktRevPerc
SUM(bs.tot_amt) AS CumRev
FROM bl_stmnt bs
FROM bl_stmnt bs
WHERE bs.bl_cyc_dt BETWEEN '2006-02-01' AND '2006-02-28'
) DT
WHERE bs.bl_cyc_dt BETWEEN '2006-02-01' AND '2006-02-28'
WHERE bs.bl_cyc_dt BETWEEN '2006-02-01' AND '2006-02-28'
GROUP BY 1
GROUP BY 1,2
ORDER BY MktRev DESC
ORDER BY MktRev DESC

•Involves a Derived Table and 2nd pass of the table


8 pg.8
OLAP Analytics –
What are we talking about?
• Give it a name
• OLAP Functions (On Line Analytical Processing)
• Window Aggregate Functions
• Ordered Analytical Functions
• What are Ordered Analytical Functions?
• Like traditional aggregate functions, window aggregate functions operate
on groups of rows and permit qualification and filtering of the group
result. Unlike aggregations, OLAP functions also return individual detail
rows, not just aggregations.
• How they work
• The window feature is ANSI SQL-99 compliant and provides a way to
dynamically define a subset of data, or window, in an ordered relational
database table. A window is specified by the OVER() phrase, which can
include the following clauses inside the parentheses:
• PARTITION BY
• ORDER BY
• ROWS
9 pg.9
Traditional SQL requests vs
OLAP Functions
Calculation

Aggregation

Ordered Analytical Functions

10
10 pg.
OLAP functions available in Teradata

Teradata specific Functions ANSI compliant Functions


(V2R4.1) (V2R5)
•CSUM •SUM (col) over (...)
•MSUM •AVG (col) over (...)
•MAVG •RANK () over (...)
•RANK •COUNT (col) over (...)
•MDIFF (composable of SUM) •MAX (col) over (...)
•MLINREG (composable of •MIN (col) over (...)
SUM and COUNT)
•PERCENT_RANK (composable
•QUANTILE (composable of of RANK and COUNT)
RANK and COUNT)
•ROW_NUMBER
(composable of COUNT)
V2R4 syntax including user friendly functions are still available
for backward compatibility. Preference to ANSI syntax.
11
11 pg.
New R12 Features
VAR_SAMP Returns the sample variance of a set of numbers.
VAR_POP Returns the population variance of a set of numbers.
COVAR_POP Returns the population covariance of a set of number pairs.
COVAR_SAMP Returns the sample covariance of a set of number pairs.
CORR Returns the coefficient of correlation of a set of number pairs.
STDDEV_POP Computes the population STDDEV and returns the square root of the
population variance.
STDDEV_SAMP Computes the cumulative sample STDDEV and returns the square root of the
sample variance.
REGR_AVGX Evaluates the average of the independent variable of the regression line.
REGR_AVGY Evaluates the average of the dependent variable of the regression line.
REGR_COUNT Returns an integer that is the number of non-null number pairs used to fit the
regression line.
REGR_INTERCEPT Returns the Y-intercept of the regression line.
REGR_R2 Returns the coefficient of determination (R-Squared) for the regression.
REGR_SLOPE Returns the slope of the line.
REGR_SXX Auxiliary functions that are used to compute various diagnostic statistics.
REGR_SXY Auxiliary functions that are used to compute various diagnostic statistics.
REGR_SYY Auxiliary functions that are used to compute various diagnostic statistics.

12
12 pg.
month day qty cum qty

OLAP Analytics - Agenda


• Business view

• Options & solutions

• Academic examples

• Real Life examples

• Summary

13
13 pg.
month day qty cum qty

OLAP Functions Types


All Windows Aggregate functions fall into one of four types.

Group Window -
Aggregates based on a grouping of rows.
Cumulative Window -
Aggregates based on a cumulation of rows.
Moving Window -
Aggregates based on a moving window of rows.
Remaining Window -
Aggregates based on the rows remaining outside of a defined window.

Each choice is determined by the ROWS clause defined in the query.


Each choice is used with one of the following aggregate functions:

• AVG • SUM
• COUNT • RANK
• MAX • PERCENT_RANK
• MIN • ROW_NUMBER
14
14 pg.
month day qty cum qty

OLAP Functions Permutations


Four Categories Aggregates
SUM ( ) OVER
Group Window COUNT ( ) OVER
Cumulative Window AVG ( ) OVER
Moving Window x MIN ( ) OVER
Remaining Window MAX ( ) OVER
Group Window Function
• Use of keywords: ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
• Absence of keywords: ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING

Moving Window Function


• Use of keywords: ROWS BETWEEN # PRECEDING AND # FOLLOWING
• Absence of keywords: UNBOUNDED

Cumulative Window Function


• Use of keywords: ROWS BETWEEN UNBOUNDED PRECEDING
• Absence of keywords: UNBOUNDED FOLLOWING

Remaining Window Function


• Use of keywords: ROWS BETWEEN UNBOUNDED FOLLOWING
• Absence of keywords: UNBOUNDED PRECEDING

15
15 pg.
month day qty cum qty

OLAP - Diagram
SUM (qty) over (...) as cum_qty
month day qty cum_qty
200401 1 2 2
200401 10 3 5

day
200401 partition by month 15 1 6

unbounded preceding
200402 5 3 3

by

and current row


200402 20 5 8

rows between
200403 6 order 7 7
200403 7 2 9
200403 8 1 10
200404 4 5 5
200404 16 4 9
200404 27 5 14
16
16 pg.
month day qty cum qty

Coding
• Options and Syntax
• Example:
SUM(qty)
OVER(PARTITION BY month ORDER BY day
ROWS BETWEEN UNBOUNDED PRECEDING
AND CURRENT ROW
) as Cumulative_Quantity
• Window defined by
• PARTITION BY clause defining the “grouping” of data
• ORDER BY clause defining the sequence of data
• ROWS BETWEEN defines the window used for calculation
e.g.: following/preceding/current row
unbounded or relative row numbers

17
17 pg.
month day qty cum qty

Group SUM Window Function

SELECT storeid, prodid, sales,


SUM(sales) OVER (ORDER BY sales DESC)
FROM salestbl;
storeid prodid sales Group Sum(sales)
----------- ------ -------------- --------------------------
1001 F 150000.00 610000.00
1001 A 100000.00 610000.00
1003 B 65000.00 610000.00
1001 C 60000.00 610000.00
1003 D 50000.00 610000.00
1002 A 40000.00 610000.00
1001 D 35000.00 610000.00
1002 C 35000.00 610000.00
1003 A 30000.00 610000.00
1002 D 25000.00 610000.00
1003 C 20000.00 610000.00

The window is defined as all rows - no PARTITION is specified.


The final column represents the total of all rows.
The default title of the last column indicates this is a Group function.

18
18 pg.
month day qty cum qty

Group SUM Window Function

SELECT storeid, prodid, sales,


SUM(sales) OVER (PARTITION BY prodid ORDER BY sales DESC)
FROM salestbl ;
storeid prodid sales Group Sum(sales)
----------- ---------- -------------- --------------------------
1001 A 100000.00 170000.00
1002 A 40000.00 170000.00
1003 A 30000.00 170000.00
1003 B 65000.00 65000.00
1001 C 60000.00 115000.00
1002 C 35000.00 115000.00
1003 C 20000.00 115000.00
1003 D 50000.00 110000.00
1001 D 35000.00 110000.00
1002 D 25000.00 110000.00
1001 F 150000.00 150000.00

Note that the Group Sum reflects the total for each product.

19
19 pg.
month day qty cum qty

Cumulative SUM Window Function

SELECT storeid, prodid, sales,


SUM(sales) OVER (ORDER BY sales DESC ROWS UNBOUNDED PRECEDING)
FROM salestbl ;

storeid prodid sales Cumulative Sum(sales)


------------ ---------- ---------------- --------------------------------
1001 F 150000.00 150000.00
1001 A 100000.00 250000.00
1003 B 65000.00 315000.00
1001 C 60000.00 375000.00
1003 D 50000.00 425000.00
1002 A 40000.00 465000.00
1001 D 35000.00 500000.00
1002 C 35000.00 535000.00
1003 A 30000.00 565000.00
1002 D 25000.00 590000.00
1003 C 20000.00 610000.00

The Cululative Sum reflects the sequential aggregation of all rows.


The default title of last column indicates this is a Cumulative function.

20
20 pg.
month day qty cum qty

Moving SUM Window Function

SELECT storeid, prodid, sales,


SUM(sales) OVER (ORDER BY sales DESC ROWS 2 PRECEDING)
FROM salestbl;
storeid prodid sales Moving Sum(sales)
----------- ---------- -------------- ---------------------------
1001 F 150000.00 150000.00
1001 A 100000.00 250000.00
1003 B 65000.00 315000.00
1001 C 60000.00 225000.00
1003 D 50000.00 175000.00
1002 A 40000.00 150000.00
1001 D 35000.00 125000.00
1002 C 35000.00 110000.00
1003 A 30000.00 100000.00
1002 D 25000.00 90000.00
1003 C 20000.00 75000.00

• Each row computes a moving sum based on itself and 2 preceding rows.
• The 1st and 2nd rows compute their sums based on one and two rows respectively.
• The default title of the last column indicates this is a Moving function.

21
21 pg.
month day qty cum qty

Remaining SUM Window Function


SELECT salesdate,itemid,sales,
SUM(sales) OVER (ORDER BY salesdate ASC ROWS BETWEEN CURRENT ROW AND unbounded following) AS "AMsum"
,CAST((AMsum - sales)AS DECIMAL(6,2)) AS "excl. current"
,SUM(sales) OVER (ORDER BY salesdate ASC ROWS BETWEEN 1 following AND unbounded following) AS "AM1sum"
FROM daily_sales ORDER BY salesdate ASC
WHERE EXTRACT(YEAR FROM salesdate) = '1997‘ AND EXTRACT(MONTH FROM salesdate) BETWEEN 1 AND 2;

salesdate itemid sales AMsum excl. current AM1sum

========== ============ ========== ========== ============= ==========


1997-01-01 10 350.00 4100.00 3750.00 3750.00
1997-01-02 10 100.00 3750.00 3650.00 3650.00
1997-01-03 10 250.00 3650.00 3400.00 3400.00
1997-01-05 10 350.00 3400.00 3050.00 3050.00
1997-01-10 10 450.00 3050.00 2600.00 2600.00
1997-01-21 10 250.00 2600.00 2350.00 2350.00
1997-01-25 10 300.00 2350.00 2050.00 2050.00
1997-01-31 10 100.00 2050.00 1950.00 1950.00
1997-02-01 10 550.00 1950.00 1400.00 1400.00
1997-02-03 10 350.00 1400.00 1050.00 1050.00
1997-02-06 10 150.00 1050.00 900.00 900.00
1997-02-17 10 250.00 900.00 650.00 650.00
1997-02-20 10 500.00 650.00 150.00 150.00
22
22 pg. 1997-02-27 10 150.00 150.00 .00 ?
month day qty cum qty

Moving Difference – MAX

SELECT Session_ID, Txn_Time,


MAX(Txn_Time)
OVER (partition BY Session_ID ORDER BY Txn_Time
ROWS BETWEEN 1 following AND 1 following) –
Txn_Time AS MovDiff,
URL
FROM TXN_Table;

Session_ID Txn_Time MovDiff URL


21 10:00 3 /url1.htm
21 10:03 4 /url2.htm
21 10:07 ? /url3.htm
22 10:05 25 /urlx.htm
22 10:30 15 /urly.htm
22 10:45 ? /urlz.htm

23
23 pg.
month day qty cum qty

Rank vs Row_Number (1 of 2)

SELECT itemid, salesdate, sales,


RANK() OVER (ORDER BY sales DESC)
FROM daily_sales_2004
WHERE salesdate BETWEEN DATE '2004-01-01' AND DATE '2004-03-01’
itemid salesdate sales Rank(sales)
----------- ------------- ----------- -----------------
10 04/01/10 550.00 1
10 04/02/17 550.00 1
10 04/02/20 450.00 3
10 04/02/06 350.00 4
10 04/02/27 350.00 4
10 04/01/05 350.00 4
10 04/01/03 250.00 7
10 04/02/03 250.00 7
10 04/01/25 200.00 9
10 04/01/02 200.00 9
10 04/01/21 150.00 11
10 04/02/01 150.00 11
10 04/01/01 150.00 11
10 04/01/31 100.00 14

Ranking positions which results in ties do not reuse the tied position
number. For example, the seventh row in this list still maintains a rank of
seven, even though the previous row has a rank of four.
24
24 pg.
month day qty cum qty

Rank vs Row_Number (2 of 2)

SELECT itemid, salesdate, sales,


ROW_NUMBER() OVER (ORDER BY sales DESC)
FROM daily_sales_2004
WHERE salesdate BETWEEN DATE '2004-01-01' AND DATE '2004-03-01’
itemid salesdate sales Row_Number()
----------- ------------- ----------- ---------------------
10 04/01/10 550.00 1
10 04/02/17 550.00 2
10 04/02/20 450.00 3
10 04/02/06 350.00 4
10 04/02/27 350.00 5
10 04/01/05 350.00 6
10 04/01/03 250.00 7
10 04/02/03 250.00 8
10 04/01/25 200.00 9
10 04/01/02 200.00 10
10 04/01/21 150.00 11
10 04/02/01 150.00 12
10 04/01/01 150.00 13
10 04/01/31 100.00 14

Ties are always assigned an incremented sequence number with


ROW_NUMBER.
Ties are always assigned the same number with RANK function.
25
25 pg.
month day qty cum qty

Percent_Rank
SELECT storeid, prodid, sales,
RANK() OVER (ORDER BY sales DESC) AS Rank_Sales,
PERCENT_RANK() OVER (ORDER BY sales DESC) AS
Pct_Rank_Sales
FROM salestbl ;
storeid prodid sales Rank_Sales Pct_Rank_Sales
------------ --------- ------------- ------------------ -----------------------
1001 F 150000.00 1 0.000000
1001 A 100000.00 2 0.100000
1003 B 65000.00 3 0.200000
1001 C 60000.00 4 0.300000
1003 D 50000.00 5 0.400000
1002 A 40000.00 6 0.500000
1001 D 35000.00 7 0.600000
1002 C 35000.00 7 0.600000
1003 A 30000.00 9 0.800000
1002 D 25000.00 10 0.900000
1003 C 20000.00 11 1.000000

PERCENT_RANK is always a value between 0.0 and 1.0 inclusive.


It’s value represents the portion of rows in the answer set which precede any given row on the list.
Formula: PERCENT_RANK = (Row Ranking - 1) / (# of rows - 1)

26
26 pg.
OLAP Analytics - Agenda
• Business view

• Options & solutions

• Academic examples

• Real Life examples

• Summary

27
27 pg.
Business question #1
How much revenue did each market have in February,
what percent of total revenue?

OLAP Way
Perc of Cum
SELECT Market MktRev GrpRev Total Perc
bs.bill_mkt_id North Pole $ 42,806,310.55 $ 231,945,644.49 18.5% 18.5%
,SUM(bs.tot_amt) AS MktRev South Pole $ 14,017,714.23 $ 231,945,644.49 6.0% 24.5%
Fairyland $ 12,427,672.94 $ 231,945,644.49 5.4% 29.9%
,SUM(MktRev) OVER (ROWS BETWEEN
Tir-na-nog $ 11,116,845.45 $ 231,945,644.49 4.8% 34.7%
UNBOUNDED PRECEDING Shan-gri-la $ 10,770,807.94 $ 231,945,644.49 4.6% 39.3%
AND UNBOUNDED FOLLOWING Land Far-Far Away $ 10,726,915.78 $ 231,945,644.49 4.6% 43.9%
Isle of Misfit Toys $ 7,410,941.03 $ 231,945,644.49 3.2% 47.1%
) AS CumRev Soder Island $ 7,224,536.83 $ 231,945,644.49 3.1% 50.2%
,MktRev / (CumRev / 1.0000) AS MktRevPerc Birdwell Island $ 7,210,494.65 $ 231,945,644.49 3.1% 53.3%
Hazzard County $ 6,814,012.59 $ 231,945,644.49 2.9% 56.3%
FROM bl_stmnt bs

WHERE bs.bl_cyc_dt BETWEEN '2006-02-01' AND '2006-02-28'

GROUP BY 1
ORDER BY MktRev DESC

28
28 pg.
Business question #2
What are the top 5 and bottom 5 Called Countries by market in February?
SELECT
calld_cntry_nam
,SUM(toll_dur_min) AS TollMin
,RANK() OVER (ORDER BY TollMin ) AS Cntry_Rnk_Asc
,RANK() OVER (ORDER BY TollMin DESC ) AS Cntry_Rnk_Desc
FROM bl_dtl_usge
WHERE bl_cyc_dt BETWEEN '2006-02-01' AND '2006-02-28'
AND calld_cntry_nam <> ''
GROUP BY 1
ORDER BY 3 DESC
QUALIFY Cntry_Rnk_Asc <= 5 OR Cntry_Rnk_Desc <= 5

calld_cntry_nam TollMin Cntry_Rnk_Asc Cntry_Rnk_Desc


CANADA 7,135,936.0 219 1
MEXICO 5,630,907.0 218 2
DOMINICAN REPUBLIC 3,660,275.0 217 3
UNITED KINGDOM 2,253,797.0 216 4
JAMAICA 1,159,594.0 215 5
LESOTHO 2.0 2 214
BHUTAN 2.0 2 214
SAINT HELENA 2.0 2 214
FAROE ISLANDS 2.0 2 214
ASCENSION 2.0 2 214
TUVALU 1.0 1 219

29
29 pg.
Business question #4 (2 of 2)

What is the average revenue and MOU for each decile of the consumer
base in February?
SELECT
DT.Acct_Decile
,DT.BillMonth
,COUNT(DT.id) AS Occur_Cnt
,SUM(DT.tot_amt) AS Dec_Chg_Amt
,AVG(DT.tot_amt) AS Avg_Chg_Amt
,MIN(DT.tot_amt) AS Min_Chg_Amt
,MAX(DT.tot_amt) AS Max_Chg_Amt
,SUM (Dec_Chg_Amt) OVER () AS Grp_Amt
,Dec_Chg_Amt / (Grp_Amt / 1.0000) AS Amt_Perc
FROM
(SELECT
bs.bill_id
,bs.id
,bs.bl_cyc_id
,bs.bl_cyc_dt
,bs.tot_amt
,ROW_NUMBER () OVER (ORDER BY bs.tot_amt) AS Acct_Rnk
,(( (Acct_Rnk - 1) * 10 ) / COUNT(*) OVER() ) + 1 AS Acct_Decile
FROM bl_stmnt bs
WHERE bs.bl_ind = 'Y'
AND bs.bl_cyc_dt BETWEEN '2006-02-01' AND '2006-02-28'
AND bs.bill_id = 9999
) DT
GROUP BY 1,2
ORDER BY 1,2;

30
30 pg.
Business Question #5 (1 of 2)

Which customers had the biggest increases in their revenue from


January to February?

id bl_cyc_dt Cur_Amt Prior_Amt Perc_Change


16300006 1/16/2006 $ 14.41
16300006 2/16/2006 $ 14.64 $ 14.41 1.6%
16300012 1/20/2006 $ 139.57
16300012 2/20/2006 $ 134.57 $ 139.57 -3.6%
16300018 1/25/2006 $ 165.84
16300018 2/25/2006 $ 184.06 $ 165.84 11.0%
16300028 1/2/2006 $ 109.16
16300028 2/2/2006 $ 126.31 $ 109.16 15.7%
16300036 1/2/2006 $ -
16300036 2/2/2006 $ - $ -
16300042 1/2/2006 $ -
16300042 2/2/2006 $ - $ -
16300046 1/2/2006 $ 13.09
16300046 2/2/2006 $ 20.27 $ 13.09 54.9%
16300078 1/2/2006 $ 118.35
16300078 2/2/2006 $ 117.24 $ 118.35 -0.9%
16300082 1/2/2006 $ 502.04
16300082 2/2/2006 $ 551.18 $ 502.04 9.8%
16300088 1/2/2006 $ 32.60
16300088 2/2/2006 $ 32.60 $ 32.60 0.0%

31
31 pg.
Business question #5 (2 of 2)

Which customers had the biggest increases in their revenue from


January to February?
SELECT bs.id
,bs.bl_cyc_dt
,bs.tot_amt AS Cur_Amt
,SUM (Cur_Amt) OVER (PARTITION BY bs.bill_mkt_id,bs.acct_id
ORDER BY bs.bl_cyc_dt ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING
) AS Prior_Amt
,CASE
WHEN ZEROIFNULL ( Prior_Amt ) > 0 THEN (Cur_Amt - Prior_Amt) / (Prior_Amt / 1.0000)
ELSE NULL
END AS Perc_Change
FROM bl_stmnt bs
WHERE bs.bl_ind = 'Y'
AND bs.bl_cyc_dt BETWEEN '2006-01-01' AND '2006-02-28'
AND bs.bill_id = 5554
ORDER BY 1,2,3,4

32
32 pg.
Business question #6 (1 of 2)

What price plan is the final one for a given day when there are several
changes over the course of the day?
srv_accs_id client_prc_pln_eff_dt client_prc_pln_end_dt client_prc_pln_seq_nbr prd_id Row_Nbr
808090 02/12/2003 02/12/2003 1 538042 6
808090 02/12/2003 02/12/2003 2 546478 5
808090 02/12/2003 02/12/2003 3 509834 4
808090 02/12/2003 02/12/2003 4 547262 3
808090 02/12/2003 02/12/2003 5 538042 2
808090 02/12/2003 02/12/2003 6 538038 1
808096 26/02/2003 26/02/2003 1 269338 2
808096 26/02/2003 26/02/2003 2 270708 1
809186 01/05/2003 01/05/2003 1 478658 2
809186 01/05/2003 01/05/2003 2 269332 1
809186 05/12/2003 05/12/2003 1 478658 3
809186 05/12/2003 05/12/2003 2 486812 2
809186 05/12/2003 05/12/2003 3 546478 1
813436 11/11/2003 11/11/2003 1 547310 2
813436 11/11/2003 11/11/2003 2 547308 1
813502 17/02/2005 17/02/2005 1853936 836964 2
813502 17/02/2005 17/02/2005 2093328 836956 1
33
33 pg.
Business question #6 (2 of 2)

What price plan is the final one for a given day when there are several
changes over the course of the day?
SELECT
vpph.srv_accs_id
,vpph.client_prc_pln_eff_dt
,vpph.client_prc_pln_end_dt
,vpph.client_prc_pln_seq_nbr
,vpph.prd_id
,ROW_NUMBER () OVER (PARTITION BY vpph.srv_accs_id
,vpph.client_prc_pln_eff_dt
,vpph.client_prc_pln_end_dt
ORDER BY vpph.client_prc_pln_seq_nbr DESC
) AS Row_Nbr

FROM Vclient_PRC_PLN_HIST vpph


INNER JOIN client s
ON vpph.srv_accs_id = s.srv_accs_id
AND s.bill_srv_area_id = 1993

ORDER BY 1,2,3,4,5

QUALIFY COUNT(vpph.srv_accs_id)
OVER (PARTITION BY vpph.srv_accs_id, vpph.client_prc_pln_eff_dt, vpph.client_prc_pln_end_dt) > 1

34
34 pg.
Real Life –
Assign phone_no Attributes 1 of 3

• Assign Country Code and Area Code to the phone number using the
‘best match’
• RANK (windows function), QUALIFY clause
phone_ID phone_no
100001 3723567890
100002 37250867890
100003 37255334234
100004 3725512543

Normalized CountryC AreaC NetworkProvider


372 372 ? Estonia
37250 372 50 Estonian Mobile
37255 372 55 Estonia Ritabell
372551 372 551 Estonia Ritabell Plus
35
35 pg.
Real Life –
Assign phone_no Attributes 2 of 3

SELECT pn.phone_ID, pn.phone_no, ac.CountryC, ac.Area


FROM phone_nos pn
INNER JOIN AreaCodes ac
ON SUBSTRING (pn.phone_no FROM 1 FOR
CHARACTERS(ac.Normalized)) = ac.Normalized;

phone_ID phone_no CountryC AreaC Chars


100001 3723567890 372 ? 3
100002 37250867890 372 ? 3
100002 37250867890 372 50 5
100003 37255334234 372 ? 3
100003 37255334234 372 55 5
100004 3725512543 372 ? 3
100004 3725512543 372 55 5
100004 3725512543 372 551 6
36
36 pg.
Real Life –
Assign phone_no Attributes 3 of 3

SELECT pn.phone_ID, pn.phone_no, ac.CountryC, ac.AreaC


FROM phone_nos pn
INNER JOIN AreaCodes ac
ON SUBSTRING (pn.phone_no FROM 1 FOR
CHARACTERS(ac.Normalized)) = ac.Normalized
QUALIFY RANK () OVER (partition BY pn.phone_ID ORDER BY
CHARACTERS(ac.Normalized) DESC) = 1;

phone_ID phone_no CountryC AreaC Chars


100001 3723567890 372 ? 3
100002 37250867890 372 50 5
100003 37255334234 372 55 5
100004 3725512543 372 551 6

37
37 pg.
OLAP Analytics - Agenda
• Business view

• Options & solutions

• Academic examples

• Real Life examples

• Summary

38
38 pg.
Summary
• Functionality of Ordered Analytical Functions
• Supports a large subset of the SQL-99 Window Functions
• All combinations of (cumulative, moving, running) x
(sum, count, min, max, avg)
• Any physical row based window definition: preceding,
following, current row, unbound
• ANSI Row_Number, ANSI Rank
• Benefit
• Exceed Application Limits, get better Performance
• Process costly OLAP Functions within Teradata
• ANSI standard makes SQL support easier for application
developers
• Tidy SQL
• less subselects

39
• replace self joins or multipass SQL
39 pg.
Remember, coding is one thing,
what you want to achieve is another!

40
40 pg.
Thanks and Questions
• Questions???
• I can be reached at [email protected]
• Thanks
• Patrick R. McHugh

And Obviously…All of you !

41
41 pg.

You might also like