Facebook Badge

Sunday, April 29, 2012

Roll Up operations in Oracle 10g XE

The purpose of this post is to let you guys know the basic OLAP rollup operation using Oracle 10g XE which is a free DBMS


First to begin with


Rollup is formally defined as an OLAP operation involving computing all of the data relationships for one or more dimensions. To do this, a computational relationship or formula might be defined.


I guess that didnt help even a bit. :p


To make things simpler


I would say a rollup  creates subtotals(totalling is an example relation considered in this case) that roll up from the most detailed level to a grand total, following a grouping list specified in the ROLLUP clause. 


ROLLUP takes as its argument an ordered list of grouping columns.


First, it calculates the standard aggregate values specified in the GROUP BY clause.


Then, it creates progressively higher-level subtotals, moving from right to left through the list of grouping columns. Finally, it creates a grand total.


To explain this consider the following example



create table Prac (
pid int primary key,
name varchar2(10),
country varchar2(15),
cost int
);


--------------------------------------------


After Filling in data


---------------------------------------------




PIDNAMECOUNTRYCOST
1RahulIN1000
2RohitIN1000
3RahulUS1500
4RohitUS1500
5AmitUS2000


----------------------------------------------


Then i execute a simple GROUP BY query on SUM(Cost) Group By Country


select country,sum(cost) from prac group by country




COUNTRYSUM(COST)
US5000
IN2000


I get the above result i.e sum of cost for each country.... this is simple




------------------------------------------------


Now a Rollup is an extension of Group By Clause


Consider the following 


select country,sum(cost) from prac group by rollup(country)




COUNTRYSUM(COST)
IN2000
US5000
-7000

Now the only difference is apart from producing a country-wise sum of cost.. the query also rolled them up to produce their total i.e 7000



-----------------------------------------------------


Now Consider another example
select name,country,sum(cost) from prac group by rollup(name,country)




NAMECOUNTRYSUM(COST)
AmitUS2000
Amit-2000
RahulIN1000
RahulUS1500
Rahul-2500
RohitIN1000
RohitUS1500
Rohit-2500
--7000

Here we see that we get Sum of cost for every Name,Country pair

Additionally we also get some more sub-totals.


Now at level 1 we get Sum(Cost) of every Name-Country pair now grouped by Name(since name is written before Country) for every pair.. hence arguements passed to rollup operation is an ordered list i.e order matters here .


And at level 2 further we get Sum(Cost) of every subtotals generated at level 1 now grouped by nothing i.e the entire total.


-----------------------------------------------------------
To explain the importance of ordering of parameters in rollup we consider yet another example


select name,country,sum(cost) from prac group by rollup(country,name)


Corresponding result set generated is-




NAMECOUNTRYSUM(COST)
RahulIN1000
RohitIN1000
-IN2000
AmitUS2000
RahulUS1500
RohitUS1500
-US5000
--7000


Here as we observe, at level 1 we get Sum(Cost) of every Name-Country pair now grouped by Country(since country is written before Name) for every pair


Again at level 2 further we get Sum(Cost) of every subtotals generated at level 1 now grouped by nothing i.e the entire total.




-------------------------------------------




Now we read the definition yet again..
Rollup is formally defined as an OLAP operation involving computing all of the data relationships for one or more dimensions. 



 "computing all of the data relationships for one or more dimensions"
Means
Like here computing subtotals at every level grouping one or more columns(dimensions)







No comments:

Post a Comment