Custom Search
www.rocket99.com : Technical Guides Sybase Oracle UNIX Javascript


Technical Guides
Sybase
Oracle
UNIX
Javascript




Of Interest

Business Intelligence and Analytics



Oracle Training





Sybase » Transact-SQL » Coding » Lead and Lag

Lead and Lag with pure SQL

     




Lead and Lag (aka Window Clause with Partition)






It is possible to do these types of queries without the lead/lag functions ... here's the trick:





- Construct a query which gives you the current id/date and the next id/date


- Wrap a query around this, join, to get the values






Note: Lead / Lag is considered an OLAP feature








Sample, Desired End Result




Sales results for North America

Current Month Last Month
Bob Smith 53212 43728
Steve Kane 75212 32819
Mary Jones 65361 57827
Fred Tuttle 43992 32782
Ray Rogers 78923 72882



Notes:

Lead/Lag works on many different key (partition) types:
- Integer Ids
- Character
- Date/Time

Gaps in keys, non-consecutive keys are not a problem.

e.g. 200510
200511
200512
200601
200602

Or
9/1/2006
9/4/2006
9/5/2006
9/6/2006



-- This query joins the table to itself, and produces a simply 2 column result
set.

select t1.region_id, t1.fiscal_month_id as fiscal_month_id1,
min(t2.fiscal_month_id) as fiscal_month_id2
from sales_data t1, sales_data t2
where t2.fiscal_month_id > t1.fiscal_month_id and
t2.region_id = t1.region_id and
t2.gross_sales > 150 and t1.region_id = 1023
group by t1.region_id, t1.fiscal_month_id
having min(t2.fiscal_month_id) > t1.fiscal_month_id
go


print ' '
go
print 'Report'
go


--- Finished query


select t1.region_id, t1.fiscal_month_id, t1.gross_sales 'current',
t2.gross_sales 'next period'
from
(
select t1.region_id, t1.fiscal_month_id as fiscal_month_id1,
min(t2.fiscal_month_id) as fiscal_month_id2
from sales_data t1, sales_data t2
where t2.fiscal_month_id > t1.fiscal_month_id and
t2.region_id = t1.region_id and
t2.gross_sales > 150 and t1.region_id = 1023
group by t1.region_id, t1.fiscal_month_id
having min(t2.fiscal_month_id) > t1.fiscal_month_id
) t3,
sales_data t1, sales_data t2
where t1.fiscal_month_id = t3.fiscal_month_id1 and
t2.fiscal_month_id = t3.fiscal_month_id2 and
t1.region_id = t3.region_id and
t2.region_id = t3.region_id
order by 1
go


print ' '
go
print 'Data Sample'
go

select fiscal_month_id, region_id, gross_sales
from sales_data where region_id = 1023
order by fiscal_month_id
go










Sybase : Related Topics

Sybase : Transact-SQL : Transact SQL: numeric functions
Sybase : Transact-SQL : Transact SQL: string functions
Sybase : Transact-SQL : Transact SQL: date/time functions
Sybase : Transact-SQL : Transact SQL: misc functions
Sybase : Transact-SQL : Transact SQL: Conditionals
Sybase : Transact-SQL : Transact SQL: looping constructs
Sybase : Transact-SQL : Transact SQL: Cursors
Sybase : Transact-SQL : Transact SQL: Complex Updates
Sybase : Transact-SQL : Transact SQL: Finding duplicate rows in a table
Sybase : Transact-SQL : Using Temporary Tables
Sybase : Transact-SQL : Inner/Outer Joins
Sybase : Transact-SQL : Isolation Levels
Sybase : Transact-SQL : Reporting: SQL Performance and Tuning
Sybase : Transact-SQL : Case Statement
Sybase : Transact-SQL : Date processing: stripping time from datetime
Sybase : Transact-SQL : Safely delete a large number of rows without blowing the t-log
Sybase : Transact-SQL : Transact SQL: date/time formats
Sybase : Transact-SQL : Creating a stored procedure
Sybase : Transact-SQL : Custom query plans
Sybase : Transact-SQL : Rowcount setting - limiting result sets
Sybase : Transact-SQL : Ranking Data - with Duplicates
Sybase : Transact-SQL : Forcing an index in a query
Sybase : Transact-SQL : Median Calculation, with pure SQL

Sybase Web Site
Sybase iAnywhere Mobile Web Site
Oracle Enterprise Web Site



Get the latest Rocket99 news and tech tips via






Site Index About this Guide to Sybase, Oracle, and UNIX Contact Us Advertise on this site




Copyright © 2016 Stoltenbar Inc All Rights Reserved.