
4주차 4회 실습 2번을 좀 더 해보고 있는데,
할인 금액까지 구하고, 실제 소비자 가격을 더 한번 구하려고 total_sales-discount_ratio 뺄셈 연산을 했는데,
에러가 나서 구글링 해보니 abs 사용하라고 되어 있어서 아래처럼 썼더니 에러가 뜨더라구요.ㅠ
왜 그런걸까요? abs는 무조건 from이 별도로(?) 있어야 하는건지..!
참고로 이후에 뭔가 그룹을 해줘야할까 싶어서 b 그룹을 만들고,
아래와 정상으로 나오는건 확인했는데, 이유가 궁금합니다!
#비정상 출력코드
select restaurant_name, total_quantity, total_sales,
abs(total_sales-discount_ratio) 'final_price',
case when total_quantity<=5 then total_sales*0.1
when total_quantity>15 and total_sales>=300000 then total_sales*0.005
else total_sales*0.01 end discount_ratio
from
(
select restaurant_name, sum(quantity) total_quantity, sum(price) total_sales
from food_orders fo
group by restaurant_name
) a

------------------------------------------------------
#정상출력 코드 (b그룹 추가생성 후 abs 활용)
select restaurant_name, total_quantity, total_sales, discount_ratio,
abs(total_sales-discount_ratio) as customer_price
from
(
select restaurant_name, total_quantity, total_sales,
case when total_quantity<=5 then total_sales*0.1
when total_quantity>15 and total_sales>=300000 then total_sales*0.005
else total_sales*0.01 end discount_ratio
from
(
select restaurant_name, sum(quantity) total_quantity, sum(price) total_sales
from food_orders fo
group by restaurant_name
) a
) b

