https://school.programmers.co.kr/learn/courses/30/lessons/157339
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
조건을 살펴보면
1. 자동차 종류가 SUV, 세단
2. 2022-11-01~ 2022-11-30 사이의 대여 기록이 없고
3. 30일 대여 금액이 50만원 이상 200만원 미만
위 조건을 토대로
1. 2022-11-01~ 2022-11-30 사이에 대여 기록이 존재하는 car_id 목록을 가진 테이블 추출
2. 30일 이상 대여 시, 할인율 계산을 위해
car_rental_company_car 과 car_rental_company_discount_plan 테이블을 inner join
조인 조건은 car_type 이 일치하고 duration_type 이 '30일 이상'
3. where 조건절에서 1번 테이블의 목록에 있는 car_id 는 11-01 ~ 11-30 사이에 대여가 불가능하므로 not in 으로 제외
car_type 이 세단, SUV 이고, 대여 가격이 50만원 이상 200만원 미만일 때
4. car_id, car_type, fee(대여 가격 계산 후 floor 로 소수점 제거) 출력
5. order by 로 fee, car_type, car_id 각각 내림차순, 오름차순, 내림차순으로 정렬
select car.car_id, car.car_type, floor(car.daily_fee * ((100-discount.discount_rate)/100) * 30) as fee
from car_rental_company_car as car inner join car_rental_company_discount_plan as discount
on car.car_type = discount.car_type and
discount.duration_type = '30일 이상'
where
car_id not in
(
select distinct car_id
from car_rental_company_rental_history
where end_date > '2022-11-01' and start_date < '2022-12-01'
order by car_id
)
and (car.car_type = 'SUV' or car.car_type = '세단')
and ((car.daily_fee * 30 * (1-(discount.discount_rate)/100)) between 500000 and 2000000)
order by car.daily_fee * 30 * (1-(discount.discount_rate)/100) desc, car.car_type asc, car.car_id desc
'코딩테스트 > 문제풀이' 카테고리의 다른 글
[프로그래머스] 저자 별 카테고리 별 매출액 집계하기 SQL (0) | 2023.10.04 |
---|---|
[프로그래머스] 가격대 별 상품 개수 구하기 SQL (0) | 2023.10.03 |
[프로그래머스] 자동차 대여 기록 별 대여 금액 구하기 SQL (0) | 2023.10.03 |
[프로그래머스] 입양 시각 구하기(2) SQL (1) | 2023.10.02 |
[프로그래머스] 년, 월, 성별 별 상품 구매 회원 수 구하기 SQL (0) | 2023.10.01 |