SlideShare a Scribd company logo
LAG OLAP SQL Function
I like the Clear definition that google gave me:
“Is an analytic function that lets you query
more than one row in a table at a time
without having to join the table to itself. It
returns values from a previous row in a table.
To return a value from the next row, try using
the LEAD function”
This function saved my butt on a request. My
story started like this. I received a request to
produce a list of prices changes by items
where it should show last price and date
when it was established and previous
price and its date from a price changes
history file. I was lazy enough to create a
program for this request.
SQL came to my request like a 911 call. I
remember sql has two functions called LAG
and LEAD that could help me on this, so I
concentrated on the LAG function which
was the one that helped me on reaching my
goal and is the one I will explain on below
example. I will show through this example
the technique I used.Be aware that file, field
name and data are no real just served as an
illustration for my example. My history file
looks like this
ITEM# PRICE CHGDATE
116059 75.00 20170906
116059 74.00 20180124
116059 70.00 20180221
116059 65.00 20180321
116059 69.00 20180418
116059 70.00 20180613
116059 73.00 20180801
116059 81.00 20180919
116059 85.00 20181017
116059 75.00 20181114
104217 64.50 20170717
104217 67.50 20171016
104217 68.50 20180115
104227 69.50 20180416
104227 70.50 20180716
104227 75.50 20181015
File HSTPRCFL(Price changes history file)
So for Item 116059, 104217 And 104217 I
need to produce below row
So using below sql statement with LAG
function (Remember it produce current row
and previous row in one row) I was able to
generate below table
ITEM# Actual
Price
Date Price_Before Date
104217 68.50 20181015 67.50 20171016
104227 75.50 20181015 70.50 20180716
116059 75.00 20181114 85.00 20181017
Check for each row that it produce the current row plus data from previous row.
ITEM# Price1 Date1 Price2 Date2
104217 64.50 20170717 64.40 20170717
104217 67.50 20171016 64.50 20170717
104217 68.50 20181015 67.50 20171016
104217 69.50 20180416 68.50 20180115
104227 70.50 20180716 69.50 20180416
104227 75.50 20181015 70.50 20180716
116059 75.00 20170906 75.50 20181015
116059 75.00 20180124 75.00 20170906
116059 70.00 20180221 75.00 20180124
116059 65.00 20180321 70.00 20180221
116059 69.00 20180418 65.00 20180321
116059 70.00 20180613 69.00 20180418
116059 73.00 20180801 70.00 20180613
116059 81.00 20180919 73.00 20180801
116059 85.00 20181017 81.00 20180919
116059 75.00 20181114 85.00 20181017
Note on below sql statement that we use IFNULL because first
row doesn't have a previous row and then it will show null on
PRICE2 and DATE2 column and we do not want this, so with this
info I produce a temporary table where I generate a row number by
ITEM and DATE1 using ROW_NUMBER() function, this row
number will help me to capture the last date price was changed for
each item and from here produce what I need.
Declare Global Temporary Table ITEMPRICETBL As
With TEMPTABLE As(
Select ITEM, PRICE1, DATE1,
IFNULL (LAG(PRICE) Over(Order By
PRICE, DATE1) , PRICE) PRICE2,
IFNULL ((LAG(CHGDATE) Over(Order By
ITEM#, CHGDATE), CHGDATE) DATE2
From HSTPRICFL)
Select ITEM, PRICE1, DATE1, PRICE2, DATE2,
ROW_NUMBER() Over(PARTITION BY ITEM ORDER BY
DATE1 DESC) ROW From TEMPTABLE) With Data With
Replace.
That sql will produce below table
ITEM# Price1 Date1 Price2 Date2 ROW
104217 68.50 20180115 67.50 20171016 1
104217 67.50 20171016 64.50 20170717 2
104217 64.50 20170717 64.50 20170717 3
104217 75.50 20181015 70.50 20180716 1
104227 70.50 20180716 69.50 20180416 2
104227 69.50 20180416 68.50 20180115 3
116059 75.00 20181114 85.00 20181017 1
116059 85.00 20181017 81.00 20180919 2
116059 81.00 20180919 73.00 20180801 3
116059 73.00 20180801 70.00 20180613 4
116059 70.00 20180613 69.00 20180418 5
116059 69.00 20180418 65.00 20180321 6
116059 65.00 20180321 70.00 20180221 7
116059 70.00 20180221 75.00 20180124 8
116059 75.00 20180124 75.00 20170906 9
116059 75.00 20170906 75.50 20181015 10
Note that each record with row=1 is the last date
price was changed for each item and besides
on this row I got previous row data (Price and
Date) from here what left is just a piece of cake.
Select ITEM, PRICE1,DATE1,PRICE2,DATE2
From TEMPRICETBL where “ROW”=1
And voila! Here is my list
ITEM# Actual
Price
Date Price_Before Date
104217 68.50 20180115 67.50 20171016
104227 75.50 20181015 70.50 20180716
116059 75.00 20181114 85.00 20181017
Note that each record with row=1 is the last date
price was changed for each item and besides
on this row I got previous row data (Price and
Date) from here what left is just a piece of cake.
Select ITEM, PRICE1,DATE1,PRICE2,DATE2
From TEMPRICETBL where “ROW”=1
And voila! Here is my list
ITEM# Actual
Price
Date Price_Before Date
104217 68.50 20180115 67.50 20171016
104227 75.50 20181015 70.50 20180716
116059 75.00 20181114 85.00 20181017

More Related Content

Similar to LAG SQL OLAP function.

Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Procedure rol against retail raw data
Procedure rol against retail raw dataProcedure rol against retail raw data
Procedure rol against retail raw data
Palash Halder
 
Sortsearch
SortsearchSortsearch
CIS 3650 Group ProjectList all functional dependencies and types.docx
CIS 3650 Group ProjectList all functional dependencies and types.docxCIS 3650 Group ProjectList all functional dependencies and types.docx
CIS 3650 Group ProjectList all functional dependencies and types.docx
sleeperharwell
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
KalyankumarVenkat1
 
Sql functions
Sql functionsSql functions
Sql functions
G C Reddy Technologies
 

Similar to LAG SQL OLAP function. (6)

Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
Procedure rol against retail raw data
Procedure rol against retail raw dataProcedure rol against retail raw data
Procedure rol against retail raw data
 
Sortsearch
SortsearchSortsearch
Sortsearch
 
CIS 3650 Group ProjectList all functional dependencies and types.docx
CIS 3650 Group ProjectList all functional dependencies and types.docxCIS 3650 Group ProjectList all functional dependencies and types.docx
CIS 3650 Group ProjectList all functional dependencies and types.docx
 
Oracle_Analytical_function.pdf
Oracle_Analytical_function.pdfOracle_Analytical_function.pdf
Oracle_Analytical_function.pdf
 
Sql functions
Sql functionsSql functions
Sql functions
 

Recently uploaded

E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
AnkitaPandya11
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
NishanthaBulumulla1
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 

Recently uploaded (20)

E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.fiscal year variant fiscal year variant.
fiscal year variant fiscal year variant.
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 

LAG SQL OLAP function.

  • 1. LAG OLAP SQL Function I like the Clear definition that google gave me: “Is an analytic function that lets you query more than one row in a table at a time without having to join the table to itself. It returns values from a previous row in a table. To return a value from the next row, try using the LEAD function”
  • 2. This function saved my butt on a request. My story started like this. I received a request to produce a list of prices changes by items where it should show last price and date when it was established and previous price and its date from a price changes history file. I was lazy enough to create a program for this request.
  • 3. SQL came to my request like a 911 call. I remember sql has two functions called LAG and LEAD that could help me on this, so I concentrated on the LAG function which was the one that helped me on reaching my goal and is the one I will explain on below example. I will show through this example the technique I used.Be aware that file, field name and data are no real just served as an illustration for my example. My history file looks like this
  • 4. ITEM# PRICE CHGDATE 116059 75.00 20170906 116059 74.00 20180124 116059 70.00 20180221 116059 65.00 20180321 116059 69.00 20180418 116059 70.00 20180613 116059 73.00 20180801 116059 81.00 20180919 116059 85.00 20181017 116059 75.00 20181114 104217 64.50 20170717 104217 67.50 20171016 104217 68.50 20180115 104227 69.50 20180416 104227 70.50 20180716 104227 75.50 20181015 File HSTPRCFL(Price changes history file)
  • 5. So for Item 116059, 104217 And 104217 I need to produce below row So using below sql statement with LAG function (Remember it produce current row and previous row in one row) I was able to generate below table ITEM# Actual Price Date Price_Before Date 104217 68.50 20181015 67.50 20171016 104227 75.50 20181015 70.50 20180716 116059 75.00 20181114 85.00 20181017
  • 6. Check for each row that it produce the current row plus data from previous row. ITEM# Price1 Date1 Price2 Date2 104217 64.50 20170717 64.40 20170717 104217 67.50 20171016 64.50 20170717 104217 68.50 20181015 67.50 20171016 104217 69.50 20180416 68.50 20180115 104227 70.50 20180716 69.50 20180416 104227 75.50 20181015 70.50 20180716 116059 75.00 20170906 75.50 20181015 116059 75.00 20180124 75.00 20170906 116059 70.00 20180221 75.00 20180124 116059 65.00 20180321 70.00 20180221 116059 69.00 20180418 65.00 20180321 116059 70.00 20180613 69.00 20180418 116059 73.00 20180801 70.00 20180613 116059 81.00 20180919 73.00 20180801 116059 85.00 20181017 81.00 20180919 116059 75.00 20181114 85.00 20181017
  • 7. Note on below sql statement that we use IFNULL because first row doesn't have a previous row and then it will show null on PRICE2 and DATE2 column and we do not want this, so with this info I produce a temporary table where I generate a row number by ITEM and DATE1 using ROW_NUMBER() function, this row number will help me to capture the last date price was changed for each item and from here produce what I need. Declare Global Temporary Table ITEMPRICETBL As With TEMPTABLE As( Select ITEM, PRICE1, DATE1, IFNULL (LAG(PRICE) Over(Order By PRICE, DATE1) , PRICE) PRICE2, IFNULL ((LAG(CHGDATE) Over(Order By ITEM#, CHGDATE), CHGDATE) DATE2 From HSTPRICFL) Select ITEM, PRICE1, DATE1, PRICE2, DATE2, ROW_NUMBER() Over(PARTITION BY ITEM ORDER BY DATE1 DESC) ROW From TEMPTABLE) With Data With Replace.
  • 8. That sql will produce below table ITEM# Price1 Date1 Price2 Date2 ROW 104217 68.50 20180115 67.50 20171016 1 104217 67.50 20171016 64.50 20170717 2 104217 64.50 20170717 64.50 20170717 3 104217 75.50 20181015 70.50 20180716 1 104227 70.50 20180716 69.50 20180416 2 104227 69.50 20180416 68.50 20180115 3 116059 75.00 20181114 85.00 20181017 1 116059 85.00 20181017 81.00 20180919 2 116059 81.00 20180919 73.00 20180801 3 116059 73.00 20180801 70.00 20180613 4 116059 70.00 20180613 69.00 20180418 5 116059 69.00 20180418 65.00 20180321 6 116059 65.00 20180321 70.00 20180221 7 116059 70.00 20180221 75.00 20180124 8 116059 75.00 20180124 75.00 20170906 9 116059 75.00 20170906 75.50 20181015 10
  • 9. Note that each record with row=1 is the last date price was changed for each item and besides on this row I got previous row data (Price and Date) from here what left is just a piece of cake. Select ITEM, PRICE1,DATE1,PRICE2,DATE2 From TEMPRICETBL where “ROW”=1 And voila! Here is my list ITEM# Actual Price Date Price_Before Date 104217 68.50 20180115 67.50 20171016 104227 75.50 20181015 70.50 20180716 116059 75.00 20181114 85.00 20181017
  • 10. Note that each record with row=1 is the last date price was changed for each item and besides on this row I got previous row data (Price and Date) from here what left is just a piece of cake. Select ITEM, PRICE1,DATE1,PRICE2,DATE2 From TEMPRICETBL where “ROW”=1 And voila! Here is my list ITEM# Actual Price Date Price_Before Date 104217 68.50 20180115 67.50 20171016 104227 75.50 20181015 70.50 20180716 116059 75.00 20181114 85.00 20181017