0% found this document useful (0 votes)
13 views6 pages

Handouts - 13 (OLAP Fun)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Handouts - 13 (OLAP Fun)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Quality Thought ETL Testing Daily Handouts

Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
Session-13
Topics Covered in session:
 OLAP Functions
 Rank
 Row_Number
 Csum
 Msum
 Mavg
 Qualify
 Partition By

Rank: For the purpose of finding or deriving rank values based on a particular column of
the table need to use Rank function.
Syntax: Rank(Columnname)
How it will works: Whenever we are using Rank function in sql by default your data will
sort in descending order by using column rank function. After that it will derive rank
values.

Scenario 100: By using Rank function what is the output of below table.
Emp Rank function output
Eid Ename Salary Did Eid Ename Salary Did R1
1 A 3000 10 4 D 5000 20 1
2 B 2000 10 5 E 4000 30 2
3 C 1000 20 1 A 3000 10 3
4 D 5000 20 2 B 2000 10 4
5 E 4000 30 3 C 1000 20 5

Sel Eid,Ename,Salary,Did,Rank(Salary) As R1 from Emp

Scenario 101: From emp table display information for 4th highest salary corresponding
details.
Sel Eid,Ename,Salary,Did,Rank(Salary) As R1 from Emp
Qualify R1=4

Scenario 102: From emp table display information for 4th least salary corresponding
details.
Sel Eid,Ename,Salary,Did,Rank(Salary Asc) As R1 from Emp Qualify R1=4

Scenario 103: From emp table display information for 2nd highest salary corresponding
details for each and every department.

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]
Quality Thought ETL Testing Daily Handouts

Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
Sel Eid,Ename,Salary,Did,Rank(Salary) As R1 from Emp Qualify R1=2 Group By Did
Note:When we are using OLAP Functions to derive a column and if you want to apply
condition on corresponding column we have to use QUALIFY clause.

Clauses Usage
Where To apply condition on normal columns
Having To apply conditions on aggregate functions
Qualify To apply conditions on OLAP function

Rank vs DenseRank: In Teradata we are not having Dense rank function. But in Oracle we
are having Dnese Rank function.
- We can say Dense Rank will work similar to Rank function when Rank column is having
unique values.
- But when Rank column is having duplicate records then Rank function will skip Rank
values in output but Dense Rank will not skip Rank values.

Scenario 104: Use Rank and Dense Rank functions on Sal column of Emp table and give
output.
Emp Rank and DenseRank function output
Eid Ename Salary Did Eid Ename Salary Did Rank(Sal) DenseRank(Sal)
1 A 3000 10 4 D 5000 20 1 1
2 B 3000 10 5 E 5000 30 1 1
3 C 1000 20 1 A 3000 10 3 2
4 D 5000 20 2 B 3000 10 3 2
5 E 5000 30 3 C 1000 20 5 3

Row_Number: For the purpose of generating sequence values into output based on
particular column of the table we have to use Row_Number.
Syntax: Row_number() Over(Order by Columnname)
How it works: First data will sort based on order by column then sequence values will
assign.

Scenario 105: Generate sequence values into output based on Eid column of Emp table.
select eid,row_number() over(order by Eid desc) as r1,
row_number() over(order by Eid asc) as r2 From emp
Output of
Emp Row_Num function
Eid Ename Salary Did Eid R1 R2
1 A 3000 10 1 5 1
2 B 3000 10 2 4 2
3 C 1000 20 3 3 3
4 D 5000 20 4 2 4

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]
Quality Thought ETL Testing Daily Handouts

Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
5 E 5000 30 5 1 5

Scenario 106: Display second highest salary corresponding details into output screen.
Approach 1: (Using Subquery)
Sel * From Emp Where Sal=
(Select Max(Sal) From Emp Where sal notin (Sel Max(Sal) From Emp)))
Approach 2 : (Using Rank function)
Select * From Emp Qualify Rank(Sal)=2
Approach 3 : (Using Row_Number)
Select * From Emp
Qualify Row_Number() Over(Order by Sal Desc)=2

Note: By using Rank and Row_Number we can populate sequence values if table is having
all unique values. If table is having duplicate records then Rank function will skip Ranks but
Row_number will populate same kind sequence values into output.

Scenario 107: Give the output below query by using Emp table data as input.
Select Eid,Sal, Rank(Sal) As R1, Row_Number() Over(order by Sal Desc) as R2 from Emp

Output of Rank &


Emp Row_Num function
Eid Ename Salary Did Eid Sal R1 R2
1 A 3000 10 4 5000 1 1
2 B 3000 10 5 5000 1 2
3 C 1000 20 1 3000 3 3
4 D 5000 20 2 3000 3 4
5 E 5000 30 3 1000 5 5

Scenario 108: Populate sequence values for each and every department.
Sel Eid,Ename,Did,Row_Number() Over(Partition By Did Order by Eid) as R1 From Emp
Sel Eid,Ename,Did,Row_Number() Over(Order by Eid) as R1 From Emp Group By Did
Both queries will work in same manner and both will similar kind of output.

CSUM() : For the purpose of finding sum of a particular column in cumulative basis we have
to use CSUM function. It will sum up current record with all previous records.
Syntax: Csum(Col1,Col2,Col3……..)
How It works: In syntax first column will represent based on which column you want to
find out sum and remaining columns will represent how you want to sort data.
Eg: Csum(Sal,Eid)
If you use above function first data will sort based on Eid column then it is finding
Cumulative sum based on Sal column.

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]
Quality Thought ETL Testing Daily Handouts

Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
Scenario 109: Find Cumulative sum based on Sal column of Emp table by sorting data in
Asc order using Eid.
Sel Eid,Ename,Sal,Csum(Sal,Eid) as C_Sum From Emp
Output of Csum
Emp Function
Eid Ename Salary Did Eid Ename Salary C_Sum
3 A 1000 10 1 C 3000 3000
2 B 2000 10 2 B 2000 5000
1 C 3000 20 3 A 1000 6000
4 D 4000 20 4 D 4000 10000
5 E 5000 30 5 E 5000 15000

Scenario 110: Find Cumulative sum based on Sal column of Emp table by sorting data in
Desc order using Eid.
Sel Eid,Ename,Sal,Csum(Sal,Eid Desc) as C_Sum From Emp
Output of Csum
Emp Function
Eid Ename Salary Did Eid Ename Salary C_Sum
3 A 1000 10 5 E 5000 5000
2 B 2000 10 4 D 4000 9000
1 C 3000 20 3 A 1000 12000
4 D 4000 20 2 B 2000 14000
5 E 5000 30 1 C 3000 15000

Scenario 111: Find Cumulative sum based on Sal column of Emp table by sorting data in
Desc order using Eid for each and every department.
Sel Eid,Ename,Sal,Csum(Sal,Eid Desc) as C_Sum From Emp Group by Did
Output of Csum
Emp Function
Eid Ename Salary Did Eid Ename Salary Did C_Sum
3 A 1000 10 3 A 1000 10 1000
2 B 2000 10 2 B 2000 10 3000
1 C 3000 20 4 D 4000 20 4000
4 D 4000 20 1 C 3000 20 7000
5 E 5000 30 5 E 5000 30 5000

MSUM(): For the purpose of finding sum based on particular period of records we have to
using MSUM function.(Moving Sum)
Syntax: Msum(Col1,Period,Col2,Col3…..)
How It will works: Col1 represent based on which column we need to find out Moving Sum,
Period represents how many records you want to Sum up. Remaining columns in the
function represent how you want to sort data before performing Moving Sum

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]
Quality Thought ETL Testing Daily Handouts

Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
Eg: MSUM(Sal,2,Eid)
With above function first data will sort using Eid column, then moving sum will find out
based on Sal column with the period of 2. 2 is nothing but Current record+Preiovus record.

Period How it works


1 Sum with only current record
2 Sum with Current Record +Previous record
3 Sum with Current Record +Previous Record+ Previous Previous
record

Scenario 112: Find out MSum based on Sal column using period 1,2,3 and give the output.
Sel Eid,Ename,Sal,
Msum(Sal,1,Eid) As M1,
Msum(Sal,2,Eid) as M2,
Msum(Sal,3,Eid) as M3
From Emp
Output of Msum
Emp Function
Eid Ename Salary Did Eid Ename Salary M1 M2 M3
3 A 1000 10 1 C 3000 3000 3000 3000
2 B 2000 10 2 B 2000 2000 5000 5000
1 C 3000 20 3 A 1000 1000 3000 6000
4 D 4000 20 4 D 4000 4000 5000 7000
5 E 5000 30 5 E 5000 5000 9000 10000

Scenario 113: Find out MSum based on Sal column for every department with period 2.
Sel Eid,Ename,Sal,
Msum(Sal,2,Eid) as M2
From Emp Group by Did

Mavg(): For the purpose of find average based on particular period of records we have to
use MAVG function. (Moving Average)
Syntax: Mavg(Col1,Period,Col2,Col3….)
How It will works: Col1 represent based on which column we need to find out Moving
average, Period represents how many records you want to average. Remaining columns in
the function represent how you want to sort data before performing Moving average
Eg: Mavg(Sal,2,Eid)
With above function first data will sort using Eid column, then moving avg will find out
based on Sal column with the period of 2. 2 is nothing but (Current record+Preiovus
record)/No of records (2).

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]
Quality Thought ETL Testing Daily Handouts

Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
Scenario 114: Find out Mavg based on Sal column using period 1,2,3 and give the output.
Sel Eid,Ename,Sal,
Mavg(Sal,1,Eid) As M1,
Mavg(Sal,2,Eid) as M2,
Mavg(Sal,3,Eid) as M3
From Emp
Output of Mavg
Emp Function
Eid Ename Salary Did Eid Ename Salary M1 M2 M3
3 A 1000 10 1 C 3000 3000 3000 3000
2 B 2000 10 2 B 2000 2000 2500 2500
1 C 3000 20 3 A 1000 1000 1500 2000
4 D 4000 20 4 D 4000 4000 2500 2333.33
5 E 5000 30 5 E 5000 5000 4500 3333.33

Scenario 115: Find out Mavg based on Sal column for every department with period 2.
Sel Eid,Ename,Sal,
Mavg(Sal,2,Eid) as M2
From Emp Group by Did

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]

You might also like