We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
WITH project_groups AS (
SELECT Task_ID, Start_Date, End_Date,
SUM(is_new_project) OVER (ORDER BY Start_Date) AS project_id
FROM (SELECT p.*,
CASE
WHEN LAG(End_Date) OVER (ORDER BY Start_Date) = Start_Date THEN 0
ELSE 1 END AS is_new_project
FROM Projects p
) t
)
SELECT
MIN(Start_Date) AS project_start, MAX(End_Date) AS project_end
FROM project_groups
GROUP BY project_id
ORDER BY DATEDIFF(MAX(End_Date), MIN(Start_Date)), MIN(Start_Date);
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
SQL Project Planning
You are viewing a single comment's thread. Return to all comments →
WITH project_groups AS ( SELECT Task_ID, Start_Date, End_Date, SUM(is_new_project) OVER (ORDER BY Start_Date) AS project_id FROM (SELECT p.*, CASE WHEN LAG(End_Date) OVER (ORDER BY Start_Date) = Start_Date THEN 0 ELSE 1 END AS is_new_project FROM Projects p ) t ) SELECT MIN(Start_Date) AS project_start, MAX(End_Date) AS project_end FROM project_groups GROUP BY project_id ORDER BY DATEDIFF(MAX(End_Date), MIN(Start_Date)), MIN(Start_Date);