根据条件查找重复项,并仅保留具有最新时间戳的记录

时间:2014-01-29 16:48:45

标签: sql sql-server-2008

我的情况是我们收到了许多重复记录。理想情况下,order_nr对于每条记录应该是唯一的。但对于所有记录而言并非如此,但很少 通过比较 serno1,serno2,model,branch,servicedate 字段,我们仍然可以获得匹配的记录。通过这种方式查找所有重复项之后,我们只需要选择具有最新时间戳的记录导入字段。

数据库: SQL SERVER 2008

rawserviceaccum model,serno1,serno2,model,branch,servicedate,import

找到重复项的第1部分将是自我加入。我能够将所有重复项都放入tmp表中。但是在两个记录之间,我如何只保留记录列表中具有最大时间戳记,即max(导入)的记录。我知道必须使用Aggregates和group by,但我无法为此成功编写任何sql脚本。

如果您认为还有其他方法可以解决此问题。请建议。

任何帮助表示赞赏,非常感谢。

select * into #tmp from rawserviceaccum s
join(
select serno1 as serial1,serno2 as serial2,serviceDate as servicedate,
model as modelname ,branch as branchname,COUNT(*) as count 
from rawserviceaccum 
where company_code = 'abc' and period ='201401' 
group by serno1,serno2,serviceDate,model,branch
having COUNT(*) > 1) d
on s.serno1=d.serial1 and s.SERNO2 = d.serial2 and 
s.servicedate= d.servicedate and s.MODEL =d.modelname
and s.branch =d.branchname 
where s.COMPANY_CODE= 'abc' and s.period = '201401'                                             The table has around 20 more columns but I want to group by only the above mentioned columns to filter out any duplicates and delete records where imported date is not the latest. The table also has non duplicate records which should continue to exist. 

我很满意这个问题。帮助急需。感谢。

0 个答案:

没有答案