一佳一

记录像1+1一样简洁的代码

导航

快速去重复

Posted on 2018-01-20 17:05  一佳一  阅读(162)  评论(0编辑  收藏  举报

use tb
go

set nocount on

create table #Tmp --创建临时表#Tmp
(
FCarNumber VARCHAR(50) ,
maxid int ,
num int
);

insert into #Tmp(FCarNumber,num,maxid)
select FCarNumber,COUNT(0) as num,Max(PID) as maxid from T_GPSCar with (nolock) group by FCarNumber having count(0)>1

delete T_GPSCar from
#Tmp p where p.FCarNumber=T_GPSCar.FCarNumber and T_GPSCar.PID<>p.maxid

Select COUNT(0) as num from #Tmp --查询临时表的数据
truncate table #Tmp --清空临时表的所有数据和约束
drop table #Tmp
set nocount off