Código SQL:
Ver originalCREATE TABLE #temp
(
id_stock INT,
id_producto INT,
precio DECIMAL(10,6)
)
INSERT INTO #temp VALUES (0800,0015,4.50)
INSERT INTO #temp VALUES (0820
,0015
,5.00)
INSERT INTO #temp VALUES (0824
,0015
,5.00)
INSERT INTO #temp VALUES (0827
,0018
,3.00)
INSERT INTO #temp VALUES (0901
,0015
,4.00)
INSERT INTO #temp VALUES (0904
,0018
,3.5)
SELECT t1.* FROM #temp AS t1
LEFT JOIN (SELECT MAX(id_stock) stock, id_producto FROM #temp GROUP BY id_producto) AS t2 ON (t1.id_producto=t2.id_producto AND t1.id_stock=t2.stock)
WHERE t2.stock IS NOT NULL
Resultado:
id_stock id_producto precio stock id_producto
904 18 3.500000 904 18
901 15 4.000000 901 15