Tema: Sum SQL
Ver Mensaje Individual
  #9  
Antiguo 13-11-2008
mjjj mjjj is offline
Miembro
 
Registrado: mar 2007
Posts: 652
Reputación: 20
mjjj Va por buen camino
Finalmente pude solucionar mi problema con el siguiente codigo:

Código SQL [-]
SELECT M.nom_insum,
          (SELECT coalesce(sum(cantidad),0) FROM inventario I WHERE I.id=M.cod_insum and tipo ='I')-
          (SELECT coalesce(sum(cantidad),0) FROM inventario i WHERE I.id=M.cod_insum and tipo ='E') AS SALDO,
          ((SELECT coalesce(sum(cantidad * precio),0) FROM inventario I WHERE I.id=M.cod_insum and tipo ='I')-
           (SELECT coalesce(sum(cantidad * precio),0) FROM inventario I WHERE I.id=M.cod_insum and tipo ='E'))/
          coalesce((SELECT sum(cantidad) FROM inventario I WHERE I.id=M.cod_insum and tipo ='I')-
          (SELECT COALESCE(sum(cantidad),0) FROM inventario i WHERE I.id=M.cod_insum and tipo ='E'),1) as precio
FROM insumos M

Ocupe 2 tablas, una con el detalle de los insumos y la otra con el movimiento de estos. (ingresos y egresos)
Responder Con Cita