링크
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
문제

정답
select min(start_date),
max(end_date)
from (
select start_date,
end_date,
date_add(end_date, interval - row_number() over(order by end_date) day) as dateadd
from projects
) sub1
group by sub1.dateadd
order by count(sub1.dateadd), min(start_date)
새롭게 이해한 내용
- 연속된 날짜를 그룹화하기 위해 date_add와 row_number를 활용한다
- 연속된 날짜라면 row_number의 결과로 나타난 날짜를 뺄 경우에 동일한 결과가 추출된다.
- 이후 그룹화하면 연속된 기간을 알 수 있다
'[업무 지식] > MySQL' 카테고리의 다른 글
| [recursive] Print Prime Numbers (0) | 2024.12.13 |
|---|---|
| [recursive] Draw The Triangle (0) | 2024.12.12 |
| [MAX() over()] Challenges (0) | 2024.11.29 |
| [row index] Weather Observation Station 20 (0) | 2024.11.25 |
| [left, right] Weather Observation Station 8 (0) | 2024.11.19 |