Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   SQL (https://www.clubdelphi.com/foros/forumdisplay.php?f=6)
-   -   SQL CASE y COALESCE (https://www.clubdelphi.com/foros/showthread.php?t=62069)

mjjj 02-12-2008 23:10:02

SQL CASE y COALESCE
 
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.

poliburro 17-12-2008 16:37:52

Podrías indicarnos que motor de base de datos utilizas?


La franja horaria es GMT +2. Ahora son las 00:32:53.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi