Oracle12c Analytic Functions
Oracle12c Analytic Functions
Description:
BISP is committed to provide BEST learning material to the beginners and advance learners. In the
same series, we have prepared a complete end-to end Hands-on Beginners Guide for Oracle
Analytic Functions. The document focuses Grouping and Aggregating data using SQL
Join our professional training program and learn from experts.
History:
Version Description Change
0.1
Initial Draft
0.1
Review#1
www.bispsolutions.com
Author
Pawan Madanan
www.bisptrainigs.com
Publish Date
www.hyperionguru.com
Page 1
Contents:
ROLLUP Operator:.............................................................................................................................................. 3
CUBE Operator :................................................................................................................................................. 5
GROUPING FUNCTION:.................................................................................................................................... 9
GROUPING_ID................................................................................................................................................. 12
GROUPING SETS:........................................................................................................................................... 13
COMPOSITE COLUMNS.................................................................................................................................. 14
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 2
ROLLUP Operator:
About ROLLUP Operator:
ROLLUP is an extension to Group By clause.
ROLLUP enables a SELECT statement to calculate multiple levels of subtotals across a specified
group of dimensions.
Can be used by report makers to extract statistics and summary information from the result to use in
charts ,graphs , reports.
ROLLUP creates grouping by moving Right to Left along the list of columns in GROUP BY clause.
Syntax of ROLLUP :
SELECT GROUP BY ROLLUP(grouping_column_reference_list).
To execute queries based on ROLLUP Operator, I am going to use table
Tables used : order_details
Order_Details basically contains details of different orders.
Before using ROLLUP operator, lets see whole of the data present in order_details table.
For we will be executing the following query :
Select * from order_details;
Then click on run button specified in below image to execute the query .
Output:
www.bisptrainigs.com
www.hyperionguru.com
Page 3
a)Total quantity for every order on each correspoding product shipping date.
b)Total quantity for every order irrespective of the corresponding product shipping dates
c)Grand total of quantities.
With a single query execution,we get all of the above information . otherwise we would have to use
multiple select statements with UNION ALL , that would take multiple table access unlike in ROLL UP
operator , only single access to the base table is required.
Query :
..
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 4
Line labelled as 1 indicates a group totaled by both ship_date and order_number and is also answer to
our first requirement but for a particular order number and ship date. You can get other total values from other
rows, wherever ordernumber and ship date is not null.
Line labelled as 2 indicates a group totaled by order_number and also answer to second requirement but
for single order .
So how do we know ,that any particular row contains total for order_number irrespective of ship_date ?
The answer to this is , In rows where you find ship_date in null , then you directly say that total has been
calculated by order_number only(the null values shown here are generated by roll up operator, they are not
actually present in database ,so how to differentiate between null values generated by roll up operator and
stored null values has also been covered in this documentation, but the NULL s here are replaced by
Blank,so originally you will be seeing nulls when you will executing the above query NOT blanks , I did so , so
you dont get confused).
Other rows havent been labelled but can be seen and you can get the total value for other order_numbers
,wherever shipdate is NULL .
Line labelled as 3 indicates Grand total and also answer to our third requirement.
Note: The NULL s in the result are replaced by Blank,so originally you will be seeing nulls when you
will executing the above query NOT blanks , I did so , so you dont get confused.
Note: if you want to the end of the query result(as Scrolling down will be cumbersome) ,click anywhere on the
result set , and then press Ctrl + End.
CUBE Operator :
About CUBE Operator :
CUBE is an extension of the GROUP BY clause.
CUBE produces subtotals for all possible combinations of groupings specified .
Used for cross-tabular reports
Syntax of CUBE operator:
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 5
Output:
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 6
Query :
In the above query the cube operator created the following groups:
(productline,producttype,productname )
(productline)
(producttype)
(productname)
(productline,producttype)
(productline,productname)
(producttype,productname)
()
Total 6 sets are created by CUBE operator with three elements , from this you can infer a direct formula , ie
For n elements/columns ,
2n
Output:
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 7
..
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 8
Line labelled as 1 indicates a whole average and also answer to our 5th requirement(e)
Line labelled as 2 indicates a group average by product name and also answer to second requirement(for a
particular product if you will ) and also answer to our second requirement(b)
Line labelled as 3 indicates a group average by product type and also answer to our third requirement(c).
Line labelled as 4 indicates a group average by product type and product name also answer to our fourth
requirement(d).
Line labelled as 5 indicates a group average by product line , product type and also productname and also
answer to our first requirement(a).
GROUPING FUNCTION:
About grouping function :
It is used with ROLLUP or CUBE operator.
It is used to identify the columns in a group forming the subtotal.
It is used to distinguish between Stored nulls and the nulls produced by ROLLUP or CUBE operator.
It returns a value 0 or 1
Syntax:
GROUPING (Column)
Use of GROUPING Function :
a)To determine the columns involved in the aggregation(sum,avg,min)
b) identify whether a NULL value in a result set indicates :
-NULL value from the base table ie stored NULL value
-NULL value created by ROLLUP or CUBE.
Return Value Indicates :
If return value is 0, then it indicates:
-- the corresponding columns was used to calculate aggregate value
-- NULL value in the expression column is a stored NULL value
If return value is 1, then it indicates:
-- the corresponding columns was NOT used to calculate aggregate value
-- NULL value in the expression column is NOT a stored NULL value, rather it has been created by CUBE
or ROLLUP
Operator .
To execute queries based on GROUPING Operator, I am going to use table
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 9
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 10
Output:
Line labeled as 1, has AVERAGE_COST column has value 90 , which is average production cost , none of
the columns are included in the aggregation ,thus grouping function returns 1,1,1 in G_PRODUCTLINE,
G_PRODUCTTYPE,
G_PRODUCTNAME .
Line labeled as 2, has AVERAGE_COST column has value 73 , to calculate this value ,only productname
is included in the aggregation ,thus grouping function returns 0 for G_PRODUCTNAME and 1,1 in
G_PRODUCTLINE, G_PRODUCTTYPE respectively.
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 11
Line labeled as 3, has AVERAGE_COST column has value 470 , to calculate this value ,both productline,
productname were included in the aggregation ,thus grouping function returns 0 for G_PRODUCTNAME ,
G_PRODUCTLINE and 1 for G_PRODUCTTYPE respectively.
(scroll up to the preceding page to see what 0 or 1 in grouping function indicates.)
GROUPING_ID
which is just the combination of results from GROUPING function for each column specified.
Syntax:
GROUPING_ID(columns)
To execute queries based on GROUPING_ID Operator, I am going to use table
Tables used : dim_products
The query used in CUBE operator one . has been modified to show the use of grouping_ID function:
OUTPUT:
The GROUPING_ID function here is : GID_LINE_TYPE_NAME, which is actually concatenating the results
of Grouping functions into a bit vector and then returning the decimal equivalent of that .
For ex in the First Row where AVERAGE_COST= 90, the grouping function values are 1 , 1 , 1 ,
converting it into bit vector 111 , and the decimal equivalent of this is 7
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 12
In next row we have GROUPING function values as 1,1,0 , converting it into bit vector : 110 and its
decimal equivalent is : 6
NOTE: In the above output, I have used 3 grouping functions, you can avoid it directly use GROUPING_ID
function.
So , GROUPING_ID avoids the use of multiple GROUPING function and also you can make the
filtering conditions easier with the use single GROUPING_ID function , instead of multiple
GROUPING functions,
by using only single condition ie GROUPING_ID = n
GROUPING SETS:
I have specified only 2 groups , so on those groups only , aggregation will be performed
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 13
Output:
Line labelled 1 indicates aggregation by both PRODUCTLINE and PRODUCTNAME , as we specified in the
GROUPING SETS
Line labelled 2 indicates aggregation by PRODUCTNAME as we specified in the GROUPING SETS.
COMPOSITE COLUMNS
About composite columns :
Collection of columns treated as single unit.
ROLLUP(a,(b,c))
CUBE(a,(b,c))
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 14
Query :
Output:
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 15
Only on four groups, aggregation was performed ,because we have specified composite columns ,which are
treated as single unit,otherwise on 23 = 8 groups aggregation would have been performed.,
Groups formed because of composite columns are :
Line labeled by 1 indicates aggregation by productline , producttype,productname
Line labeled by 2 indicates aggregation by producttype and productname
Line labeled by 3 indicates aggregation by productline
Line labeled by 4 indicates average cost of production_cost column in the in dim_products table
www.bispsolutions.com
www.bisptrainigs.com
www.hyperionguru.com
Page 16