I got situation to find duplicates from different sourceport system.
for Ex: I got table like below:
declare @table table (id int,portnumber int, [sourceport] varchar(50), sourcereportedDate datetime ) insert into @table values (1, 1111, 'north' , '2016-08-20 09:44:30.847') insert into @table values (2, 1111, 'north' , '2016-08-21 09:44:30.847') insert into @table values (3, 1111, 'north' , '2016-08-22 09:44:30.847') insert into @table values (4, 2222, 'north' , '2016-08-20 09:44:30.847') insert into @table values (5, 2222, 'north' , '2016-08-26 09:44:30.847') insert into @table values (6, 2222, 'south' , '2016-08-22 09:44:30.847') insert into @table values (7, 3333, 'south' , '2016-08-10 09:44:30.847') insert into @table values (8, 3333, 'north' , '2016-08-12 09:44:30.847') insert into @table values (9, 4444, 'north' , '2016-08-20 09:44:30.847') insert into @table values (10, 5555, 'south' , '2016-08-21 09:44:30.847') insert into @table values (11, 5555, 'south' , '2016-08-27 09:44:30.847') insert into @table values (12, 6666, 'south' , '2016-08-10 09:44:30.847') insert into @table values (13, 6666, 'north' , '2016-08-21 09:44:30.847') insert into @table values (14, 6666, 'south' , '2016-08-09 09:44:30.847') Now I want to find duplicates with 'portnumber' should be same and 'sourceport' should be different. if 'portnumber' same and 'sourceport' same it should not be duplicate. and also I need additional column which holds the Id of greatest 'sourcereportedDate' date
I want get output like below:
(4, 2222, 'north' , '2016-08-20 09:44:30.847',5) (5, 2222, 'north' , '2016-08-26 09:44:30.847','latest') (6, 2222, 'south' , '2016-08-22 09:44:30.847',5) (7, 3333, 'south' , '2016-08-10 09:44:30.847',8) (8, 3333, 'north' , '2016-08-12 09:44:30.847','latest') (12, 6666, 'south' , '2016-08-10 09:44:30.847',13) (13, 6666, 'north' , '2016-08-21 09:44:30.847','latest') (14, 6666, 'south' , '2016-08-09 09:44:30.847',13) Thanks in advance.