-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject Planning
More file actions
25 lines (21 loc) · 780 Bytes
/
Project Planning
File metadata and controls
25 lines (21 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
First, extract the start_date which not the same with the end_date and end_date which not the same with the start_date.
Then combine this two information, with the requirement that start_date < end_date and use the MIN() function to extract the minimum end_date for each start_date.
Then sort it by the different days between start and end date and by the start date
*/
WITH A AS
(SELECT Start_Date
FROM Projects
WHERE Start_Date NOT IN (SELECT End_Date FROM Projects))
, B AS
(SELECT End_Date
FROM Projects
WHERE End_Date NOT IN (SELECT Start_Date FROM Projects))
, NP AS
(SELECT Start_Date, MIN(End_Date) AS End_date
FROM A,B
WHERE Start_Date < End_Date
GROUP BY Start_Date)
SELECT *
FROM NP
ORDER BY DATEDIFF(day, Start_Date, End_Date), Start_date;