34,871
社区成员
发帖
与我相关
我的任务
分享delete from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )> 1)
declare @tb table (id int,type int,date datetime)
insert into @tb select 1,1,'2008-01-14'
union all select 2,1,'2008-01-15'
union all select 3,1,'2008-01-16'
union all select 4,2,'2008-01-14'
union all select 5,2,'2008-01-15'
union all select 6,2,'2008-01-16'
select *
into #t
from @tb a
where not exists(select * from @tb where type=a.type and date>a.date)
select * from #tdeclare @tb table (id int,type int,date datetime)
insert into @tb select 1,1,'2008-01-14'
insert into @tb select 2,1,'2008-01-15'
insert into @tb select 3,1,'2008-01-16'
insert into @tb select 4,2,'2008-01-14'
insert into @tb select 5,2,'2008-01-15'
insert into @tb select 6,2,'2008-01-16'
delete @tb from @tb a
where exists(select 1 from @tb where type=a.type and date>a.date)
select * from @tbselect * into #
from tablename a
where not exists(select 1 from tablename where id = a.id and date > a.date)
truncate table tablename
insert tablename select * from #
drop table #select distinct * into # from tb
truncate table tb
insert into tb select * from #
drop table #