sales 表格如下
id total_price sales_num
1 96.3 5
2 0.0 0
3 159.3 3
字段total_price 为销售总金额
字段sales_num为销售数量
现需要计算销售单价price
select id,
total_price,
sales_num,
total_price/sales_num as price
from sales;
那么sales_num有存在0的情况,就会出错。
用decode做处理:
select id,
total_price,
sales_num,
decode(sales_num,0,0,total_price/sales_num) as price
from sales;