조건문과 Subquery 를 결합하여 user segmentation 과 연산해보자
음식점의 평균 단가별 segmentation 을 진행하고 그룹에 따라 수수료 연산
select restaurant_name,
price_per_plate*ratio_of_add as "수수료"
from
(
select restaurant_name, price_per_plate,
case when price_per_plate < 5000 then 0.05
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
from
(
select restaurant_name,
avg(price/quantity) as price_per_plate
from food_orders
group by restaurant_name
) as a
) as b
음식점의 지역과 평균 배달시간으로 segmentation 진행
select restaurant_name, sido, avg_delivery_time,
case when avg_delivery_time <= 20 then '<=20'
when avg_delivery_time > 20 and avg_delivery_time <= 30 then '20<x<=30'
else '>30' end avg_delivery_time_segment
from
(
select restaurant_name,
substr(addr, 1, 2) as sido,
avg(delivery_time) as avg_delivery_time
from food_orders
group by restaurant_name, sido
) as a
하나의 쿼리문에서 수행하기 어려운 복잡한 연산을 Subquery로 실행
select cuisine_type,
total_quantity, count_res,
case when count_res >= 5 and total_quantity >= 30 then 0.005
when count_res >= 5 and total_quantity < 30 then 0.008
when count_res < 5 and total_quantity >= 30 then 0.01
when count_res < 5 and total_quantity < 30 then 0.02
end as rate
from
(
select cuisine_type,
sum(quantity) as total_quantity,
count(distinct restaurant_name) as count_res
from food_orders
group by cuisine_type
) as a;select restaurant_name,
total_quantity, total_price,
case when total_quantity <= 5 then 0.1
when total_quantity > 15 and total_price >= 300000 then 0.05
else 0.1 end discount_rate
from
(
select restaurant_name,
sum(quantity) as total_quantity,
sum(price) as total_price
from food_orders
group by restaurant_name
) as a;JOIN의 기본 원리와 종류
JOIN의 기본 구조
- LEFT JOIN -
select 조회할 컬럼
from 테이블1 a left join 테이블2 b on a.공통컬럼명 = b.공통컬럼명;
- INNER JOIN -
select 조회할 컬럼
from 테이블1 a inner join 테이블2 b on a.공통컬럼명 = b.공통컬럼명;
공통컬럼은 묶어 주기 위한 '공통 값' 이기 때문에 두 테이블의 컬럼명은 달라도 괜찮다.
예를 들어 주문정보에는 '고객ID', 고객정보에는 '고객아이디'라고 컬럼명이 되어 있다면주문정보.고객ID = 고객정보.고객아이디와 같이 묶어줄 수 있다.
JOIN의 사용예시시
select a.order_id,
a.customer_id,
a.restaurant_name,
a.price,
b.name,
b.age,
b.gender
from food_orders a left join customers b on
a.customer_id=b.customer_id
한국 음식의 주문별 결제 수단과 수수료율을 조회하기
select a.order_id,
a.restaurant_name,
a.price,
b.pay_type,
b.vat
from food_orders as a left join payments as b
on a.order_id = b.order_id
where cuisine_type='korean';
고객의 주문 식당 조회하기
select distinct b.name,
b.age,
b.gender,
a.restaurant_name
from food_orders as a left join customers as b
on a.customer_id = b.customer_id
order by b.name;
select f.order_id,
f.restaurant_name,
f.price,
p.vat,
f.price * p.vat as vat2
from food_orders as f inner join payments as p
on f.order_id = p.order_id;select cuisine_type,
sum(price) as price,
sum(price*discount_rate) as discounted_price
from
(
select f.cuisine_type,
f.price,
c.age,
(c.age-50)*0.005 as discount_rate
from food_orders as f left join customers as c
on f.customer_id = c.customer_id
where c.age >= 50
) as a
group by cuisine_type
order by discounted_price desc;