End_Date<\/em> of the tasks are consecutive, then they are part of the same project. Samantha is interested in finding the total number of different projects completed.<\/p>\n\n\n\nWrite a query to output the start and end dates of projects listed by the number of days it took to complete the project in ascending order. If there is more than one project that have the same number of completion days, then order by the start date of the project.<\/p>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\nTask_ID<\/th> | Start_Date<\/th> | End_Date<\/th><\/tr><\/thead> |
---|
1<\/em><\/td>2015-10-01<\/em><\/td>2015-10-02<\/em><\/td><\/tr>2<\/em><\/td>2015-10-02<\/em><\/td>2015-10-03<\/em><\/td><\/tr>3<\/em><\/td>2015-10-03<\/em><\/td>2015-10-04<\/em><\/td><\/tr>4<\/em><\/td>2015-10-13<\/em><\/td>2015-10-14<\/em><\/td><\/tr>5<\/em><\/td>2015-10-14<\/em><\/td>2015-10-15<\/em><\/td><\/tr>6<\/em><\/td>2015-10-28<\/em><\/td>2015-10-29<\/em><\/td><\/tr>7<\/em><\/td>2015-10-30<\/em><\/td>2015-10-31<\/em><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n Sample Output<\/strong><\/p>\n\n\n\n2015-10-28 2015-10-29\n2015-10-30 2015-10-31\n2015-10-13 2015-10-15\n2015-10-01 2015-10-04<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h2>\n\n\n\nThe example describes following four<\/em> projects:<\/p>\n\n\n\n- Project 1<\/em>: Tasks 1<\/em>, 2<\/em> and 3<\/em> are completed on consecutive days, so these are part of the project. Thus start date of project is 2015-10-01<\/em> and end date is 2015-10-04<\/em>, so it took 3 days<\/em> to complete the project.<\/li>
- Project 2<\/em>: Tasks 4<\/em> and 5<\/em> are completed on consecutive days, so these are part of the project. Thus, the start date of project is 2015-10-13<\/em> and end date is 2015-10-15<\/em>, so it took 2 days<\/em> to complete the project.<\/li>
- Project 3<\/em>: Only task 6<\/em> is part of the project. Thus, the start date of project is 2015-10-28<\/em> and end date is 2015-10-29<\/em>, so it took 1 day<\/em> to complete the project.<\/li>
- Project 4<\/em>: Only task 7<\/em> is part of the project. Thus, the start date of project is 2015-10-30<\/em> and end date is 2015-10-31<\/em>, so it took 1 day<\/em> to complete the project.<\/li><\/ul>\n\n\n\n
<\/span>Solution – SQL Project Planning in SQL <\/strong><\/span><\/h2>\n\n\n\n<\/span>MySQL<\/strong><\/span><\/h3>\n\n\n\nSELECT START_DATE, MIN(END_DATE)\nFROM\n (SELECT START_DATE\n FROM PROJECTS\n WHERE START_DATE NOT IN\n (SELECT END_DATE\n FROM PROJECTS)) A,\n (SELECT END_DATE\n FROM PROJECTS\n WHERE END_DATE NOT IN\n (SELECT START_DATE\n FROM PROJECTS)) B\nWHERE START_DATE < END_DATE\nGROUP BY START_DATE\nORDER BY (MIN(END_DATE) - START_DATE), START_DATE;<\/pre>\n\n\n\n
| | | | | | | | | | | | | | | | | | | | |