SELECT car_id, max(case when '2022-10-16' between start_date and end_date then '대여중'
else '대여 가능'
end) AVAILABILITY
from car_rental_company_rental_history
group by 1
order by car_id desc
group by 하고 같은 car_id 중 '대여중'이 있는 car_id이면 '대여중'이 출력되도록 하려고 했다.
MAX 함수
를 사용했다.
그룹 내에서 가장 우선순위가 높은 값을 선택한다.
→ 그룹 내에서 가장 큰(사전순으로 뒤에 오는) 값을 선택
SELECT car_id, if(car_id in (select car_id
from car_rental_company_rental_history
where '2022-10-16' between start_date and end_date),'대여중','대여 가능') AVAILABILITY
from car_rental_company_rental_history
group by 1
order by car_id desc
car_id가 한 번이라도 대여중이면 전체가 ‘대여중’으로 통일됨.