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.
Find the previous end date of a task using LAG(end_date) OVER(ORDER BY end_date) window function
Assigning values to individual rows to form groups(new column name = group_check)
1. Assign 1 -> if the start_date and prev_end does not match or prev_end is NULL. This means it is the start of a new project
2. Assign 0 -> if the start_date and prev_end matches. This means it is a a task of the same project
Use the cummulative_sum SUM(group_check) OVER(ORDER BY end_date) on the group_check_column
3.1 Intuition - As long as cummulative sum stays same, it indicates that tasks are part of same project, else different projects
SELECT MIN(start_date) and MAX(end_date) from each group order by the cummulative sum value(which is essentially group_id)
ORDER BY DATEDIFF(MAX(end_date) - MIN(end_date)) => essentially means ordering by no of days
SQL Project Planning
You are viewing a single comment's thread. Return to all comments →
Steps: