SlideShare uma empresa Scribd logo
1 de 19
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?

Mais conteúdo relacionado

Mais procurados (20)

Oracle SQL Advanced
Oracle SQL AdvancedOracle SQL Advanced
Oracle SQL Advanced
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
 
SQL and NoSQL Better Together in Alasql
SQL and NoSQL Better Together in AlasqlSQL and NoSQL Better Together in Alasql
SQL and NoSQL Better Together in Alasql
 
Les03 Single Row Function
Les03 Single Row FunctionLes03 Single Row Function
Les03 Single Row Function
 
SQL subquery
SQL subquerySQL subquery
SQL subquery
 
PLSQL CURSOR
PLSQL CURSORPLSQL CURSOR
PLSQL CURSOR
 
Oracle sql & plsql
Oracle sql & plsqlOracle sql & plsql
Oracle sql & plsql
 
Oracle: Functions
Oracle: FunctionsOracle: Functions
Oracle: Functions
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
Complex queries in sql
Complex queries in sqlComplex queries in sql
Complex queries in sql
 
Sub query_SQL
Sub query_SQLSub query_SQL
Sub query_SQL
 
5.Analytical Function.pdf
5.Analytical Function.pdf5.Analytical Function.pdf
5.Analytical Function.pdf
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
SQL BASIC QUERIES SOLUTION ~hmftj
SQL BASIC QUERIES SOLUTION ~hmftjSQL BASIC QUERIES SOLUTION ~hmftj
SQL BASIC QUERIES SOLUTION ~hmftj
 
SQL
SQLSQL
SQL
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
Analytic & Windowing functions in oracle
Analytic & Windowing functions in oracleAnalytic & Windowing functions in oracle
Analytic & Windowing functions in oracle
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Sql loader good example
Sql loader good exampleSql loader good example
Sql loader good example
 

Semelhante a Oracle sql analytic functions

dplyr Package in R
dplyr Package in Rdplyr Package in R
dplyr Package in RVedant Shah
 
Company segmentation - an approach with R
Company segmentation - an approach with RCompany segmentation - an approach with R
Company segmentation - an approach with RCasper Crause
 
Enabling Applications with Informix' new OLAP functionality
 Enabling Applications with Informix' new OLAP functionality Enabling Applications with Informix' new OLAP functionality
Enabling Applications with Informix' new OLAP functionalityAjay Gupte
 
Olap Functions Suport in Informix
Olap Functions Suport in InformixOlap Functions Suport in Informix
Olap Functions Suport in InformixBingjie Miao
 
Assumptions: Check yo'self before you wreck yourself
Assumptions: Check yo'self before you wreck yourselfAssumptions: Check yo'self before you wreck yourself
Assumptions: Check yo'self before you wreck yourselfErin Shellman
 
Adding measures to Calcite SQL
Adding measures to Calcite SQLAdding measures to Calcite SQL
Adding measures to Calcite SQLJulian Hyde
 
Procedure rol against retail raw data
Procedure rol against retail raw dataProcedure rol against retail raw data
Procedure rol against retail raw dataPalash Halder
 
cost II chapter 1.pptx
cost II chapter 1.pptxcost II chapter 1.pptx
cost II chapter 1.pptxZAKIRKASSAYE
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptxSabrinaShanta2
 
IT301-Datawarehousing (1) and its sub topics.pptx
IT301-Datawarehousing (1) and its sub topics.pptxIT301-Datawarehousing (1) and its sub topics.pptx
IT301-Datawarehousing (1) and its sub topics.pptxReneeClintGortifacio
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questionsPyadav010186
 
Engine90 crawford-decision-making (1)
Engine90 crawford-decision-making (1)Engine90 crawford-decision-making (1)
Engine90 crawford-decision-making (1)Divyansh Dokania
 
Getting Started with MDX 20140625a
Getting Started with MDX 20140625aGetting Started with MDX 20140625a
Getting Started with MDX 20140625aRon Moore
 
Divide and Be Conquered?
Divide and Be Conquered?Divide and Be Conquered?
Divide and Be Conquered?brooksaix
 
I really need help with this Assignment Please in C programming not .pdf
I really need help with this Assignment Please in C programming not .pdfI really need help with this Assignment Please in C programming not .pdf
I really need help with this Assignment Please in C programming not .pdfpasqualealvarez467
 

Semelhante a Oracle sql analytic functions (20)

dplyr Package in R
dplyr Package in Rdplyr Package in R
dplyr Package in R
 
Company segmentation - an approach with R
Company segmentation - an approach with RCompany segmentation - an approach with R
Company segmentation - an approach with R
 
Enabling Applications with Informix' new OLAP functionality
 Enabling Applications with Informix' new OLAP functionality Enabling Applications with Informix' new OLAP functionality
Enabling Applications with Informix' new OLAP functionality
 
Olap Functions Suport in Informix
Olap Functions Suport in InformixOlap Functions Suport in Informix
Olap Functions Suport in Informix
 
Assumptions: Check yo'self before you wreck yourself
Assumptions: Check yo'self before you wreck yourselfAssumptions: Check yo'self before you wreck yourself
Assumptions: Check yo'self before you wreck yourself
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 
Adding measures to Calcite SQL
Adding measures to Calcite SQLAdding measures to Calcite SQL
Adding measures to Calcite SQL
 
Procedure rol against retail raw data
Procedure rol against retail raw dataProcedure rol against retail raw data
Procedure rol against retail raw data
 
cost II chapter 1.pptx
cost II chapter 1.pptxcost II chapter 1.pptx
cost II chapter 1.pptx
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
 
IT301-Datawarehousing (1) and its sub topics.pptx
IT301-Datawarehousing (1) and its sub topics.pptxIT301-Datawarehousing (1) and its sub topics.pptx
IT301-Datawarehousing (1) and its sub topics.pptx
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
Data ware housing- Introduction to olap .
Data ware housing- Introduction to  olap .Data ware housing- Introduction to  olap .
Data ware housing- Introduction to olap .
 
Engine90 crawford-decision-making (1)
Engine90 crawford-decision-making (1)Engine90 crawford-decision-making (1)
Engine90 crawford-decision-making (1)
 
Getting Started with MDX 20140625a
Getting Started with MDX 20140625aGetting Started with MDX 20140625a
Getting Started with MDX 20140625a
 
Divide and Be Conquered?
Divide and Be Conquered?Divide and Be Conquered?
Divide and Be Conquered?
 
Ali upload
Ali uploadAli upload
Ali upload
 
I really need help with this Assignment Please in C programming not .pdf
I really need help with this Assignment Please in C programming not .pdfI really need help with this Assignment Please in C programming not .pdf
I really need help with this Assignment Please in C programming not .pdf
 
breakeven point
breakeven pointbreakeven point
breakeven point
 
DW-lecture2.ppt
DW-lecture2.pptDW-lecture2.ppt
DW-lecture2.ppt
 

Último

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 

Último (20)

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

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