Growth ๐ŸŒณ/Practice ๐Ÿ’ป

[Hacker Rank] SQL Project Planning

์ธ” 2022. 9. 20. 18:39

๐Ÿ“ข ๋ณธ ํฌ์ŠคํŒ…์— ํ™œ์šฉ๋˜๋Š” ๊ธฐ๋ณธ ๋ฌธ์ œ ๋ฐ ์ž๋ฃŒ ์ถœ์ฒ˜๋Š” HackerRank ์ž„์„ ๋ฐํž™๋‹ˆ๋‹ค.

https://www.hackerrank.com


โ–  ๋ฌธ์ œ

https://www.hackerrank.com/challenges/sql-projects/problem

 

SQL Project Planning | HackerRank

Write 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.

www.hackerrank.com

You are given a table, Projects, containing three columns: Task_ID, Start_Date and End_Date. It is guaranteed that the difference between the End_Date and the Start_Date is equal to 1 day for each row in the table.

If the End_Date 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.

Write 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.

 

Sample Input

Sample Output

2015-10-28 2015-10-29
2015-10-30 2015-10-31
2015-10-13 2015-10-15
2015-10-01 2015-10-04

 

Explanation โ–ผ

๋”๋ณด๊ธฐ

The example describes following four projects:

  • Project 1: Tasks 1, 2 and 3 are completed on consecutive days, so these are part of the project. Thus start date of project is 2015-10-01 and end date is 2015-10-04, so it took 3 days to complete the project.
  • Project 2: Tasks 4 and 5 are completed on consecutive days, so these are part of the project. Thus, the start date of project is 2015-10-13 and end date is 2015-10-15, so it took 2 days to complete the project.
  • Project 3: Only task 6 is part of the project. Thus, the start date of project is 2015-10-28 and end date is 2015-10-29, so it took 1 day to complete the project.
  • Project 4: Only task 7 is part of the project. Thus, the start date of project is 2015-10-30 and end date is 2015-10-31, so it took 1 day to complete the project.

โ–  ํ’€์ด

๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ

> ํ”„๋กœ์ ํŠธ์˜ ๋๋‚ ์งœ(End_Date)๊ฐ€ ์—ฐ์ด์–ด์ง„๋‹ค๋ฉด, ๊ทธ ํ–‰์€ ๊ฐ™์€ ํ”„๋กœ์ ํŠธ์ด๋‹ค.

  ์‚ฌ๋งŒ๋‹ค๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ์™„๋ฃŒ๋œ ํ”„๋กœ์ ํŠธ์˜ ์ด ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธธ ์›ํ•จ.

  ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์™„๋ฃŒ๋œ ์ผ์ž์˜ ํ”„๋กœ์ ํŠธ์˜ '์‹œ์ž‘ ์ผ์ž'์™€ '์™„๋ฃŒ์ผ์ž'๋ฅผ ์ถœ๋ ฅํ•˜๊ณ 

  ๋งŒ์•ฝ ๊ฐ™์€ ์ผ์ž์— ์™„๋ฃŒ๋œ ํ”„๋กœ์ ํŠธ๋ผ๋ฉด, ์‹œ์ž‘์ผ์ž ์ˆœ์œผ๋กœ ๋‚˜์—ดํ•  ๊ฒƒ.

 

โ‘   ๊ฐ™์€ ํ”„๋กœ์ ํŠธ ์ฐพ๊ธฐ > ํ”„๋กœ์ ํŠธ์˜ end_date๊ฐ€ ๋‹ค๋ฅธ ํ–‰์˜ start_date์™€ ๋™์ผ

โ‘ก  ์™„๋ฃŒ๋œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ฐพ๋Š”๋‹ค > ํ”„๋กœ์ ํŠธ์˜ end_date๊ฐ€ ๋‹ค๋ฅธ ํ–‰์˜ start_date์— ์กด์žฌํ•ด์„  ์•ˆ๋จ.

 

 

 

> start_date ์ถ”์ถœ ํ…Œ์ด๋ธ” > End_Date ์ถ”์ถœ ํ…Œ์ด๋ธ”
SELECT Start_Date, 
                 ROW_NUMBER() over (ORDER BY Start_Date) rnk
 FROM Projects
 WHERE Start_Date NOT IN (SELECT DISTINCT End_Date
                                                           FROM Projects)
SELECT end_date,
            ROW_NUMBER() OVER (ORDER BY End_Date) rnk
 FROM Projects
 WHERE end_date NOT IN (SELECT DISTINCT Start_Date 
                                                        FROM Projects)

> ์œ„ ๋‘ ํ…Œ์ด๋ธ”์„ rnk์— ๋Œ€ํ•ด inner joinํ•˜๋ฉด ๊ฐ๊ฐ ํ”„๋กœ์ ํŠธ๊ฐ€ ๋๋‚˜๋Š” start_date์™€ end_date๊ฐ€ ์—ฐ๊ฒฐ๋˜๊ณ 
   ์ด ํ”„๋กœ์ ํŠธ๊ฐ€ 11๊ฐœ์ž„์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โ‘ข > ์™„๋ฃŒ ์ผ์ž๊ฐ€ ๊ฐ™์œผ๋ฉด ํ”„๋กœ์ ํŠธ ์†Œ์š”๊ธฐ๊ฐ„์ด ์งง์€ ๊ฒƒ๋ถ€ํ„ฐ > ์‹œ์ž‘ ์ผ์ž๊ฐ€ ๋น ๋ฅธ๊ฒƒ ๋ถ€ํ„ฐ ๋‚˜์—ด

SELECT p_start_date.Start_Date, p_end_date.End_Date
 FROM (SELECT Start_Date, 
               ROW_NUMBER() over (ORDER BY Start_Date) rnk
         FROM Projects
         WHERE Start_Date NOT IN (SELECT DISTINCT End_Date
                                   FROM Projects)) p_start_date
         INNER JOIN (
                     SELECT end_date,
                      ROW_NUMBER() OVER (ORDER BY End_Date) rnk
                      FROM Projects
                      WHERE end_date NOT IN (SELECT DISTINCT Start_Date 
                                              FROM Projects)) p_end_date
         ON p_start_date.rnk = p_end_date.rnk
 ORDER BY DATEDIFF(End_date, Start_Date), Start_Date;

 

'Growth ๐ŸŒณ > Practice ๐Ÿ’ป' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] 586. Customer Placing the Largest Number of Orders  (0) 2022.09.28
[Hacker Rank] Contest Leaderboard  (0) 2022.09.20
[Hacker Rank] Occupations  (0) 2022.09.20
[Hacker Rank] Draw The Triangle 2  (1) 2022.09.20
[Hacker Rank] Draw The Triangle 1  (0) 2022.09.13