[실습] 서울 지역의 음식 타입별 평균 음식 주문금액 구하기 (출력 : ‘서울’, ‘타입’, ‘평균 금액’)
select SUBSTR(addr, 1, 2) as "서울", cuisine_type "타입", AVG(price) "평균 금액"
from food_orders fo
where fo.addr like '서울%'
group by 1,2 ;
[실습] 이메일 도메인별 고객 수와 평균 연령 구하기
select SUBSTR(email, 10), COUNT(*), AVG(age)
from customers c
group by 1;
[실습] ‘[지역(시도)] 음식점이름 (음식종류)’ 컬럼을 만들고, 총 주문건수 구하기
select CONCAT('[',SUBSTR(addr,1,2),'] ', restaurant_name, ' (', cuisine_type,')'), COUNT(*)
from food_orders fo
group by 1;
if(조건, 조건을 충족할 때, 조건을 충족하지 못할 때) select restaurant_name,
cuisine_type "원래 음식 타입",
if(cuisine_type='Korean', '한식', '기타') "음식 타입"
from food_ordersselect addr "원래 주소",
if(addr like '%평택군%', replace(addr, '문곡리', '문가리'), addr) "바뀐 주소"
from food_orders
where addr like '%문곡리%'select substring(if(email like '%gmail%', replace(email, 'gmail', '@gmail'), email), 10) "이메일 도메인",
count(customer_id) "고객 수",
avg(age) "평균 연령"
from customers
group by 1case when 조건1 then 값(수식)1
when 조건2 then 값(수식)2
else 값(수식)3
end select restaurant_name,
cuisine_type AS "원래 음식 타입",
case when (cuisine_type='Korean') then '한식'
when (cuisine_type in (Japanese, Chienese) then '아시아'
else '기타'
end as " 음식 타입"
from food_orders select order_id,
price,
quantity,
case when quantity=1 then price
when quantity>=2 then price/quantity end "음식 단가"
from food_orders select order_id,
price,
quantity,
if(quantity = 1, price, price/quantity) "음식 단가"
from food_orders select restaurant_name,
addr,
case when addr like '%경기도%' then '경기도'
when addr like '%특별%' or addr like '%광역%' then substring(addr, 1, 5)
else substring(addr, 1, 2) end "변경된 주소"
from food_orders 한국 음식, 아시아 음식, 미국 음식, 유럽 음식10대 여성, 10대 남성, 20대 여성, 20대 남성 등[실습] 10세 이상, 30세 미만의 고객의 나이와 성별로 그룹 나누기 (이름도 같이 출력)
select name,
case when gender = 'male' and (age BETWEEN 10 and 19) then '10대 남성'
when gender = 'female' and (age BETWEEN 10 and 19) then '10대 여성'
when gender = 'male' and (age BETWEEN 20 and 29) then '20대 남성'
when gender = 'female' and (age BETWEEN 20 and 29) then '20대 여성'
end as category
from customers c
where age BETWEEN 10 and 29;
[실습] 음식 단가, 음식 종류 별로 음식점 그룹 나누기
음식 종류
가격
select cuisine_type,
price,
case when cuisine_type = 'Korean' and price/quantity < 5000 then 'Cheap Korean food'
when cuisine_type = 'Korean' and (price/quantity BETWEEN 5000 and 15000) then 'Korean food'
when cuisine_type = 'Korean' and price/quantity > 15000 then 'Expensive Korean food'
when cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and price/quantity < 5000 then 'Cheap Asian food'
when cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and (price/quantity BETWEEN 5000 and 15000) then 'Asian food'
when cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and price/quantity > 15000 then 'Expensive Asian food'
when cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and price/quantity < 5000 then 'Cheap Western food'
when cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and (price/quantity BETWEEN 5000 and 15000) then 'Western food'
when cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and price/quantity > 15000 then 'Expensive Western food' end as category
from food_orders fo ;
[실습] 지역과 배달시간을 기반으로 배달수수료 구하기 (식당 이름, 주문 번호 함께 출력)
select restaurant_name,
order_id,
case when addr like '서울%' and delivery_time < 25 then price * 1.1
when addr LIKE '서울%' and (delivery_time BETWEEN 25 and 30) then price * 1.1 * 1.05
when addr LIKE '서울%' and delivery_time > 30 then price * 1.1 * 1.1
when addr not LIKE '서울%' and delivery_time < 25 then price
when addr not LIKE '서울%' and (delivery_time BETWEEN 25 and 30) then price * 1.05
when addr not LIKE '서울%' and delivery_time > 30 then price * 1.1
end as "수수료"
from food_orders fo ;
⇒ 답안지
select restaurant_name,
order_id,
addr,
delivery_time,
price,
case when delivery_time between 26 and 30 then price * 0.05 * IF(addr like '%서울%', 1.1, 1)
WHEN delivery_time > 30 then price * 0.1 * IF(addr like "%서울%", 1.1, 1)
else 0 END as "수수료"
from food_orders fo ;
[실습] 주문 시기와 음식 수를 기반으로 배달할증료 구하기
select day_of_the_week,
quantity,
case when day_of_the_week = 'Weekend' then 3000 * if(quantity > 3, 1.2, 1)
else 3500 * if(quantity > 3, 1.2, 1)
end as "배달할증료"
from food_orders fo ;
--숫자로 변경
cast(if(rating='Not given', '1', rating) as decimal)
--문자로 변경
concat(restaurant_name, '-', cast(order_id as char))HW. 3주차 숙제 해결
💡다음의 조건으로 배달시간이 늦었는지 판단하는 값을 만들어주세요.
주중 : 25분 이상
주말 : 30분 이상
select day_of_the_week,
delivery_time,
case when day_of_the_week = 'Weekday' and fo.delivery_time >= 25 then "LATE"
when day_of_the_week = 'Weekend' and fo.delivery_time >= 30 then "LATE"
else "Okay" end as "지연여부"
from food_orders fo ;
select SUBSTR(addr, 1, 2) as "서울", cuisine_type "타입", AVG(price) "평균 금액"
from food_orders fo
where fo.addr like '서울%'
group by 1,2 ;select SUBSTR(email, 10), COUNT(*), AVG(age)
from customers c
group by 1;Case when 1 then A
when 2 then B
else 3
end
/* Case문은 기본적으로 When 이후 괄호 사용 X
여기서 아래처럼 And 사용 이후 Between A and B 와 같이
And 가 두 번 사용되어야 할 경우만 괄호로 구분*/select name,
case when gender = 'male' and (age BETWEEN 10 and 19) then '10대 남성'
when gender = 'female' and (age BETWEEN 10 and 19) then '10대 여성'
when gender = 'male' and (age BETWEEN 20 and 29) then '20대 남성'
when gender = 'female' and (age BETWEEN 20 and 29) then '20대 여성'
end as category
from customers c
where age BETWEEN 10 and 29;select cuisine_type,
price,
case when cuisine_type = 'Korean' and price/quantity < 5000 then 'Cheap Korean food'
when cuisine_type = 'Korean' and (price/quantity BETWEEN 5000 and 15000) then 'Korean food'
when cuisine_type = 'Korean' and price/quantity > 15000 then 'Expensive Korean food'
when cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and price/quantity < 5000 then 'Cheap Asian food'
when cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and (price/quantity BETWEEN 5000 and 15000) then 'Asian food'
when cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and price/quantity > 15000 then 'Expensive Asian food'
when cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and price/quantity < 5000 then 'Cheap Western food'
when cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and (price/quantity BETWEEN 5000 and 15000) then 'Western food'
when cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') and price/quantity > 15000 then 'Expensive Western food' end as category
from food_orders fo ;select restaurant_name,
order_id,
addr,
delivery_time,
price,
case when delivery_time between 26 and 30 then price * 0.05 * IF(addr like '%서울%', 1.1, 1)
WHEN delivery_time > 30 then price * 0.1 * IF(addr like "%서울%", 1.1, 1)
else 0 END as "수수료"
from food_orders fo ;