TIL/SQL

TIL (7/11) <Round(), div>

jojoon2786 2024. 7. 11. 16:22

 

풀이

select round(avg(daily_fee),0) as average_fee
from car_rental_company_car
where car_type = 'SUV'

 

ROUND('수치값', '반올림 자릿수')

 

 

나의 풀이

select case when price between 10000 and 19999 then 10000
                   when price between 20000 and 29999 then 20000
                   when price between 30000 and 39999 then 30000
                   when price between 40000 and 49999 then 40000
                   when price between 50000 and 59999 then 50000
                   when price between 60000 and 69999 then 60000
                   when price between 70000 and 79999 then 70000
                   when price >= 80000 then 80000
                   end as price_group, count(*)
from product
group by price_group
order by price_group

범위가 주어졌기에 가능 case문을 백 개, 천 개 이상 써야될 수도 있다.

 

해결

select price div 10000 * 10000 as price_group, count(*)
from product
group by price_group
order by price_group

컬럼 div 나눠줄 수 >> 몫 반환