如何删除A表中,A_1字段相同的记录,但还保留一条(A_Time字段时间最大的那一条)?
如何删除A表中,A_1字段相同的记录,但还保留一条(A_Time字段时间最大的那一条)?
------解决方案--------------------declare @t table(id int identity,A_1 nvarchar(50),
date_time datetime)
insert into @t select 'a ', '2007-03-02 ' union all
select 'a ', '2007-03-01 ' union all
select 'b ', '2007-03-01 ' union all
select 'b ', '2007-03-02 ' union all
select 'c ', '2007-03-03 '
select idd=identity(int,1,1),A_1,date_time into #t from @t order by date_time desc
--select * from #t
--select min([id]) from @t order date_time desc group by A_1
delete from #t where idd not in (select min([id]) from @t group by A_1)
select * from #t
drop table #t