Programing/데이터베이스(MySQL)

[프로그래머스] MySQL Lv.2 (정답률 80% 이하)

hye3193 2023. 10. 31. 17:16

자동차 평균 대여 기간 구하기

SELECT car_id, average_duration
from (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) as history
where average_duration >= 7
order by average_duration desc, car_id desc


조건에 부합하는 중고거래 상태 조회하기

SELECT board_id,
    writer_id,
    title,
    price,
    case
        when status = 'SALE' then '판매중'
        when status = 'RESERVED' then '예약중'
        when status = 'DONE' then '거래완료'
    end as status
from used_goods_board
where created_date = '2022-10-05'
order by board_id desc


재구매가 일어난 상품과 회원 리스트 구하기

SELECT user_id, product_id
from (select user_id, product_id, count(*) as c
     from online_sale
     group by user_id, product_id) as repurchase
where c >= 2
order by user_id asc, product_id desc