Avatar of rp
rp
Flag for Portugal asked on

Get ballance between In and out product/lot in a given date

Hi,

I have a table "EstLotes" where I keep some data referring to a certain lot. For example lot 205200073. Lots have a single input, but usually have multiple outputs. The Productin column is the quantity produced of a given product (Description) and has a single entry, the ProductOut column is the accumulated value of the outputs of that product/lot.
Then I have a join with the "Estlin" table, which are the various lot outputs, the Quant column (quantity) and the date it came out.
I wanted to get a single line that would return the lote, state, description fields, and another field with the difference between the input and the outputs.
In practice, I need to know what quantity there was on a given date for each lot.
If I for example make a query with the date of 2021-01-07 I should get a balance of 2991-1308=1683

select ESTLotes.lote,ESTLotes.State,ESTLotes.Description,ESTLotes.ProductIn,ESTLotes.ProductOut,
EstLin.Quant,EstLin.Date
from ESTLotes inner join EstLin on ESTLotes.lote=EstLin.lote where EstLin.data <='2021-01-07' and EstLin.lote='205200073'

Open in new window



best regards

SQLMicrosoft SQL Server

Avatar of undefined
Last Comment
rp

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
slightwv (䄆 Netminder)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rp

ASKER
Thanks. I just had to adjust 2 small details, it worked perfectly.  
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy