IF object_id('tempdb..#cu') is not null DROP table #cu;;with a1 (zd1,zd2,zd3) as ( select 'a','b','c' union all select 'a','b','c' union all select 'a','b','c' union all select 'a1','b1','c1' ) select * into #cu from a1;with a1 as ( select *,ROW_NUMBER() OVER(PARTITION BY zd1,zd2,zd3 ORDER BY GETDATE()) re from #cu ) delete a1 where re>1select * from #cu
IF object_id('tempdb..#cu') is not null
DROP table #cu;;with a1 (zd1,zd2,zd3) as
(
select 'a','b','c' union all
select 'a','b','c' union all
select 'a','b','c' union all
select 'a1','b1','c1'
)
select * into #cu from a1;with a1 as
(
select *,ROW_NUMBER() OVER(PARTITION BY zd1,zd2,zd3 ORDER BY GETDATE()) re
from #cu
)
delete a1 where re>1select * from #cu