부제:
자동차 평균 대여 기간 구하기, 레벨 4
1. 문제 링크:https://school.programmers.co.kr/learn/courses/30/lessons/157342
2. 정답 코드:
SELECT
car_id,
round(avg(datediff(end_date, start_date)+1), 1) as average_duration
from car_rental_company_rental_history
group by car_id
having average_duration >= 7
order by
average_duration desc,
car_id desc
3. 오류 상황:
SELECT
car_id,
round(avg(datediff(end_date, start_date)+1), 1) as average_duration
from car_rental_company_rental_history
having average_duration >= 7
order by
average_duration desc,
car_id desc
=> 그룹화 누락
4. 시도 방법: 그룹화 추가
5. 최종 문제 해결 방법: 자동차 ID를 기준으로 그룹화한다. GROUP BY 절에 CAR_ID 추가
'SQL 알고리즘 코드카타' 카테고리의 다른 글
| SQL 알고리즘 코드카타 21일차(1) 26.01.19(월) (0) | 2026.01.19 |
|---|---|
| SQL 알고리즘 코드카타 20일차(5) 26.01.18(일) (0) | 2026.01.18 |
| SQL 알고리즘 코드카타 20일차(3) 26.01.18(일) (1) | 2026.01.18 |
| SQL 알고리즘 코드카타 20일차(2) 26.01.18(일) (1) | 2026.01.18 |
| SQL 알고리즘 코드카타 20일차(1) 26.01.18(일) (0) | 2026.01.18 |