SlideShare a Scribd company logo
5
Most read
8
Most read
11
Most read
Oracle SQL Analytic
Functions
BY MELINDA WEBSTER
Toy Retailer Example
create table purchases ( category varchar2( 100 ) not null, year_no number not null,
month_no number not null, sales number );
category year_no month_no sales
Action Figures 2014 1 $ 3,000
Action Figures 2014 2 $ 1,700
Action Figures 2014 3 $ 1,200
Action Figures 2014 4 $ 1,500
Action Figures 2014 5 $ 2,000
Action Figures 2014 6 $ 2,150
Dolls 2014 1 $ 200
Dolls 2014 2 $ 175
Dolls 2014 3 $ 400
Dolls 2014 4 $ 300
Dolls 2014 5 $ 350
…
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.
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
…
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.
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?
Anatomy of Analytic Functions
<analytic function> ( [ arg1, … argn] )
OVER ( [ PARTITION BY expr1 [,…,expn] ]
[ ORDER BY expr1 [ , …., expn ] ]
<windowing clause> )
Analytic - Sum
select category, month_no, sales,
sum( sales ) over ( partition by category ) as ttl_cat_sales,
sum( sales ) over ( ) as ttl_sales
from purchases;
category month_no sales ttl category sales ttl sales
Action Figures 1 $ 3,000 $ 11,550 $ 26,075
Action Figures 2 $ 1,700 $ 11,550 $ 26,075
Action Figures 3 $ 1,200 $ 11,550 $ 26,075
Action Figures 4 $ 1,500 $ 11,550 $ 26,075
Action Figures 5 $ 2,000 $ 11,550 $ 26,075
Action Figures 6 $ 2,150 $ 11,550 $ 26,075
Dolls 1 $ 200 $ 1,825 $ 26,075
Dolls 2 $ 175 $ 1,825 $ 26,075
…
Anatomy of Rank, dense_rank,
row_number
RANK | DENSE_RANK | ROW_NUMBER ( [ arg1, … argn] )
OVER ( [ PARTITION BY expr1 [,…,expn] ]
ORDER BY expr1 [ , …., expn ] )
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.
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
…
Anatomy of Ratio_to_report
RATIO_TO_REPORT ( [ arg1, … argn] )
OVER ( [ PARTITION BY expr1 [,…,expn] ]
ORDER BY expr1 [ , …., expn ] )
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
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
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
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.
Anatomy of Windowing
<windowing_function_name > ( [ arg1, … argn] )
OVER ( [ PARTITION BY expr1 [,…,expn] ]
ORDER BY expr1 [ , …., expn ]
<windowing_clause>
)
Rolling Totals
select month_no, category, sales
,sum( sales) over ( partition by category order by month_no rows unbounded preceding )
as rolling_ttl
from purchases;
month_no category sales rolling_ttl
1Action Figures $ 3,000 $ 3,000
2Action Figures $ 1,700 $ 4,700
3Action Figures $ 1,200 $ 5,900
4Action Figures $ 1,500 $ 7,400
5Action Figures $ 2,000 $ 9,400
6Action Figures $ 2,150 $ 11,550
1Dolls $ 200 $ 200
2Dolls $ 175 $ 375
Questions?

More Related Content

ODP
Oracle SQL Advanced
PPTX
SQL Functions
PPT
Sql – Structured Query Language
PPTX
Group By, Order By, and Aliases in SQL
PPT
Introduction to structured query language (sql)
PPTX
Sql subquery
PDF
Oracle Advanced SQL and Analytic Functions
Oracle SQL Advanced
SQL Functions
Sql – Structured Query Language
Group By, Order By, and Aliases in SQL
Introduction to structured query language (sql)
Sql subquery
Oracle Advanced SQL and Analytic Functions

What's hot (20)

PPTX
Sql server ___________session_17(indexes)
PPSX
Analytic & Windowing functions in oracle
DOCX
Difference between all topics in oracle
PDF
OLTP vs OLAP
PDF
Partitioning tables and indexing them
PPTX
Oracle sql high performance tuning
PPT
PPT
Introduction to sql
PPTX
Basic sql Commands
PPT
Including Constraints -Oracle Data base
PPT
SQL subquery
PDF
SQL JOINS
PPTX
SQL UNION
PPTX
Stored procedure in sql server
PPT
Advanced sql
PPT
Aggregate functions
PPTX
PLPgSqL- Datatypes, Language structure.pptx
PPTX
Sql commands
DOC
PPTX
sql function(ppt)
Sql server ___________session_17(indexes)
Analytic & Windowing functions in oracle
Difference between all topics in oracle
OLTP vs OLAP
Partitioning tables and indexing them
Oracle sql high performance tuning
Introduction to sql
Basic sql Commands
Including Constraints -Oracle Data base
SQL subquery
SQL JOINS
SQL UNION
Stored procedure in sql server
Advanced sql
Aggregate functions
PLPgSqL- Datatypes, Language structure.pptx
Sql commands
sql function(ppt)
Ad

Similar to Oracle sql analytic functions (20)

PPT
Extreme querying with_analytics
PDF
Oracle_Analytical_function.pdf
PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
PPTX
Exploring Advanced SQL Techniques Using Analytic Functions
PDF
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
PPT
Olap Functions Suport in Informix
PPT
Enabling Applications with Informix' new OLAP functionality
PPTX
SQL Windowing
PDF
Windowing Functions - Little Rock Tech fest 2019
PDF
Windowing Functions - Little Rock Tech Fest 2019
PDF
Olapsql
PDF
Uncovering SQL Server query problems with execution plans - Tony Davis
PDF
Data Love Conference - Window Functions for Database Analytics
PDF
5.Analytical Function.pdf
PDF
advance-sqaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal.pdf
PPTX
Really using Oracle analytic SQL functions
PPT
Introduction to OLAP and OLTP Concepts - DBMS
PPTX
Project report aditi paul1
PPTX
Database Analysis, OLAP, Aggregate Functions
PDF
Introducing Windowing Functions (pgCon 2009)
Extreme querying with_analytics
Oracle_Analytical_function.pdf
Exploring Advanced SQL Techniques Using Analytic Functions
Exploring Advanced SQL Techniques Using Analytic Functions
OOW2016: Exploring Advanced SQL Techniques Using Analytic Functions
Olap Functions Suport in Informix
Enabling Applications with Informix' new OLAP functionality
SQL Windowing
Windowing Functions - Little Rock Tech fest 2019
Windowing Functions - Little Rock Tech Fest 2019
Olapsql
Uncovering SQL Server query problems with execution plans - Tony Davis
Data Love Conference - Window Functions for Database Analytics
5.Analytical Function.pdf
advance-sqaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaal.pdf
Really using Oracle analytic SQL functions
Introduction to OLAP and OLTP Concepts - DBMS
Project report aditi paul1
Database Analysis, OLAP, Aggregate Functions
Introducing Windowing Functions (pgCon 2009)
Ad

Recently uploaded (20)

PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
KodekX | Application Modernization Development
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Advanced Soft Computing BINUS July 2025.pdf
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
madgavkar20181017ppt McKinsey Presentation.pdf
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
KodekX | Application Modernization Development
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Advanced Soft Computing BINUS July 2025.pdf
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
“AI and Expert System Decision Support & Business Intelligence Systems”
Chapter 3 Spatial Domain Image Processing.pdf
madgavkar20181017ppt McKinsey Presentation.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Transforming Manufacturing operations through Intelligent Integrations
Reach Out and Touch Someone: Haptics and Empathic Computing

Oracle sql analytic functions

  • 2. Toy Retailer Example create table purchases ( category varchar2( 100 ) not null, year_no number not null, month_no number not null, sales number ); category year_no month_no sales Action Figures 2014 1 $ 3,000 Action Figures 2014 2 $ 1,700 Action Figures 2014 3 $ 1,200 Action Figures 2014 4 $ 1,500 Action Figures 2014 5 $ 2,000 Action Figures 2014 6 $ 2,150 Dolls 2014 1 $ 200 Dolls 2014 2 $ 175 Dolls 2014 3 $ 400 Dolls 2014 4 $ 300 Dolls 2014 5 $ 350 …
  • 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> )
  • 8. Analytic - Sum select category, month_no, sales, sum( sales ) over ( partition by category ) as ttl_cat_sales, sum( sales ) over ( ) as ttl_sales from purchases; category month_no sales ttl category sales ttl sales Action Figures 1 $ 3,000 $ 11,550 $ 26,075 Action Figures 2 $ 1,700 $ 11,550 $ 26,075 Action Figures 3 $ 1,200 $ 11,550 $ 26,075 Action Figures 4 $ 1,500 $ 11,550 $ 26,075 Action Figures 5 $ 2,000 $ 11,550 $ 26,075 Action Figures 6 $ 2,150 $ 11,550 $ 26,075 Dolls 1 $ 200 $ 1,825 $ 26,075 Dolls 2 $ 175 $ 1,825 $ 26,075 …
  • 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 …
  • 12. Anatomy of Ratio_to_report RATIO_TO_REPORT ( [ arg1, … argn] ) OVER ( [ PARTITION BY expr1 [,…,expn] ] ORDER BY expr1 [ , …., expn ] )
  • 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.
  • 17. Anatomy of Windowing <windowing_function_name > ( [ arg1, … argn] ) OVER ( [ PARTITION BY expr1 [,…,expn] ] ORDER BY expr1 [ , …., expn ] <windowing_clause> )
  • 18. Rolling Totals select month_no, category, sales ,sum( sales) over ( partition by category order by month_no rows unbounded preceding ) as rolling_ttl from purchases; month_no category sales rolling_ttl 1Action Figures $ 3,000 $ 3,000 2Action Figures $ 1,700 $ 4,700 3Action Figures $ 1,200 $ 5,900 4Action Figures $ 1,500 $ 7,400 5Action Figures $ 2,000 $ 9,400 6Action Figures $ 2,150 $ 11,550 1Dolls $ 200 $ 200 2Dolls $ 175 $ 375