본문 바로가기
HackerRank - MS SQL Server

[해커랭크/MS SQL] SQL Project Planning

by nomeleon 2022. 4. 8.
반응형

 

문제 링크입니다!

 

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

 

문제

 

 

=> 각 태스크마다 시작일자와 종료일자가 저장되어 있는 테이블이 존재하고 두 일자의 차이는 무조건 1일이다.

만약 종료일자가 다른 태스크의 시작일자와 동일하다면 해당 태스크들은 모두 동일한 프로젝트의 일부라고 할 수 있다. 

이 때 테이블에 저장되어 있는 모든 프로젝트를 구하는데, 프로젝트 기간 오름차순, 기간이 동일할 경우 시작일자로 정렬해라.


 

풀이

select p1.start_date, p2.end_date
from (select start_date, row_number() over (order by start_date) as rn
    from projects
    where start_date not in (select end_date
                          from projects)
    ) as p1
    join
    (select end_date, row_number() over (order by end_date) as rn
        from projects
        where end_date not in (select start_date
                            from projects)
        ) as p2 on p1.rn = p2.rn
order by datediff(day,p1.start_date,p2.end_date),   
p1.start_date

댓글