1
need to create a query that returns the oldest stock of each product within a specified period:
Tabela Produtos:
ID DESCRICAO
1 TOMATE
2 ABACAXI
Tabela Estoque:
DATA HORA PRODUTO ESTOQUE
01/01/2015 09:01:00 1 8
01/01/2015 10:05:15 1 7
01/01/2015 11:00:20 2 15
Should return something like:
Produto Estoque
1 8
2 15
I tried to create something like this:
select e.produto, s1.estoque from estoque e
left outer join
(select produto, e1.estoque
Min(CAST(Right('0' + Cast(DayOfMonth(e1.data) as sql_varchar),2) + '/' +
Right('0' + Cast(Month(e1.data) as sql_varchar),2) + '/' +
Right('00' + Cast(Year(e1.data) as sql_varchar),4) + ' ' +
Trim(e1.Hora) AS SQL_TIMESTAMP)) as tempo
from estmovd e1
where e1.data between '10/01/2015' and '10/10/2015') s1 on s1.produto = e1.produto
where e.data between '10/01/2015' and '10/10/2015'
But without success. Does anyone have any tips to help me?
Thank you
Which database?
– Roger Oliveira
I am using the FREE version of ADS (Advantage Database Server), version 9.1
– Fabio Souza
So the example I answered below uses SQL Server 2012, try to use it in your database. I believe it works! @Fabio
– Roger Oliveira