데이터 분석/SQL
Codility SQL 테스트 문제 답변 기록
쎄마비
2023. 1. 25. 21:49
728x90
반응형
단순 기록용으로 답안 남깁니다.
문제: 카드 지출액 + 월별 요금 - 특정 금액 이상 지출한 월의 요금
select sum(amount) - 5*(12-min(counted_month))
from transactions origin
cross join
(
select count(*) as counted_month
from (select date_trunc('month', date) as ym
from transactions where amount < 0
group by ym
having count(*) > 2 and sum(amount) <= -100
) counted ) counted2
문제2: 임금이 2배 이상인지 여부에 따라 직속 상사 또는 직속 상사의 직속 상사의 id 표시하기
select origin.id,
(case
when origin.salary * 2 <= second.salary
then second.id
else second.boss_id
end) as boss_id
from employees origin left join employees second
on origin.boss_id = second.id
order by origin.id
728x90
반응형