0
help me. I’m in a situation where I want to bring only the last note entry of each product to create a report. I created a select, but there are products repeating where they shouldn’t. See select:
SELECT P.CodProd, MAX(E.Entrada) Entrada, P.PrecoCustoUNSC,IE.ValorCaixa 
FROM Entradas E 
        INNER JOIN ItensEntrada IE ON E.Cod_Entrada=IE.Cod_Entrada 
        INNER JOIN Produtos P ON IE.CodProd=P.CodProd 
        WHERE IE.ValorCaixa>0 
GROUP BY P.CodProd, P.PrecoCustoUNSC, IE.ValorCaixa 
ORDER BY 1
Upshot:
CodProd Entrada                 PrecoCustoUNSC                          ValorCaixa
------- ----------------------- --------------------------------------- ---------------------------------------
000001  2019-08-20 00:00:00.000 3.0000000000                            3.0000000000
000001  2019-08-19 00:00:00.000 3.0000000000                            10.0000000000
000001  2019-08-23 00:00:00.000 3.0000000000                            15.0000000000
000002  2019-08-20 00:00:00.000 15.0000000000                           15.0000000000
000002  2019-08-23 00:00:00.000 15.0000000000                           65.0000000000
000003  2019-08-23 00:00:00.000 30.0000000000                           30.0000000000
000003  2019-09-06 00:00:00.000 30.0000000000                           50.0000000000
000004  2019-08-23 00:00:00.000 50.0000000000                           50.0000000000
000005  2019-09-04 00:00:00.000 5.0000000000                            5.0000000000
000006  2019-09-04 00:00:00.000 5.0000000000                            5.0000000000
000007  2019-09-04 00:00:00.000 30.0000000000                           30.0000000000
000008  2019-09-04 00:00:00.000 50.0000000000                           50.0000000000
000009  2019-09-05 00:00:00.000 69.0000000000                           69.0000000000
000010  2019-09-05 00:00:00.000 35.0000000000                           35.0000000000
000011  2019-09-11 00:00:00.000 50.0000000000                           50.0000000000
000011  2019-09-06 00:00:00.000 50.0000000000                           60.0000000000
000012  2019-09-11 00:00:00.000 30.0000000000                           30.0000000000
000013  2019-09-11 00:00:00.000 15.0000000000                           15.0000000000
000014  2019-09-11 00:00:00.000 30.0000000000                           30.0000000000
000015  2019-09-11 00:00:00.000 13.0000000000                           13.0000000000
000015  2019-09-11 00:00:00.000 13.0000000000                           6.5000000000
000016  2019-09-11 00:00:00.000 30.0000000000                           30.0000000000
So, product 1 for example is repeating itself, and I wanted only the last record (Input = '2019-08-23') which was the last entry of his product. I needed to relate because in Entries is the date of entry, the Itensentrada contains Valorcaixa that is the cost price where I want to compare with the registration of the product that is Precocustounsc.
And how to know the last entry of a product? The column
Entradais unreliable as there may be more than one entry for the same product in a single day.– José Diz
The database manager is Mysql or SQL Server?
– José Diz
SQL SERVER. True, I hadn’t thought about it, we can use instead of input the COD_ENTRADA field which is the sequential number of each note.
– Giovane Barbosa
The 3 lines you say are repeated have different values for the Valubox field, one of the fields present in the GROUP BY clause.
– anonimo