https://school.programmers.co.kr/learn/courses/30/lessons/131534
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
문제를 잘못 파악해서 꽤나 오래 걸린 문제
2021년에 가입한 사람 중 물건을 구매한 날짜의 년/월을 기준으로 group by를 해야 한다
분모에 해당하는 2021년에 가입한 사람 전체수를 어떻게 처리해줘야 할지 잘 떠오르지 않아서
테이블을 하나 더 추가한 후에 group by 할 때에 이 컬럼도 추가해줬다..
but 다른 사람들 풀이를 찾아보니 select 절에서 서브쿼리로 한번에 처리해주는 것을 보고 수정해보았다
[1차 풀이]
select to_char(t2.sales_date, 'YYYY') as year,
to_number(to_char(t2.sales_date, 'MM')) as month,
count(distinct(t2.user_id)) as purchased_users,
round(count(distinct(t2.user_id))/t3.total,1) as purchased_ratio
from (select count(user_id) as total
from user_info
where to_char(joined, 'yyyy')=2021 ) t3,
user_info t1 inner join online_sale t2
on t1.user_id=t2.user_id
where to_char(t1.joined, 'yyyy')=2021
group by to_char(t2.sales_date, 'YYYY'), to_number(to_char(t2.sales_date, 'MM')),t3.total
order by 1, 2
[2차 풀이]
select to_char(t2.sales_date, 'YYYY') as year,
to_number(to_char(t2.sales_date, 'MM')) as month,
count(distinct(t2.user_id)) as purchased_users,
round(count(distinct(t2.user_id))/(select count(user_id)
from user_info
where to_char(joined, 'yyyy')=2021),1) as purchased_ratio
from user_info t1 inner join online_sale t2
on t1.user_id=t2.user_id
where to_char(t1.joined, 'yyyy')=2021
group by to_char(t2.sales_date, 'YYYY'), to_number(to_char(t2.sales_date, 'MM'))
order by 1, 2
'스터디 > Algorithm' 카테고리의 다른 글
[프로그래머스스쿨] 입양 시각 구하기(2) oracle (1) | 2024.09.20 |
---|---|
[프로그래머스스쿨] 특정 기간동안 대여 가능한 자동차들의 대여비용 구하기 oracle (1) | 2024.09.20 |
[백준] 암벽 등반 2412 java -bfs (1) | 2024.09.16 |
[백준] 합이 0 3151 java - 투포인터 (2) | 2024.09.15 |
[백준] 수들의 합2 2003 java - 투포인터 (1) | 2024.09.10 |