Ver Mensaje Individual
  #4 (permalink)  
Antiguo 14/10/2013, 11:37
GeriReshef
 
Fecha de Ingreso: julio-2012
Ubicación: Israel
Mensajes: 360
Antigüedad: 11 años, 9 meses
Puntos: 40
Respuesta: Problema inventario

Adjunto un ejemplo simple apto a SQL Server 2012, espero que te sirva de ayuda:
Código SQL:
Ver original
  1. CREATE TABLE #Inventario(Fecha DateTime,
  2.                         Producto INT,
  3.                         Tipo CHAR(7),
  4.                         Cantidad INT);
  5.  
  6. INSERT
  7. INTO    #Inventario
  8. VALUES  ('20130101',1,'Entrada',100),
  9.         ('20130102',1,'Salida',30),
  10.         ('20130103',1,'Salida',40),
  11.         ('20130101',2,'Entrada',200),
  12.         ('20130102',2,'Entrada',100),
  13.         ('20130101',3,'Entrada',50);
  14.  
  15. SELECT  *,
  16.         SUM(CASE WHEN Tipo='Entrada' THEN 1 ELSE -1 END*Cantidad)
  17.                     OVER(Partition BY Producto ORDER BY Fecha
  18.                     ROWS BETWEEN Unbounded Preceding AND CURRENT ROW) RunningAggregate
  19. FROM    #Inventario
  20. ORDER BY Producto,
  21.         Fecha;
__________________
El Castellano no es mi lengua materna: discúlpenme por los errores gramaticales.
Mi blog