[Programmers] SQL - 상품을 구매한 회원 비율 구하기

DMIS·2022년 10월 29일
0

SQL

목록 보기
43/48
post-thumbnail

문제

풀이

2021년에 가입한 전체 회원 수를 미리 구해놓고 문제에서 요구하는대로 년, 월별 구매한 유저 수를 구해 비율까지 구해주면 된다.

with t100 as
(select
    count(user_id) as cntall
from user_info
where year(joined) = 2021)
select
    year(sales_date) as year,
    month(sales_date) as month,
    count(distinct user_id) as puchased_users,
    round(count(distinct user_id) / t100.cntall, 1) as puchased_ratio
from online_sale, t100
where user_id in (select
                    user_id
                 from user_info
                 where year(joined) = 2021)
group by 1, 2
order by 1, 2
profile
Data + Math

0개의 댓글