Como no tengo la consulta previa, solo se me ocurre hacerlo de la siguiente manera. copia y pega para probar y aplicarlo a tu codigo.
Código SQL:
Ver originalDECLARE @inventario AS TABLE(
id INT IDENTITY(1,1),
NodeParte VARCHAR(25),
EntryNo INT,
FechaReg datetime,
Cantidad INT
)
DECLARE @Nuevoinventario AS TABLE(
id INT IDENTITY(1,1),
NodeParte VARCHAR(25),
EntryNo INT,
FechaReg datetime,
Cantidad INT
)
DECLARE
@c INT = 0,
@currentID INT = 1,
@LIMIT INT
INSERT INTO @inventario
SELECT
71199051762, 530647, '2013/10/29', 6
UNION
SELECT 71199051762, 540517, '2013/12/10', 1
UNION
SELECT
71471291604, 541501, '2013/12/16', 14
UNION
SELECT
71471291604, 546111, '2014/01/07', 97
UNION
SELECT
71471544475,518581,'2013/09/07', 12
UNION
SELECT
71471544475,536419,'2013/11/23',1
UNION
SELECT
11121740065 , 531418 , '2013/10/31' , 1
UNION
SELECT
11121740065,531407,'2013/10/31',1
UNION
SELECT
11127507217 , 544866 , '2013/12/31' , 2
UNION
SELECT
11127507217 , 544867 , '2013/12/31' , 2
UNION
SELECT
11129070990 , 542506 , '2013/12/19' , 4
UNION
SELECT
11129070990 , 549891 , '2014/01/23' , 4
SET @LIMIT = (SELECT COUNT(*) FROM @inventario)
WHILE @currentID <= @LIMIT
BEGIN
INSERT INTO @Nuevoinventario
SELECT TOP 1 NodeParte,EntryNo,FechaReg,Cantidad
FROM @inventario
WHERE id = @currentID AND NodeParte NOT IN(SELECT NodeParte FROM @Nuevoinventario)
ORDER BY FechaReg ASC
SET @currentID = @currentID + 1
END
SELECT * FROM @Nuevoinventario