I have a table:
table1
tran_id user_id start_date end_date
1 100 01-04-2018 02-04-2018
2 100 14-06-2018 14-06-2018
4 100 12-06-2018 12-06-2018
7 101 05-01-2018 05-01-2018
9 101 08-01-2018 08-01-2018
3 101 03-01-2018 03-01-2018
Date format is DD-MM-YYYY
I need to find the day difference ordered by user_id
and start_date
. The formula to find day difference is to subtract end_date
of the prior record and start_date
of the next.
Here is the expected output:
tran_id user_id start_date end_date day_diff
1 100 01-04-2018 02-04-2018 71
4 100 12-06-2018 12-06-2018 2
2 100 14-06-2018 14-06-2018 0
3 101 03-01-2018 03-01-2018 2
7 101 05-01-2018 05-01-2018 3
9 101 08-01-2018 08-01-2018 0
How to get this in an SQL query?