코드카타는 모르는 거나 틀린 거를 기록할 예정이다.
코드들을 복기하면서 정리할 거다.
# 첫 코드
SELECT date_format(datetime,'%H') hours,count(animal_id) cnt
from animal_outs group by hours order by hours
도저히 0시부터 23시까지 조회를 못하겠더라.
그래서 결국 해설을 봤는데 처음 보는 친구가 있었다.
RECURSIVE라고 하는
WITH RECURSIVE rec AS (SELECT 0 AS
UNION ALL
SELECT n + 1 AS hours
FROM rec
WHERE n < 24)
SELECT hours,count(animal_id) cnt
from rec join (SELECT date_format(datetime,'%H') hours,count(animal_id) cnt
from rec join animal_outs)
using(hours) group by hours order by hours
그래서 대충 해설 보고 했는데 실수를 했다.
WITH RECURSIVE CTE AS(
SELECT 0 AS NUM
UNION ALL
SELECT NUM +1
FROM CTE
WHERE NUM < 23)
select num,count(animal_id) from cte left join animal_outs
on num=hour(datetime) group by num order by num
솔직히 아직 잘 모르겠다.
다음에 복습 때 자세히 알아보겠다.

with a as (SELECT car_id,car_type,daily_fee,start_date,end_date
from CAR_RENTAL_COMPANY_CAR join CAR_RENTAL_COMPANY_RENTAL_HISTORY using(car_id))
select car_id,car_type,daily_fee*discount_rate fee from a
join CAR_RENTAL_COMPANY_DISCOUNT_PLAN b using(car_type)
where (car_type='세단' or car_type='suv') and duration_type='30일 이상'
having fee >= 500000 and fee < 2000000
order by fee desc, car_type, car_id
fee를 구할 때 discount_rate는 할인율이라서 무작정 곱하면 안됐다.
with a as (SELECT car_id,car_type,daily_fee,start_date,end_date
from CAR_RENTAL_COMPANY_CAR join CAR_RENTAL_COMPANY_RENTAL_HISTORY using(car_id)
where end_date<'2022-10-31')
select car_id,car_type,round((daily_fee*30)-(daily_fee*30*discount_rate/100)) fee from a
join CAR_RENTAL_COMPANY_DISCOUNT_PLAN b using(car_type)
where (car_type='세단' or car_type='suv') and duration_type='30일 이상'
group by car_id
having fee >= 500000 and fee < 2000000
order by fee desc, car_type, car_id
수정을 했지만 car_id가 중복되는 게 있어서 a 테이블을 고쳐야 했다.
with a as (select car_id,car_type,daily_fee*30 monthly_fee,max(end_date) n_date
from car_rental_company_car left join car_rental_company_rental_history using(car_id)
group by car_id having n_date<'2022-10-31')
select car_id,car_type,round(monthly_fee-(monthly_fee*discount_rate/100)) fee
from a join car_rental_company_discount_plan b using(car_type)
where (car_type='세단' or car_type='SUV') and duration_type='30일 이상'
having fee between 500000 and 1999999
group by를 마지막에 하는 게 아니라 a 테이블에서부터 했어야 했다.

마지막 문제는 정말 코드 쓰는 게 길었다.
내가 비효율적이었나 문득 생각을 하게 됐다.
with a as (SELECT history_id,daily_fee,datediff(end_date,start_date)+1 diff_date
from CAR_RENTAL_COMPANY_CAR join CAR_RENTAL_COMPANY_RENTAL_HISTORY
using(car_id) where car_type='트럭')
select history_id,round(og_fee-og_fee*n_dis/100) fee
from
(
select *,if(discount_rate is null,0,discount_rate) n_dis
from
(select history_id,daily_fee*diff_date og_fee,
case when diff_date > 7 then '7일 미만'
when diff_date > 30 then '7일 이상'
when diff_date > 90 then '30일 이상'
else '90일 이상' end duration from a) ab
left join (select duration_type,discount_rate
from CAR_RENTAL_COMPANY_DISCOUNT_PLAN where car_type='트럭') b
on ab.duration=b.duration_type
) c
order by fee desc,history_id desc
진짜 영혼을 갈아서 하나하나 코드를 짜서 오류가 났을 때 막막했다.
그래서 실수를 한 게 있나 찾아보았다.
with a as (SELECT history_id,daily_fee,datediff(end_date,start_date)+1 diff_date
from CAR_RENTAL_COMPANY_CAR
join CAR_RENTAL_COMPANY_RENTAL_HISTORY using(car_id) where car_type='트럭')
select history_id,round(og_fee-og_fee*n_dis/100) fee
from
(
select *,if(discount_rate is null,0,discount_rate) n_dis
from
(select history_id,daily_fee*diff_date og_fee,
case when diff_date < 7 then '7일 미만'
when diff_date < 30 then '7일 이상'
when diff_date < 90 then '30일 이상'
else '90일 이상' end duration from a) ab
left join (select duration_type,discount_rate
from CAR_RENTAL_COMPANY_DISCOUNT_PLAN where car_type='트럭')
b on ab.duration=b.duration_type
) c
order by fee desc,history_id desc
하하하 ab 서브쿼리에 있는 case when 절에 범위 실수가 있었다.
사소했지만 코딩테스트였다면 틀렸을 것이다.
실수하지 말자.
오늘 파이썬 공부하면서 이해 안되는게 있었다.
'filter 조건을 따라 추출 map 함수대로 만들어 주는 것'
내일 알아보자