Hello, good morning, I request a help or guide to try to solve a query in sql, it should be noted that I am new in this world; I am trying to make a query that shows the last record by item code that is to say I have values such as item code, date and cost, I need to structure a query that gives me the last record of that item based on the maximum cost and date.
This is the example of my code.
select CodItem, Descrip1, Costo, convert(varchar(10),FechaE,105) as [Fecha de Compra], NumeroD, TipoCom, CodProv, Cantidad
from saitemcom
group by CodItem, Descrip1, Costo, FechaE, NumeroD, TipoCom, CodProv, Cantidad
having TipoCom = 'H'
order by CodItem,FechaE Desc
And my result is this:
CodItem Descrip1 Costo Fecha de Compra NumeroD TipoCom CodProv Cantidad
00001 Porta Vaso Ram 159.51000 09-06-2011 00000051 H 313816680 2.00000
00001 Porta Vaso Ram 333.34000 11-03-2011 00001388 H 29849302-0 6.00000
00001 Porta Vaso Ram 210.88000 17-07-2009 00000980 H 314418777 2.00000
00004 Porta Ipod Ram 276.48000 17-07-2009 00000980 H 314418777 1.00000
00005 Porta Iphone Ram 100000.00000 22-04-2017 000053 H 404366709 2.00000
00005 Porta Iphone Ram 276.48000 17-07-2009 00000980 H 314418777 1.00000
000164 Manilla Compuerta Chevrolet Silverado ce 166.50000 26-05-2010 00000201 H 29849302-0 1.00000
00031 Rejilla Stop Toyota Corolla 09 266.07000 26-05-2010 00000201 H 29849302-0 1.00000
001-031 Alarma Up-Grade Alsama 38094.86000 23-11-2016 0000024 H 407318675 5.00000
001-031 Alarma Up-Grade Alsama 31745.72000 22-11-2016 002 H 407318675 1.00000
Now if you can see the code 00001 it is repeated several times I need that code the last record with the highest value and obviously the date and so on with the other codes such as 001-0031 or 000005 which also present the same case.
But I do not know what to look for in order to make this query I can not achieve it with group by, nor with max because then it shows me a single record of the whole table and not one of each or with last_value; I do not know if I'm not grouping the right way.
Could you please guys and girls from the forum!