5/23 TIL

정희린·2025년 5월 23일

QCC ~
만점은 당연히 못받았고 ㅎ 이번에 3번이 너무 어려웠다..
두 문제를 맞았다..! Wow

제가 10명 안에 들다니.. 영광스럽다
이럴 줄 알았으면 닉네임 삼성라이온즈로 밀어붙일걸 ㅎ
솔직히 2번은 정답 아니라 생각했는데.. 어케 맞췄지?

QCC 1번

상위 3명의 성별, 이름, 점수 반환

select gender,
       name,
       score
from
(
select rank () over(partition by GENDER order by SCORE desc, age desc) as ranking,
      gender,
      score,
      age,
      name
from students
  ) a
where ranking<=3
order by 1, ranking

window 함수 쓰면 아주 쉽게 풀 수 있는 문제였다 ! 혹시 몰라 order by 를 추가했는데, 튜터님 풀이를 보니 order by는 생략해도 되었다

QCC 2번

DUE(미결제 금액), PAID(결제 완료) 구하기

select title,
       coalesce(round(sum(case when bo.paid_date is null then boi.line_total
       else 0 end), 0), 0) as due,
       coalesce(round(sum(case when bo.paid_date is not null then boi.line_total
       else 0 end), 0), 0) as paid
from books b left join book_order_items boi on b.id=boi.book_id
left join book_orders bo on boi.order_id=bo.id
group by title
order by 1

일단 문제가 너무 길었고.. join 키 찾는 것도 꽤 걸렸다
길었던 문제에 비해 단순하게 푼 것 같아 틀렸다 생각했는데 생각보다 간단한 문제였음..
늘 풀이를 보며 case when 문을 잘 활용해야 한다는걸 깨달았는데 이번엔 잘 쓴 것 같아 뿌듯하다
마지막에 paid를 order by 해보니 null값이 있었는데 coalesce 조건 추가를 해서 0으로 변환까지 완료

QCC 3번

Cohort 정의, 월 단위로 재구매한 고객 수 집계

#튜터님 답
with cohort as (
  select user_id
    , min(order_date) first_order_date
    , min(date_format(order_date, '%Y-%m')) first_order_month 
  from orders
  group by 1
) 
select 
    c.first_order_month
    , timestampdiff(month, c.first_order_date, o.order_date) months_after
    , count(distinct o.user_id) active_user
from cohort c 
join orders o 
on c.user_id = o.user_id
where timestampdiff(month, c.first_order_date, o.order_date) between 0 and 6 
and c.first_order_month between '2023-01' and '2023-06'
group by 1, 2
order by 1, 2

해설 제대로 못들어서 다시 들어봐야할듯 ㅜ ㅜ

오늘 크롤링 강의도 제대로 안들었다 ㅎ.ㅎ.. 주말에 sqld도 해야되고.. 오늘은 일단 쉬자..

profile
스포츠 광인

0개의 댓글