I have a table that contains parts of projects with their start and end dates. This is in SQL Server 2017. The task is to list start, end dates and duration by project. (A project consists of all lines where start_date of next row is the same as end_date of previous. No lines overlap)
With this, you can reproduce the same sample data I'm working with.
CREATE TABLE PROJECTS (
Task_id int PRIMARY KEY ,
Start_Date date,
End_Date date )
INSERT INTO PROJECTS
VALUES
(1, '01/01/2020','01/02/2020'),
(2, '01/02/2020','01/03/2020'),
(3, '01/03/2020','01/04/2020'),
(4, '01/06/2020','01/07/2020'),
(5, '01/07/2020','01/08/2020'),
(6, '01/10/2020','01/11/2020'),
(7, '01/12/2020','01/13/2020'),
(8, '01/21/2020','01/22/2020'),
(9, '01/22/2020','01/23/2020'),
(10,'01/23/2020','01/24/2020')
based on the above data, my output would be as follows:
Project_id Start_Date End_Date Duration_days
1 01/01/2020 04/01/2020 3
2 06/01/2020 08/01/2020 2
3 10/01/2020 11/01/2020 1
4 12/01/2020 13/01/2020 1
5 21/01/2020 24/01/2020 3
The code below represents the first part of the execution, calculation of difference between end dates.
select projects.*,
datediff(day, end_date,
lead(end_date) over (partition by task_id order by end_date)
) as diff
from projects;
This is what I expected:
Task_id Start_Date End_Date diff
1 2020-01-01 2020-01-02 1
2 2020-01-02 2020-01-03 1
3 2020-01-03 2020-01-04 1
4 2020-01-06 2020-01-07 3
5 2020-01-07 2020-01-08 1
6 2020-01-10 2020-01-11 3
7 2020-01-12 2020-01-13 2
8 2020-01-21 2020-01-22 9
9 2020-01-22 2020-01-23 1
10 2020-01-23 2020-01-24 1
what I get is NULL in every row of Diff column.