我有一张大桌子(130列)。它是按月分隔的月度数据集(jan,feb,mar,...)。每个月我都会得到一小组重复的行。我想删除其中一行,删除哪一行无关紧要。
当我只选择要过滤重复的ID时,此查询似乎工作正常,但当我从表中选择所有"*"
时,我最终会包含所有行,包括重复项。我的目标是过滤掉dup并将结果集插入到新表中。
SELECT DISTINCT a.[ID]
FROM MonthlyLoan a
JOIN (SELECT COUNT(*) as Count, b.[ID]
FROM MonthlyLoan b
GROUP BY b.[ID])
AS b ON a.[ID] = b.[ID]
WHERE b.Count > 1
and effectiveDate = '01/31/2017'
任何帮助将不胜感激。
答案 0 :(得分:1)
这将显示每个ID的所有重复项:
;WITH Duplicates AS
(
SELECT ID
rn = ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID)
FROM MonthlyLoan
)
SELECT ID,
rn
FROM Duplicates
WHERE rn > 1
或者,您可以设置rn = 2
以查找每个ID的立即重复。
答案 1 :(得分:0)
由于你的身份证被重复(一个DUPPED ID !!!!)
您只需要在聚合中使用HAVING
子句。
请参阅以下示例。
declare @tableA as table
(
ID int not null
)
insert into @tableA
values
(1),(2),(2),(3),(3),(3),(4),(5)
select ID, COUNT(*) as [Count]
from @tableA
group by ID
having COUNT(*) > 1
结果:
ID Count
----------- -----------
2 2
3 3
将结果插入#Temporary表:
select ID, COUNT(*) as [Count]
into #temp
from @tableA
group by ID
having COUNT(*) > 1
select * from #temp