Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have the date columns contains two dates of transaction, one is start_date and another one is end_date. The output will have two columns for start and end date

my table
id     transaction    date
10001      7001      2024-01-08
10001      7006      2024-01-09
10001      7001      2024-03-13
10001      7006      2024-03-24
10001      7006      2024-04-01

need this result
id       start           end
1001    2024-01-08    2024-01-09
1001    2024-03-13    2024-03-24
1001       null       2024-04-01

if transaction 7001 it is start date if 7006 it is end date
lead and lag function not work in db2

What I have tried:

SQL
select id,date0 as start,lag(date0)over(partition by tansaction order by date) 
try with lead and lag
return error code
no authorised routine named lead of type having compatible arguments was found sqlstate=42884
Posted
Updated 8-May-24 23:44pm
v2
Comments
M-Badger 9-May-24 5:19am    
All of the id's are the same, how do you differentiate between transactions ?
CHill60 9-May-24 8:52am    
Do you have any scope to upgrade your version of DB2? OLAP functions have been around for a while but 2010 is well out of date
Maciej Los 9-May-24 16:21pm    
What version of DB2?
Can you use DFSort programme (which supprosts JCL)?

You can mimic the effect of LEAD or LAG in earlier versions of DB2 by using a self-join and a row number. This is a much simplified demo in T-SQL (I no longer have access to DB2, sorry)
SQL
-- demo table with dummy data
declare @demo table (id int identity(1,1), cdata varchar(100));
insert into @demo (cdata) values
('A'),('B'),('C'),('D'),('E'),('F'),('G'),('H');

SQL
-- equivalent of LAG
select *
from @demo a
left outer join @demo b on b.id = a.id -1;
gives results
id	cdata	id	cdata
1	A		NULL	NULL
2	B		1		A
3	C		2		B
4	D		3		C
5	E		4		D
6	F		5		E
7	G		6		F
8	H		7		G
Equivalent of LEAD
SQL
select *
from @demo a
left outer join @demo b on b.id - 1 = a.id;
which gives results
id	cdata	id	cdata
1	A		2		B
2	B		3		C
3	C		4		D
4	D		5		E
5	E		6		F
6	F		7		G
7	G		8		H
8	H		NULL	NULL
Note - you should not rely on an ID column - the values will be unique but not necessarily contiguous, meaning the join will not work. Use instead a row number[^] in a sub-query, common table expression or temporary table - use the latter with an ID column if your version of DB2 does not support row_number()
 
Share this answer
 
Comments
Maciej Los 9-May-24 16:19pm    
5ed!
Look at the error message - it's probably telling you that your version of SQL Server is too old.
LAG and LEAD were not add3ed to Sql Server until the 2012 edition - so previous versions read the command as a standard function call and cannot find any appropriate function to call.

You can check your SQL server version with this command:
SQL
SELECT @@VERSION
 
Share this answer
 
Comments
Richard Deeming 9-May-24 5:08am    
The OP is using DB2 - it's buried in the question text. :)
Gehan Wafy 9-May-24 5:23am    
Iam usin db2 ver 2010

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900