본문 바로가기
알고리즘

[프로그래머스] LV.5 상품을 구매한 회원 비율 구하기 (MySQL)

by kigo23 2023. 4. 21.
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/131534?language=mysql

SELECT 
    YEAR(B.SALES_DATE) AS YEAR, 
    MONTH(B.SALES_DATE) AS MONTH, 
    COUNT(DISTINCT(B.USER_ID)) AS PUCHASED_USERS,
    ROUND(
    	COUNT(DISTINCT(B.USER_ID)) / (
            SELECT COUNT(1) 
            FROM USER_INFO 
            WHERE JOINED <= '2021-12-31'
        )
    , 1) AS PUCHASED_RATIO
FROM USER_INFO A
JOIN ONLINE_SALE  B ON A.USER_ID = B.USER_ID
WHERE A.JOINED BETWEEN '2021-01-01' AND '2021-12-31'
GROUP BY YEAR(B.SALES_DATE), MONTH(B.SALES_DATE)