我得到了從不同的源端系統找到重複的情況。在T-sql中查找具有不同值的重複項?
防爆:我有表如下圖所示:
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')
現在我想找到「portnumber
」副本應該是相同的,「sourceport
」應該是不同的。如果'portnumber
'和'sourceport
'相同,則不應該重複。 ,也是我需要additional column
持有最大的「sourcereportedDate
」日期
我想獲取輸出像下面的Id
:
(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)
在此先感謝。
謝謝你的答案。你們真棒。謝謝 – user3583912