Write a solution to find the students who have shown improvement. A student is considered to have shown improvement if they meet both of these conditions:
Return the result table ordered by student_id
, subject
in ascending order.
The result format is in the following example.
exam_date
를 기준으로 가장 최신, 나중 값을 가져온다.
latest_score
를 뽑아올 때, LAST_VALUE()
를 쓰고 싶다면?LAST_VALUE()
의 경우, ORDER_BY
이후,
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
이 필요함.
이후에 WHERE
조건에 맞는 값들만 중복을 제거해 가져온다.
with ranked as (
select student_id,
subject,
first_value(score) over
(partition by student_id, subject order by exam_date) as first_score,
first_value(score) over
(partition by student_id, subject order by exam_date desc) as latest_score
from scores
)
select distinct *
from ranked
where first_score < latest_score
order by student_id, subject;
최근 배웠던
CTE
,FIRST_VALUE
,LAST_VALUE
를 모두 접할 수 있던 문제.