Calcular saldo sobre una misma tabla

05/09/2005 - 14:59 por Pablodegerli | Informe spam
Hola gente
Tengo esta tabla que la cargo desde un proceso con los 2 campos mas
importantes de Ingreso y Egreso.
Lo que debo es ir calculando en el campo Saldo la diferencia entre los
Ingresos - Egresos hasta la fecha del registro.

Create Table Tmp(Fecha_Hora smalldatetime, NºComprobante int, NºLinea
smallint, Lote int, Pcb smallint, Pedido smallint, Ingreso smallint, Egreso
smallint, Saldo smallint)
Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb, Pedido,
Ingreso, Egreso, Saldo)
values('2005-05-02 01:15:00',4,1,21,6,334,167,0,0)
Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb, Pedido,
Ingreso, Egreso, Saldo)
values('2005-05-03 15:07:10',5,1,29,6,167,167,0,0)
Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb, Pedido,
Ingreso, Egreso, Saldo)
values('2005-07-01 12:25:15',6,1,12,6,200,50,0,0)
Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb, Pedido,
Ingreso, Egreso, Saldo)
values('2005-07-01 21:05:52',25,1,1,6,200,0,140,0)

Fecha_Hora NºComprobante NºLinea
Lote Pcb Pedido Ingreso Egreso Saldo
2005-02-05 01:15:00 4 1
21 6 334 167 0 0
2005-03-05 15:07:00 5 1
29 6 167 167 0 0
2005-01-07 12:25:00 6 1
12 6 200 50 0 0
2005-01-07 21:06:00 25 1
1 6 200 0 140 0

Trate de realizarlo de la siguiente manera

Update Tmp set Saldo = (Select sum(Ingreso) - sum(Egreso) from Tmp T2 where
T2.Fecha <= Fecha )

Pero evidentemente me actualiza todos porque no le estoy poniendo el filtro
para el update pero no puedo hacerlo con T2 porque es una subconsulta.


Como siempre, Gracias de antemano

Preguntas similare

Leer las respuestas

#16 Pablodegerli
05/09/2005 - 21:36 | Informe spam
Wait Paso mi amigo Murphy y me dijo lo siguiente
Servidor: mensaje 137, nivel 15, estado 2, procedimiento SpStock_S, línea 43
Debe declarar la variable '@Table'.
En la Linea:
Update @Table set Saldo = (Select sum(T.Ingreso - T.Egreso) from @Table
T where T.Fecha_Hora <= @Table.Fecha_Hora)
Fue en ese momento que antes de revolear por el aire el servidor note que
con tu respuesta anterior tenia la solucion al hacer

Select A.Fecha_Hora, A.NºComprobante, A.NºLinea, A.Lote, A.Pcb, A.Pedido,
A.Ingreso, A.Egreso, sum(B.Ingreso - B.Egreso) Saldo
from @Table A inner join @Table as B on B.Fecha_Hora <= A.Fecha_Hora
group by A.Fecha_Hora, A.NºComprobante, A.NºLinea, A.Lote, A.Pcb,
A.Pedido, A.Ingreso, A.Egreso order by 1 asc

Y ahi si consegui lo que necesitaba. Esta trabado en el otro razonamiento y
no lo veia.

Nuevamente, GRACIAS


"Pablodegerli" escribió en el mensaje
news:
Update @Table set Saldo = (Select sum(Ingreso - Egreso) from @Table as 2
where t2.fecha <= @table.fecha

AHI ESTA!! esta seria la final porque el
No estaba poniendo el <= @table.fecha
sino que ponia solo <= fecha pensando que lo tomaba de la tabla que estaba
actualizando

Muchas Gracias, Troesma!!!!


"Alejandro Mesa" escribió en el
mensaje news:
> Trata:
>
> update
> @Table
> set
> Saldo = (Select sum(Ingreso - Egreso) from @Table as 2 where t2.fecha <> > @table.fecha)
>
>
> AMB
>
> "Pablodegerli" wrote:
>
> > Claro el tema es el siguiente Alejandro, para no abrumarlos con todo


el
> > codigo genere un ejemplo con la tabla que calculo en base a los
comprobantes
> > de ingreso y egreso de mercaderia
> > Lo que hago es esto
> > set nocount on
> > Insert into @Table
> > Select Ne.Fecha_Recepcion, Ne.Id_Nota_Entrada, Ie.Nro_Linea,


Lo.Id_Lote,
> > Ine.Pcb, Ine.Bultos_Entrantes, Ine.Bultos_Recibidos, 0, 0
> > from Nota_Entrada Ne
> > inner join Item_Nota_Entrada Ine on Ine.Id_Nota_Entrada > > > Ne.Id_Nota_Entrada
> > inner join Item_Entrada Ie on Ie.Id_Item_Entrada > > > Ine.Id_Item_Entrada
> > inner join Lote Lo on Lo.Id_Item_Nota_Entrada > > > Ine.Id_Item_Nota_Entrada
> > and Lo.Id_Nota_Entrada = Ne.Id_Nota_Entrada and
Lo.Id_Producto
> > = Ie.Id_Producto
> > where Ie.Id_Producto = @Id_Producto
> >
> > Insert into @Table
> > Select Re.Fecha, Re.Id_Remito_Salida, It.Nro_Linea, '', It.Pcb,
It.Cantidad
> > * It.Pcb, 0, It.Cantidad_Entregada * It.Pcb, 0
> > from Remito_Salida Re
> > inner join Item_Remito_Salida Ire on Ire.Id_Remito_Salida > > > Re.Id_Remito_Salida
> > inner join Item_Salida It on It.Id_Item_Salida > Ire.Id_Item_Salida
> > where It.Id_Producto = @Id_Producto
> >
> > Para armar el ejemplo use Tmp en vez de @Table
> > Lo que hago es primero llenar @Table y despues tratar de calcular el
saldo
> > que de momento hago asi
> >
> > Declare @Fecha smalldatetime
> > Declare c cursor for select Fecha_Hora from @Table order by Fecha_Hora
> > open c
> > fetch next from c into @Fecha
> > while @@Fetch_Status = 0
> > begin
> > update @Table set Saldo > > > (Select sum(Ingreso) - sum(Egreso) from @Table T2 where
> > T2.Fecha_Hora <= @Fecha )
> > where Fecha_Hora = @Fecha
> > fetch next from c into @Fecha
> > end
> > close c
> > deallocate c
> >
> > Asi que lo que necesitaria es la misma sentencia que haz hecho pero


para
el
> > update y ahi es donde fallo
> >
> > Gracias por entender y por bancarte mi mareo
> > Saludos
> >
> > "Alejandro Mesa" escribió en
el
> > mensaje news:
> > > Trata:
> > >
> > > Create Table Tmp(
> > > Fecha_Hora smalldatetime,
> > > No_Comprobante int,
> > > No_Linea smallint,
> > > Lote int,
> > > Pcb smallint,
> > > Pedido smallint,
> > > Ingreso smallint,
> > > Egreso smallint,
> > > Saldo smallint
> > > )
> > > go
> > >
> > > Insert into Tmp values('2005-05-02 01:15:00',4,1,21,6,334,167,0,0)
> > > Insert into Tmp values('2005-05-03 15:07:10',5,1,29,6,167,167,0,0)
> > > Insert into Tmp values('2005-07-01 12:25:15',6,1,12,6,200,50,0,0)
> > > Insert into Tmp values('2005-07-01 21:05:52',25,1,1,6,200,0,140,0)
> > > go
> > >
> > > select
> > > a.fecha_hora,
> > > a.ingreso,
> > > a.egreso,
> > > sum(b.ingreso - b.egreso) as saldo
> > > from
> > > tmp as a
> > > inner join
> > > tmp as b
> > > on b.Fecha_Hora <= a.Fecha_Hora
> > > group by
> > > a.fecha_hora,
> > > a.ingreso,
> > > a.egreso
> > > order by
> > > 1 asc
> > > go
> > >
> > > drop table tmp
> > > go
> > >
> > > Si este calculo lo quieres hacer por producto, entonces debes


agregar
una
> > > condicion mas al "inner join", que es:
> > >
> > > a.product_id = b.productid
> > >
> > >
> > > AMB
> > >
> > >
> > > "Pablodegerli" wrote:
> > >
> > > > Hola gente
> > > > Tengo esta tabla que la cargo desde un proceso con los 2 campos


mas
> > > > importantes de Ingreso y Egreso.
> > > > Lo que debo es ir calculando en el campo Saldo la diferencia entre
los
> > > > Ingresos - Egresos hasta la fecha del registro.
> > > >
> > > > Create Table Tmp(Fecha_Hora smalldatetime, NºComprobante int,
NºLinea
> > > > smallint, Lote int, Pcb smallint, Pedido smallint, Ingreso


smallint,
> > Egreso
> > > > smallint, Saldo smallint)
> > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb,
Pedido,
> > > > Ingreso, Egreso, Saldo)
> > > > values('2005-05-02 01:15:00',4,1,21,6,334,167,0,0)
> > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb,
Pedido,
> > > > Ingreso, Egreso, Saldo)
> > > > values('2005-05-03 15:07:10',5,1,29,6,167,167,0,0)
> > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb,
Pedido,
> > > > Ingreso, Egreso, Saldo)
> > > > values('2005-07-01 12:25:15',6,1,12,6,200,50,0,0)
> > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb,
Pedido,
> > > > Ingreso, Egreso, Saldo)
> > > > values('2005-07-01 21:05:52',25,1,1,6,200,0,140,0)
> > > >
> > > > Fecha_Hora


NºComprobante
> > NºLinea
> > > > Lote Pcb Pedido Ingreso Egreso Saldo
> > >
> >

> > > > 2005-02-05 01:15:00 4
1
> > > > 21 6 334 167 0 0
> > > > 2005-03-05 15:07:00 5
1
> > > > 29 6 167 167 0 0
> > > > 2005-01-07 12:25:00 6
1
> > > > 12 6 200 50 0 0
> > > > 2005-01-07 21:06:00 25
1
> > > > 1 6 200 0 140 0
> > > >
> > > > Trate de realizarlo de la siguiente manera
> > > >
> > > > Update Tmp set Saldo = (Select sum(Ingreso) - sum(Egreso) from Tmp
T2
> > where
> > > > T2.Fecha <= Fecha )
> > > >
> > > > Pero evidentemente me actualiza todos porque no le estoy poniendo


el
> > filtro
> > > > para el update pero no puedo hacerlo con T2 porque es una
subconsulta.
> > > >
> > > >
> > > > Como siempre, Gracias de antemano
> > > >
> > > >
> > > >
> >
> >
> >


Respuesta Responder a este mensaje
#17 Alejandro Mesa
05/09/2005 - 22:20 | Informe spam
Pablo,

Tu amigo Murphy esta en lo correcto. SQL Server no permite este tipo de
referencia con variables tipo tabla. La solucion seria usar otro tipo de
patron en la sentencia "update".

Update a
set Saldo = (
Select sum(b.Ingreso - b.Egreso) from @Table as b
where b.fecha <= a.fecha
)
from @Table as a


Ejemplo:

declare @t table (
c1 int not null identity,
c2 int
)

insert into @t default values
insert into @t default values
insert into @t default values

update a
set c2 = (select sum(b.c1) from @t as b where b.c1 <= a.c1)
from
@t as a

select * from @t
go


AMB

"Pablodegerli" wrote:

Wait Paso mi amigo Murphy y me dijo lo siguiente
Servidor: mensaje 137, nivel 15, estado 2, procedimiento SpStock_S, línea 43
Debe declarar la variable '@Table'.
En la Linea:
Update @Table set Saldo = (Select sum(T.Ingreso - T.Egreso) from @Table
T where T.Fecha_Hora <= @Table.Fecha_Hora)
Fue en ese momento que antes de revolear por el aire el servidor note que
con tu respuesta anterior tenia la solucion al hacer

Select A.Fecha_Hora, A.NºComprobante, A.NºLinea, A.Lote, A.Pcb, A.Pedido,
A.Ingreso, A.Egreso, sum(B.Ingreso - B.Egreso) Saldo
from @Table A inner join @Table as B on B.Fecha_Hora <= A.Fecha_Hora
group by A.Fecha_Hora, A.NºComprobante, A.NºLinea, A.Lote, A.Pcb,
A.Pedido, A.Ingreso, A.Egreso order by 1 asc

Y ahi si consegui lo que necesitaba. Esta trabado en el otro razonamiento y
no lo veia.

Nuevamente, GRACIAS


"Pablodegerli" escribió en el mensaje
news:
> Update @Table set Saldo = (Select sum(Ingreso - Egreso) from @Table as 2
> where t2.fecha <= @table.fecha
>
> AHI ESTA!! esta seria la final porque el
> No estaba poniendo el <= @table.fecha
> sino que ponia solo <= fecha pensando que lo tomaba de la tabla que estaba
> actualizando
>
> Muchas Gracias, Troesma!!!!
>
>
> "Alejandro Mesa" escribió en el
> mensaje news:
> > Trata:
> >
> > update
> > @Table
> > set
> > Saldo = (Select sum(Ingreso - Egreso) from @Table as 2 where t2.fecha <> > > @table.fecha)
> >
> >
> > AMB
> >
> > "Pablodegerli" wrote:
> >
> > > Claro el tema es el siguiente Alejandro, para no abrumarlos con todo
el
> > > codigo genere un ejemplo con la tabla que calculo en base a los
> comprobantes
> > > de ingreso y egreso de mercaderia
> > > Lo que hago es esto
> > > set nocount on
> > > Insert into @Table
> > > Select Ne.Fecha_Recepcion, Ne.Id_Nota_Entrada, Ie.Nro_Linea,
Lo.Id_Lote,
> > > Ine.Pcb, Ine.Bultos_Entrantes, Ine.Bultos_Recibidos, 0, 0
> > > from Nota_Entrada Ne
> > > inner join Item_Nota_Entrada Ine on Ine.Id_Nota_Entrada > > > > Ne.Id_Nota_Entrada
> > > inner join Item_Entrada Ie on Ie.Id_Item_Entrada > > > > Ine.Id_Item_Entrada
> > > inner join Lote Lo on Lo.Id_Item_Nota_Entrada > > > > Ine.Id_Item_Nota_Entrada
> > > and Lo.Id_Nota_Entrada = Ne.Id_Nota_Entrada and
> Lo.Id_Producto
> > > = Ie.Id_Producto
> > > where Ie.Id_Producto = @Id_Producto
> > >
> > > Insert into @Table
> > > Select Re.Fecha, Re.Id_Remito_Salida, It.Nro_Linea, '', It.Pcb,
> It.Cantidad
> > > * It.Pcb, 0, It.Cantidad_Entregada * It.Pcb, 0
> > > from Remito_Salida Re
> > > inner join Item_Remito_Salida Ire on Ire.Id_Remito_Salida > > > > Re.Id_Remito_Salida
> > > inner join Item_Salida It on It.Id_Item_Salida > > Ire.Id_Item_Salida
> > > where It.Id_Producto = @Id_Producto
> > >
> > > Para armar el ejemplo use Tmp en vez de @Table
> > > Lo que hago es primero llenar @Table y despues tratar de calcular el
> saldo
> > > que de momento hago asi
> > >
> > > Declare @Fecha smalldatetime
> > > Declare c cursor for select Fecha_Hora from @Table order by Fecha_Hora
> > > open c
> > > fetch next from c into @Fecha
> > > while @@Fetch_Status = 0
> > > begin
> > > update @Table set Saldo > > > > (Select sum(Ingreso) - sum(Egreso) from @Table T2 where
> > > T2.Fecha_Hora <= @Fecha )
> > > where Fecha_Hora = @Fecha
> > > fetch next from c into @Fecha
> > > end
> > > close c
> > > deallocate c
> > >
> > > Asi que lo que necesitaria es la misma sentencia que haz hecho pero
para
> el
> > > update y ahi es donde fallo
> > >
> > > Gracias por entender y por bancarte mi mareo
> > > Saludos
> > >
> > > "Alejandro Mesa" escribió en
> el
> > > mensaje news:
> > > > Trata:
> > > >
> > > > Create Table Tmp(
> > > > Fecha_Hora smalldatetime,
> > > > No_Comprobante int,
> > > > No_Linea smallint,
> > > > Lote int,
> > > > Pcb smallint,
> > > > Pedido smallint,
> > > > Ingreso smallint,
> > > > Egreso smallint,
> > > > Saldo smallint
> > > > )
> > > > go
> > > >
> > > > Insert into Tmp values('2005-05-02 01:15:00',4,1,21,6,334,167,0,0)
> > > > Insert into Tmp values('2005-05-03 15:07:10',5,1,29,6,167,167,0,0)
> > > > Insert into Tmp values('2005-07-01 12:25:15',6,1,12,6,200,50,0,0)
> > > > Insert into Tmp values('2005-07-01 21:05:52',25,1,1,6,200,0,140,0)
> > > > go
> > > >
> > > > select
> > > > a.fecha_hora,
> > > > a.ingreso,
> > > > a.egreso,
> > > > sum(b.ingreso - b.egreso) as saldo
> > > > from
> > > > tmp as a
> > > > inner join
> > > > tmp as b
> > > > on b.Fecha_Hora <= a.Fecha_Hora
> > > > group by
> > > > a.fecha_hora,
> > > > a.ingreso,
> > > > a.egreso
> > > > order by
> > > > 1 asc
> > > > go
> > > >
> > > > drop table tmp
> > > > go
> > > >
> > > > Si este calculo lo quieres hacer por producto, entonces debes
agregar
> una
> > > > condicion mas al "inner join", que es:
> > > >
> > > > a.product_id = b.productid
> > > >
> > > >
> > > > AMB
> > > >
> > > >
> > > > "Pablodegerli" wrote:
> > > >
> > > > > Hola gente
> > > > > Tengo esta tabla que la cargo desde un proceso con los 2 campos
mas
> > > > > importantes de Ingreso y Egreso.
> > > > > Lo que debo es ir calculando en el campo Saldo la diferencia entre
> los
> > > > > Ingresos - Egresos hasta la fecha del registro.
> > > > >
> > > > > Create Table Tmp(Fecha_Hora smalldatetime, NºComprobante int,
> NºLinea
> > > > > smallint, Lote int, Pcb smallint, Pedido smallint, Ingreso
smallint,
> > > Egreso
> > > > > smallint, Saldo smallint)
> > > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb,
> Pedido,
> > > > > Ingreso, Egreso, Saldo)
> > > > > values('2005-05-02 01:15:00',4,1,21,6,334,167,0,0)
> > > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb,
> Pedido,
> > > > > Ingreso, Egreso, Saldo)
> > > > > values('2005-05-03 15:07:10',5,1,29,6,167,167,0,0)
> > > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb,
> Pedido,
> > > > > Ingreso, Egreso, Saldo)
> > > > > values('2005-07-01 12:25:15',6,1,12,6,200,50,0,0)
> > > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote, Pcb,
> Pedido,
> > > > > Ingreso, Egreso, Saldo)
> > > > > values('2005-07-01 21:05:52',25,1,1,6,200,0,140,0)
> > > > >
> > > > > Fecha_Hora
NºComprobante
> > > NºLinea
> > > > > Lote Pcb Pedido Ingreso Egreso Saldo
> > > >
> > >
>
> > > > > 2005-02-05 01:15:00 4
> 1
> > > > > 21 6 334 167 0 0
> > > > > 2005-03-05 15:07:00 5
> 1
> > > > > 29 6 167 167 0 0
> > > > > 2005-01-07 12:25:00 6
> 1
> > > > > 12 6 200 50 0 0
> > > > > 2005-01-07 21:06:00 25
> 1
> > > > > 1 6 200 0 140 0
> > > > >
> > > > > Trate de realizarlo de la siguiente manera
> > > > >
> > > > > Update Tmp set Saldo = (Select sum(Ingreso) - sum(Egreso) from Tmp
> T2
> > > where
> > > > > T2.Fecha <= Fecha )
> > > > >
> > > > > Pero evidentemente me actualiza todos porque no le estoy poniendo
el
> > > filtro
> > > > > para el update pero no puedo hacerlo con T2 porque es una
> subconsulta.
> > > > >
> > > > >
> > > > > Como siempre, Gracias de antemano
> > > > >
> > > > >
> > > > >
> > >
> > >
> > >
>
>



Respuesta Responder a este mensaje
#18 Pablodegerli
05/09/2005 - 22:27 | Informe spam
Barbaro probaba update Tabla as X set pero no me dejaba poner el alias asi
No encontraba que podia poner el update directamente con el alis para hacer
referencia mas tarde.
Muchas Gracias

"Alejandro Mesa" escribió en el
mensaje news:
Pablo,

Tu amigo Murphy esta en lo correcto. SQL Server no permite este tipo de
referencia con variables tipo tabla. La solucion seria usar otro tipo de
patron en la sentencia "update".

Update a
set Saldo = (
Select sum(b.Ingreso - b.Egreso) from @Table as b
where b.fecha <= a.fecha
)
from @Table as a


Ejemplo:

declare @t table (
c1 int not null identity,
c2 int
)

insert into @t default values
insert into @t default values
insert into @t default values

update a
set c2 = (select sum(b.c1) from @t as b where b.c1 <= a.c1)
from
@t as a

select * from @t
go


AMB

"Pablodegerli" wrote:

> Wait Paso mi amigo Murphy y me dijo lo siguiente
> Servidor: mensaje 137, nivel 15, estado 2, procedimiento SpStock_S,


línea 43
> Debe declarar la variable '@Table'.
> En la Linea:
> Update @Table set Saldo = (Select sum(T.Ingreso - T.Egreso) from


@Table
> T where T.Fecha_Hora <= @Table.Fecha_Hora)
> Fue en ese momento que antes de revolear por el aire el servidor note


que
> con tu respuesta anterior tenia la solucion al hacer
>
> Select A.Fecha_Hora, A.NºComprobante, A.NºLinea, A.Lote, A.Pcb,


A.Pedido,
> A.Ingreso, A.Egreso, sum(B.Ingreso - B.Egreso) Saldo
> from @Table A inner join @Table as B on B.Fecha_Hora <A.Fecha_Hora
> group by A.Fecha_Hora, A.NºComprobante, A.NºLinea, A.Lote, A.Pcb,
> A.Pedido, A.Ingreso, A.Egreso order by 1 asc
>
> Y ahi si consegui lo que necesitaba. Esta trabado en el otro


razonamiento y
> no lo veia.
>
> Nuevamente, GRACIAS
>
>
> "Pablodegerli" escribió en el mensaje
> news:
> > Update @Table set Saldo = (Select sum(Ingreso - Egreso) from @Table


as 2
> > where t2.fecha <= @table.fecha
> >
> > AHI ESTA!! esta seria la final porque el
> > No estaba poniendo el <= @table.fecha
> > sino que ponia solo <= fecha pensando que lo tomaba de la tabla que


estaba
> > actualizando
> >
> > Muchas Gracias, Troesma!!!!
> >
> >
> > "Alejandro Mesa" escribió en


el
> > mensaje news:
> > > Trata:
> > >
> > > update
> > > @Table
> > > set
> > > Saldo = (Select sum(Ingreso - Egreso) from @Table as 2 where


t2.fecha <> > > > @table.fecha)
> > >
> > >
> > > AMB
> > >
> > > "Pablodegerli" wrote:
> > >
> > > > Claro el tema es el siguiente Alejandro, para no abrumarlos con


todo
> el
> > > > codigo genere un ejemplo con la tabla que calculo en base a los
> > comprobantes
> > > > de ingreso y egreso de mercaderia
> > > > Lo que hago es esto
> > > > set nocount on
> > > > Insert into @Table
> > > > Select Ne.Fecha_Recepcion, Ne.Id_Nota_Entrada, Ie.Nro_Linea,
> Lo.Id_Lote,
> > > > Ine.Pcb, Ine.Bultos_Entrantes, Ine.Bultos_Recibidos, 0, 0
> > > > from Nota_Entrada Ne
> > > > inner join Item_Nota_Entrada Ine on Ine.Id_Nota_Entrada > > > > > Ne.Id_Nota_Entrada
> > > > inner join Item_Entrada Ie on Ie.Id_Item_Entrada > > > > > Ine.Id_Item_Entrada
> > > > inner join Lote Lo on Lo.Id_Item_Nota_Entrada > > > > > Ine.Id_Item_Nota_Entrada
> > > > and Lo.Id_Nota_Entrada = Ne.Id_Nota_Entrada and
> > Lo.Id_Producto
> > > > = Ie.Id_Producto
> > > > where Ie.Id_Producto = @Id_Producto
> > > >
> > > > Insert into @Table
> > > > Select Re.Fecha, Re.Id_Remito_Salida, It.Nro_Linea, '', It.Pcb,
> > It.Cantidad
> > > > * It.Pcb, 0, It.Cantidad_Entregada * It.Pcb, 0
> > > > from Remito_Salida Re
> > > > inner join Item_Remito_Salida Ire on Ire.Id_Remito_Salida
> > > > Re.Id_Remito_Salida
> > > > inner join Item_Salida It on It.Id_Item_Salida > > > Ire.Id_Item_Salida
> > > > where It.Id_Producto = @Id_Producto
> > > >
> > > > Para armar el ejemplo use Tmp en vez de @Table
> > > > Lo que hago es primero llenar @Table y despues tratar de calcular


el
> > saldo
> > > > que de momento hago asi
> > > >
> > > > Declare @Fecha smalldatetime
> > > > Declare c cursor for select Fecha_Hora from @Table order by


Fecha_Hora
> > > > open c
> > > > fetch next from c into @Fecha
> > > > while @@Fetch_Status = 0
> > > > begin
> > > > update @Table set Saldo > > > > > (Select sum(Ingreso) - sum(Egreso) from @Table T2 where
> > > > T2.Fecha_Hora <= @Fecha )
> > > > where Fecha_Hora = @Fecha
> > > > fetch next from c into @Fecha
> > > > end
> > > > close c
> > > > deallocate c
> > > >
> > > > Asi que lo que necesitaria es la misma sentencia que haz hecho


pero
> para
> > el
> > > > update y ahi es donde fallo
> > > >
> > > > Gracias por entender y por bancarte mi mareo
> > > > Saludos
> > > >
> > > > "Alejandro Mesa"


escribió en
> > el
> > > > mensaje news:
> > > > > Trata:
> > > > >
> > > > > Create Table Tmp(
> > > > > Fecha_Hora smalldatetime,
> > > > > No_Comprobante int,
> > > > > No_Linea smallint,
> > > > > Lote int,
> > > > > Pcb smallint,
> > > > > Pedido smallint,
> > > > > Ingreso smallint,
> > > > > Egreso smallint,
> > > > > Saldo smallint
> > > > > )
> > > > > go
> > > > >
> > > > > Insert into Tmp values('2005-05-02


01:15:00',4,1,21,6,334,167,0,0)
> > > > > Insert into Tmp values('2005-05-03


15:07:10',5,1,29,6,167,167,0,0)
> > > > > Insert into Tmp values('2005-07-01


12:25:15',6,1,12,6,200,50,0,0)
> > > > > Insert into Tmp values('2005-07-01


21:05:52',25,1,1,6,200,0,140,0)
> > > > > go
> > > > >
> > > > > select
> > > > > a.fecha_hora,
> > > > > a.ingreso,
> > > > > a.egreso,
> > > > > sum(b.ingreso - b.egreso) as saldo
> > > > > from
> > > > > tmp as a
> > > > > inner join
> > > > > tmp as b
> > > > > on b.Fecha_Hora <= a.Fecha_Hora
> > > > > group by
> > > > > a.fecha_hora,
> > > > > a.ingreso,
> > > > > a.egreso
> > > > > order by
> > > > > 1 asc
> > > > > go
> > > > >
> > > > > drop table tmp
> > > > > go
> > > > >
> > > > > Si este calculo lo quieres hacer por producto, entonces debes
> agregar
> > una
> > > > > condicion mas al "inner join", que es:
> > > > >
> > > > > a.product_id = b.productid
> > > > >
> > > > >
> > > > > AMB
> > > > >
> > > > >
> > > > > "Pablodegerli" wrote:
> > > > >
> > > > > > Hola gente
> > > > > > Tengo esta tabla que la cargo desde un proceso con los 2


campos
> mas
> > > > > > importantes de Ingreso y Egreso.
> > > > > > Lo que debo es ir calculando en el campo Saldo la diferencia


entre
> > los
> > > > > > Ingresos - Egresos hasta la fecha del registro.
> > > > > >
> > > > > > Create Table Tmp(Fecha_Hora smalldatetime, NºComprobante int,
> > NºLinea
> > > > > > smallint, Lote int, Pcb smallint, Pedido smallint, Ingreso
> smallint,
> > > > Egreso
> > > > > > smallint, Saldo smallint)
> > > > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote,


Pcb,
> > Pedido,
> > > > > > Ingreso, Egreso, Saldo)
> > > > > > values('2005-05-02 01:15:00',4,1,21,6,334,167,0,0)
> > > > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote,


Pcb,
> > Pedido,
> > > > > > Ingreso, Egreso, Saldo)
> > > > > > values('2005-05-03 15:07:10',5,1,29,6,167,167,0,0)
> > > > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote,


Pcb,
> > Pedido,
> > > > > > Ingreso, Egreso, Saldo)
> > > > > > values('2005-07-01 12:25:15',6,1,12,6,200,50,0,0)
> > > > > > Insert into Tmp(Fecha_Hora, NºComprobante, NºLinea, Lote,


Pcb,
> > Pedido,
> > > > > > Ingreso, Egreso, Saldo)
> > > > > > values('2005-07-01 21:05:52',25,1,1,6,200,0,140,0)
> > > > > >
> > > > > > Fecha_Hora
> NºComprobante
> > > > NºLinea
> > > > > > Lote Pcb Pedido Ingreso Egreso Saldo
> > > > >
> > > >
> >
>
> > > > > > 2005-02-05 01:15:00 4
> > 1
> > > > > > 21 6 334 167 0 0
> > > > > > 2005-03-05 15:07:00 5
> > 1
> > > > > > 29 6 167 167 0 0
> > > > > > 2005-01-07 12:25:00 6
> > 1
> > > > > > 12 6 200 50 0 0
> > > > > > 2005-01-07 21:06:00 25
> > 1
> > > > > > 1 6 200 0 140 0
> > > > > >
> > > > > > Trate de realizarlo de la siguiente manera
> > > > > >
> > > > > > Update Tmp set Saldo = (Select sum(Ingreso) - sum(Egreso) from


Tmp
> > T2
> > > > where
> > > > > > T2.Fecha <= Fecha )
> > > > > >
> > > > > > Pero evidentemente me actualiza todos porque no le estoy


poniendo
> el
> > > > filtro
> > > > > > para el update pero no puedo hacerlo con T2 porque es una
> > subconsulta.
> > > > > >
> > > > > >
> > > > > > Como siempre, Gracias de antemano
> > > > > >
> > > > > >
> > > > > >
> > > >
> > > >
> > > >
> >
> >
>
>
>
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una pregunta AnteriorRespuesta Tengo una respuesta
Search Busqueda sugerida