hola amigos, tengo un problema al hacer una consulta SQL.
Estoi haciendo una aplicacion de inventario, la cual calcula el precio ponderado del item...
Todo funciona bien hasta que trato de dividir por 0, esto ocurre cuando la sumatoria de los ingresos, es igual, a la sumatoria de los egresos. (lo remarco en rojo)
Utilizo este codigo
Código SQL
[-]
SELECT M.nom_insum, m.unidad,
((SELECT coalesce(sum(cantidad * precio),0) FROM inventario I WHERE I.id=M.cod_insum and tipo ='I' and i.bodega ='2')-
(SELECT coalesce(sum(cantidad * precio),0) FROM inventario I WHERE I.id=M.cod_insum and tipo ='E' and i.bodega ='2'))/
(coalesce((SELECT coalesce(sum(cantidad),0) FROM inventario I WHERE I.id=M.cod_insum and tipo ='I' and i.bodega ='2')-
(SELECT COALESCE(sum(cantidad),0) FROM inventario i WHERE I.id=M.cod_insum and tipo ='E' and i.bodega ='2'),1)) as precio
FROM insumos M
ORDER BY M.NOM_INSUM ASC
La pregunta es como puedo solucionar esto, trate que agregando un "CASE", en el caso de que esto fuera 0, pero me arroja este error:
"Unsuccessful execution caused by a system error that precludes
successful execution of subsequent statements.
Dynamic SQL Error.
expression evaluation not supported."
al utilizar este codigo (el que incluye el CASE)
Código SQL
[-]
SELECT M.nom_insum, m.unidad,
((SELECT coalesce(sum(cantidad * precio),0) FROM inventario I WHERE I.id=M.cod_insum and tipo ='I' and i.bodega ='2')-
(SELECT coalesce(sum(cantidad * precio),0) FROM inventario I WHERE I.id=M.cod_insum and tipo ='E' and i.bodega ='2'))/
case (coalesce((SELECT coalesce(sum(cantidad),0) FROM inventario I WHERE I.id=M.cod_insum and tipo ='I' and i.bodega ='2')-
(SELECT COALESCE(sum(cantidad),0) FROM inventario i WHERE I.id=M.cod_insum and tipo ='E' and i.bodega ='2'),1)) when '0' then '1' end as precio
FROM insumos M
ORDER BY M.NOM_INSUM ASC
Bueno.. ese es mi problema, ojala alguien me pueda ayudar.
Muchas gracias por su tiempo.