3. Aggregate Functions
* Function used to group together data resulting in a total for that group
*Some examples:
* Sum
* Count
* Max
* Min
* Use “group by” clause to group rows together
* Don’t care to see all the rows that make up the total.
4. Aggregate - Sum
select category, month_no, sum( sales ) as sales
from purchases
group by category, month_no;
category month_no sales
Action Figures 1 $ 3,000
Dolls 1 $ 200
Plush 1 $ 1,000
Video Games 1 $ 1,000
Action Figures 2 $ 1,700
Dolls 2 $ 175
Plush 2 $ 300
…
5. Analytic Functions
* Introduced in Oracle v 8i
* Also known as windowing functions
* Use “partition by” clause to group rows together
* The last set of operations performed in a query, except for the order by clause.
* All joins, where, group by, and having clauses are completed before the analytic functions are
processed.
* Can only appear in the select list or order by clauses.
6. Why Analytic Functions?
* Why would you want to use Analytic Functions?
*Simplify your code.
*Code is easier to maintain and read.
*In the long run, type less.
* Can use native joins and sub-queries to get the same results, but why would you, if you had a
simplier way to do the same thing?
7. Anatomy of Analytic Functions
<analytic function> ( [ arg1, … argn] )
OVER ( [ PARTITION BY expr1 [,…,expn] ]
[ ORDER BY expr1 [ , …., expn ] ]
<windowing clause> )
9. Anatomy of Rank, dense_rank,
row_number
RANK | DENSE_RANK | ROW_NUMBER ( [ arg1, … argn] )
OVER ( [ PARTITION BY expr1 [,…,expn] ]
ORDER BY expr1 [ , …., expn ] )
10. Difference between Rank, dense_rank,
row_number
All
* serial number for each row based on some column or expression.
Rank
* If 2 rows have the same value (N), they receive the same sequential value of N, and the next
value N+1 will be skipped and the N+2 value will be given to the next record.
Dense_rank
* If 2 rows have the same value (N), it does not skip position N+1.
Row_number
* running serial number to the partition records based on the order by clause.
11. Rank, Dense_rank, Row_number
select month_no, category, sales,
row_number( ) over ( partition by month_no order by sales desc ) as row_no,
rank( ) over ( partition by month_no order by sales desc ) as rank_no,
dense_rank( ) over ( partition by month_no order by sales desc ) as dense_rank_no
from purchases;
month_no category sales row_no rank_no dense_rank_no
1Action Figures $ 3,000 1 1 1
1Plush $ 1,000 2 2 2
1Video Games $ 1,000 3 2 2
1Dolls $ 200 4 4 3
2Action Figures $ 1,700 1 1 1
2Video Games $ 500 2 2 2
2Plush $ 300 3 3 3
2Dolls $ 175 4 4 4
…
13. Ratio_to_report
select month_no, category, sales,
round( ratio_to_report( sales) over ( partition by month_no ) , 2 ) as ratio
from purchases;
month_nof category sales ratio
1Action Figures $ 3,000 0.58
1Video Games $ 1,000 0.19
1Plush $ 1,000 0.19
1Dolls $ 200 0.04
2Action Figures $ 1,700 0.64
2Video Games $ 500 0.19
2Plush $ 300 0.11
2Dolls $ 175 0.07
3Video Games $ 1,500 0.43
14. Lag and Lead
* Provides access to more than one row of a table at the same time without a self join
* Lag – access to data from a prior row.
* Lead – access to data from a following row.
* Examples:
* To compare prior value to current record value
* Compare prices of items to see if the item was an increase/decrease
15. Lag/Lead
select month_no, category, sales
,lag( sales) over ( partition by category order by month_no) as prior_mth_sales
,lead( sales) over ( partition by category order by month_no) as next_mth_sales
from purchases;
month_no category sales prior_mth_sales next_mth_sales
1Action Figures $ 3,000 $ 1,700
2Action Figures $ 1,700 $ 3,000 $ 1,200
3Action Figures $ 1,200 $ 1,700 $ 1,500
4Action Figures $ 1,500 $ 1,200 $ 2,000
5Action Figures $ 2,000 $ 1,500 $ 2,150
6Action Figures $ 2,150 $ 2,000
1Dolls $ 200 $ 175
2Dolls $ 175 $ 200 $ 400
3Dolls $ 400 $ 175 $ 300
16. Windowing Clause
* Windowing definition is a group of rows defined in the windowing clause.
* A sliding window of rows is defined for each row
* The window determines the range of rows used to perform the calculations for the current row.
* Window sizes can be based on either physical number of rows or a logical interval such as time.
* Windowing clause examples:
* UNBOUNDED PRECEDING : The window starts at the first row of the partition. Only available
for start points.
* UNBOUNDED FOLLOWING : The window ends at the last row of the partition. Only available
for end points.
* CURRENT ROW : The window starts or ends at the current row. Can be used as start or end
point.