473,414 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Oracle Migration - LAG function equivalent.

What is the equivalent of ORACLE LAG FUNCTION in DB2 on Z/OS and UDB on
LUW??

LAG FUNCTION RETURNS THE PREVIOUS ROW.
select id, createdate, name old_name, lag(name) over(partition by id
order by id,createddate desc)new_name
from
(select id,name,createdate
from people
union
select id,name,createdate
from people_arch)
order by createddate asc

Nov 12 '05 #1
4 10268
db*****@yahoo.com wrote:
What is the equivalent of ORACLE LAG FUNCTION in DB2 on Z/OS and UDB on
LUW??

LAG FUNCTION RETURNS THE PREVIOUS ROW.
select id, createdate, name old_name, lag(name) over(partition by id
order by id,createddate desc)new_name
from
(select id,name,createdate
from people
union
select id,name,createdate
from people_arch)
order by createddate asc

*Squinting hard* Looks like syntactic sugar for MAX() OVER() or MIN()
OVER() using a window of size 1 preceeding by one....
DB2 for zOS does not support OLAP, I suspect you will need to use a join
in that case to achieve portable code.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
As Serge said, no direct equivalent in DB2 on LUW, but you can simulate
it using a single row window specification, something similar to:

select id, createdate, name old_name,
max(name) over(partition by id order by id, createddate desc
rows between 1 preceding and 1 preceding) new_name
from
(select id,name,createdate
from people
union
select id,name,createdate
from people_arch)
order by createddate asc

Similarly, LEAD can be simulated with "between 1 following and 1
following".

Hope this helps,
Miro

Nov 12 '05 #3
Serge Rielau wrote:
db*****@yahoo.com wrote:
What is the equivalent of ORACLE LAG FUNCTION in DB2 on Z/OS and UDB on
LUW??

LAG FUNCTION RETURNS THE PREVIOUS ROW.
select id, createdate, name old_name, lag(name) over(partition by id
order by id,createddate desc)new_name
from
(select id,name,createdate
from people
union
select id,name,createdate
from people_arch)
order by createddate asc

*Squinting hard* Looks like syntactic sugar for MAX() OVER() or MIN()
OVER() using a window of size 1 preceeding by one....
DB2 for zOS does not support OLAP, I suspect you will need to use a join
in that case to achieve portable code.

Cheers
Serge

Just as an FYI - LAG and LEAD actually take an row offset and default
value as well. So LAG(name, 2, 'Fred') would return the value from 2
rows before the current row and FRED if you had stepped out of bounds.
If not specified (which is the example given here), the offset is
defaulted to 1 and the default value as NULL

Nov 12 '05 #4
Mark Townsend wrote:
Serge Rielau wrote:
db*****@yahoo.com wrote:
What is the equivalent of ORACLE LAG FUNCTION in DB2 on Z/OS and UDB on
LUW??

LAG FUNCTION RETURNS THE PREVIOUS ROW.
select id, createdate, name old_name, lag(name) over(partition by id
order by id,createddate desc)new_name
from
(select id,name,createdate
from people
union
select id,name,createdate
from people_arch)
order by createddate asc

*Squinting hard* Looks like syntactic sugar for MAX() OVER() or MIN()
OVER() using a window of size 1 preceeding by one....
DB2 for zOS does not support OLAP, I suspect you will need to use a
join in that case to achieve portable code.

Cheers
Serge

Just as an FYI - LAG and LEAD actually take an row offset and default
value as well. So LAG(name, 2, 'Fred') would return the value from 2
rows before the current row and FRED if you had stepped out of bounds.
If not specified (which is the example given here), the offset is
defaulted to 1 and the default value as NULL

Presuming name is not nullable...
MIN(CASE WHEN name IS NOT NULL THEN name ELSE 'Fred' END) OVER (...)
(?) Get's trickier when to emulate when name is nullable.
OLAP is quite powerful. Unfortunately not many users master it.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Azhar Bilgrami | last post by:
Dear Hi: Hope to see u in good health. " I want to migrate a database which is currently running Unix as Operating System and Oracle ver 6 as Database, it is also using oracle froms version 3...
1
by: Phil Hindmoor | last post by:
Hi, I am sure if anyone can help me, you guys can! I am an Informix Developer, moving to Oracle 8i and later databases. I am struggling to find the Oracle equivelant to many of the useful...
2
by: Amin Schoeib | last post by:
Hi, Like I see there is no equivalent to the Oracle decode Function In Postgres.Is there maybe somebody who wrote decode as a Function? Schoeib 4Tek Gesellschaft für angewandte...
3
by: db2sysc | last post by:
1. In the oracle trigger we have a SINGLE INSERT STORED PROCEDURE CALLED MUTLIPLE TIMES with different values. But when changed to DB2, it gives SQL -746 ?? Any help?? 2. In oracle we have ...
4
by: db2sysc | last post by:
All: What is the equivalent of Oracle BULK COLLECT in DB2 UDB? I do see MUTIPLE-ROW SELECT in DB2 ON Z/OS, where in INTO hostvariables can be declared as arrays. Is this possible in UDB...
1
by: db2sysc | last post by:
All. We have LOT of variables declared in the Oracle package as ORACLE CONSTANTS like, v_test CONSTANT INTEGER=1; When converting to DB2, MTK changes each of these CONSTANTs into...
0
by: usa777 | last post by:
I use 'CURRENT SERVER' special register to return the name of the database I am connected to in DB2. Is the function corresponding to 'CURRENT SERVER' in Oracle?
2
by: manindra | last post by:
Recently we migrated our product from MS-SQL 2000 to Oracle 9i. We see lot of performance degradation due to migration. Some times complex queries are hitting 10 fold slower than SQL Server. ...
0
Saii
by: Saii | last post by:
Hello Can somebody point me to a good document on PostgreSQL to Oracle migration steps. I have checked various sites including postgresql.org but I need to have a basic head start to evaluate the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.