๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋ HackerRank ์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
https://www.hackerrank.com/challenges/sql-projects/problem
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 |