Un poco rebuscado el query jejejje
Código SQL:
Ver originalCREATE TABLE #temp (
id INT,
fecha datetime
)
CREATE TABLE #temp2 (
id INT,
codarticulo INT,
precio INT
)
INSERT INTO #temp (id,fecha) VALUES (1,getdate()-1)
INSERT INTO #temp (id,fecha) VALUES (2,getdate()-2)
INSERT INTO #temp (id,fecha) VALUES (3,getdate()-3)
INSERT INTO #temp (id,fecha) VALUES (4,getdate()-4)
INSERT INTO #temp2 (id,codarticulo,precio) VALUES (1,1,25)
INSERT INTO #temp2 (id,codarticulo,precio) VALUES (2,1,15)
INSERT INTO #temp2 (id,codarticulo,precio) VALUES (3,1,35)
INSERT INTO #temp2 (id,codarticulo,precio) VALUES (4,1,23)
INSERT INTO #temp2 (id,codarticulo,precio) VALUES (1,2,51)
INSERT INTO #temp2 (id,codarticulo,precio) VALUES (2,2,28)
INSERT INTO #temp2 (id,codarticulo,precio) VALUES (3,2,13)
SELECT codarticulo,fecha,precio FROM #temp t
INNER JOIN #temp2 t1 ON (t.id=t1.id)
WHERE CONVERT(VARCHAR(20),fecha) + '|' + CONVERT(VARCHAR(20),codarticulo) IN (
SELECT CONVERT(VARCHAR(200),MAX(fecha)) + '|' + CONVERT(VARCHAR(20),codarticulo) FROM #temp t
INNER JOIN #temp2 t1 ON (t.id=t1.id)
GROUP BY codarticulo
)
Saludos!