주문 테이블에서
주문수량량 1건의 주문건의
음식 가격의 평균을 음식종류별로 조회하여
음식 가격이 높은 순서대로 정렬하기
select cuisine_type "음식종류" , avg(price) "평균가격"
from food_orders fo
where quantity = 1
group by cuisine_type
order by avg(price) desc
SELECT CONCAT('[',SUBSTR(addr, 1, 2),"]",restaurant_name,'-',cuisine_type) "음식점",
count(1) '총 주문건수'
from food_orders fo
group by 1
#addr = 지역(시도) restauant_name = 음식점 이름, cuisine_type = 음식종류 quantity=총주문건수
SELECT case when cuisine_type='Korea' then '한식'
when cuisine_type in ('Japannse', 'Chinese') then '아시아'
else '기타' end "음식타입",
cuisine_type
from food_orders fo
select order_id,
price,
quantity,
case when quantity=1 then price
when quantity>=2 then price/quantity end "음식 단가"
from food_orders
#10세이상, 30세 미만의 고객의 나이와 성별로 그룹 나우기
SELECT case when (age between 10 and 19) and gender ='male' then '10대 남성'
when (age BETWEEN 10 and 19)and gender='female' then '10대 여성'
when (age BETWEEN 20 and 29)and gender='male' then '20대 남성'
when (age BETWEEN 20 and 29)and gender='female' then '20대 여성' end "고객분류",
name,
age,
gender
from customers
WHERE age between 10 and 29
#음식단가, 음식 종류별로 음식점 그룹 나누기
select restaurant_name,
price/quantity "단가",
cuisine_type,
order_id,
case when (price/quantity <5000) and cuisine_type='Korean' then '한식1'
when (price/quantity between 5000 and 15000) and cuisine_type='Korean' then '한식2'
when (price/quantity > 15000) and cuisine_type='Korean' then '한식3'
when (price/quantity <5000) and cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '아시아식1'
when (price/quantity between 5000 and 15000) and cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '아시아식2'
when (price/quantity > 15000) and cuisine_type in ('Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '아시아식3'
when (price/quantity <5000) and cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '기타1'
when (price/quantity between 5000 and 15000) and cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '기타2'
when (price/quantity > 15000) and cuisine_type not in ('Korean', 'Japanese', 'Chinese', 'Thai', 'Vietnamese', 'Indian') then '기타3' end "식당 그룹"
from food_orders
order by 5 desc
# 25분 초과 5% 30분 초과 10% 음식가격
# 서울이면 1.1%
SELECT case when delivery_time > 30 then price*0.1*if(addr like '%서울%', 1.1 , 1)
when delivery_time > 25 then price*0.05*if(addr like '%서울%', 1.1 , 1)
else 0 end "수수료",
restaurant_name ,
order_id ,
price,
delivery_time,
addr
from food_orders fo
숙제
#주중: 25분 이상
#주말: 30분 이상
SELECT order_id ,
restaurant_name ,
day_of_the_week ,
delivery_time,
case when day_of_the_week ='Weekday' and delivery_time >=25 then 'Late'
when day_of_the_week ='Weekend' and delivery_time >=30 then 'Late'
else 'On-time' end "지연여부"
from food_orders fo
'SQL' 카테고리의 다른 글
SQL 1주차 라이브세션 과제 (1~3회) (작성중) (0) | 2024.06.27 |
---|---|
SQL 피벗 테이블 ( 드디어!!) (0) | 2024.05.31 |
SQL 5주차 강의 내용 (0) | 2024.05.31 |
SQL 2주 (1) | 2024.05.23 |
SQL 1주차 (0) | 2024.05.22 |