코딩테스트/MySQL
[프로그래머스 Level 3] 대여 횟수가 많은 자동차들의 월별 대여 횟수 구하기(MySQL) .Feat 날짜 비교
최-코드
2024. 8. 16. 10:16
https://school.programmers.co.kr/learn/courses/30/lessons/151139
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
car_id별 대여횟수를 구해서 8,9,10월에 총 대여 횟수가 5이상인지 먼저 파악한 이후에 월+car_id별 대여횟수를 따로 구해줘야한다.
날짜 비교의 경우 date타입을 date_format 함수를 통해 문자열로 변경한 후 문자열 비교로 해주면 된다.
select month(w.start_date) as month, w.car_id, count(w.car_id) as records
from car_rental_company_rental_history as w natural join
(
select car_id
from car_rental_company_rental_history
where date(start_date) between '2022-08-01' and '2022-10-31'
group by car_id
having count(car_id)>=5
) as q
where date(start_date) between '2022-08-01' and '2022-10-31'
group by month(start_date), car_id
order by month(start_date), car_id desc