Update with calculation

Asked

Viewed 426 times

0

I have a product table and I want to calculate the profit margin based on the columns preco_venda and preco_custo I did as below:

update produto set margem_lucro=((((preco_unit/preco_custo)*100)-100))

The mistake happens

ERROR: Division by zero

ERROR: Division by zero SQL state: 22012

What is wrong?

  • 3

    In some line the value of preco_cost must be zero , it cannot be divided by zero, update product set margem_profit= (case when preco_product > 0 then (((preco_unit/preco_cost)*100)-100)) Else null end)

  • update produto set margem_lucro= (case when preco_custo > 0 then (((preco_unit/preco_custo)*100)-100)) Else null end) that’s right, I did as you said and worked, Motta was worth!

  • @Motta, as an answer

  • update product set margem_lucro= (case when preco_custo > 0 then (((preco_unit/preco_custo)*100)-100)) Else null end) that’s right, I did as I said and worked, it was worth @Motta!

  • update product set margem_lucro= (case when preco_custo > 0 then (((preco_unit/preco_custo)*100)-100)) Else null end) that’s right, I did as I said and worked, it was worth @Motta

1 answer

1

In some line the value of preco_cost must be zero , can not be divided by zero , do :

update produto set margem_lucro =
                   (case when preco_produto > 0 then 
                              ((((preco_unit/preco_custo) * 100) - 100))         
                         else null end)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.