SQL 4주차
select restaurant_name,
price_per_plate*ratio_of_add "수수료"// 3.수수료와 평균값을 곱함
from
( select restaurant_name,
case when price_per_plate<5000 then 0.005
when price_per_plate between 5000 and 19999 then 0.01
when price_per_plate between 20000 and 29999 then 0.02
else 0.03 end ratio_of_add,
price_per_plate
from
( select restaurant_name, avg(price/quantity) price_per_plate
from food_orders
group by 1
) a //1. 테이블에서 레스토랑 이름과 그룹별 가격 평균을 구함
) b // 2. 조건별로 수수료 구함
****
(문제를 보고 미리 실습을 해보고 잊은 부분. 1)그룹별 평균 구하기: sum, avg, min 등 연산 함수는 group by랑 한 세트인 걸 잊지말자! 2)어째선지 중첩을 한 후로 값이 나오질 않고, 필드명만 연속으로 뜸.->중첩연산문 안에서 필드명을 한글로 썼기 때문 . 조심.
답은 3번을 중첩했는데 다른 방식으로 값을 구할 수 있는지 한 번 생각해봐야겠다. 3)between 대신 <20000, <30000으로 해도 값이 동일하다.
select restaurant_name,
sido,
case when avg_time<=20 then '<=20'
when avg_time>20 and avg_time <=30 then '20<x<=30'
when avg_time>30 then '>30' end time_segment
from
(
select restaurant_name,
substring(addr, 1, 2) sido,
avg(delivery_time) avg_time
from food_orders
group by 1, 2
)
****
(내가 생각하지 못한 부분