본문 바로가기
TIL - 프로그래밍/SQL

[프로그래머스] 상품을 구매한 회원 비율 구하기 - MySQL

by chaemj97 2023. 3. 2.
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/131534

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


    •  풀이
      • 2021년 가입한 사람 수
        • SELECT COUNT(*) FROM USER_INFO WHERE YEAR(JOINED)=2021
      • 2021년에 가입한 사람 중 구매한 사람 수
        • COUNT(DISTINCT USER_ID)
        • COUNT(*)을 사용할 경우 여러번 구매한 사람이 중복 카운트 됨
      • 소수 2번째자리에서 반올림
        • ROUNT(반올림하고 싶은 숫자,1)
        • 2번째 인자 생략시 첫번째자리에서 반올림
SELECT year(sales_date) as year,month(sales_date) as month, count(distinct user_id) as puchased_users,
# 구매 비율
round(count(distinct user_id)/(select count(*) from user_info where year(joined)=2021),1) as puchased_ratio
from online_sale
where user_id in (select user_id from user_info where year(joined) = 2021)
group by year(sales_date),month(sales_date)
order by year,month
728x90
반응형

댓글