내가 한 풀이 과정은 이러했다. 하지만 결과 화면과 달라서 확인해보니, group by 1,2를 해야했던 것.
지역별로 배달시간을 측정한다는게 이해가 가지 않아서 챗gpt에게 물어봤다.
결론은 group by 1이라고 작성했을 때, 음식점 이름이 같으면 지역에 관계없이 모든 데이터가 하나의 그룹으로 처리된다는 것이다.
제대로 작성해주면 올바른 결과가 나오게 된다.
우선 음식 타입별로 음식점수와 주문수를 구한다.
음식점 이름이 같으면 하나로 계산할 수 있도록 distinct를 붙이고, 주문수를 order_id가 아닌 quantity를 합산한 결과로 하면 정답이다.
select cuisine_type, 음식점수, 주문수, case when 음식점수>=5 and 주문수>=30 then 0.005
when 음식점수>=5 and 주문수<30 then 0.008
when 음식점수<=5 and 주문수>=30 then 0.01
when 음식점수<=5 and 주문수<30 then 0.02 end "수수료"
from
(select cuisine_type , count(distinct restaurant_name) "음식점수", sum(quantity) "주문수"
from food_orders
group by cuisine_type)a
우선 음식점별로 총 주문 금액과 주문수를 계산한다.
이번엔 한 번에 성공!
select restaurant_name, 총주문금액, 주문수, case when 주문수<=5 then 0.1
when 주문수>=15 and 총주문금액>=300000 then 0.005
else 0.01 end 할인율
from
(select restaurant_name, sum(price)"총주문금액", sum(quantity) "주문수"
from food_orders
group by restaurant_name)a