
正文
sqlserver中重复,sqlserver查询重复数据
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何使用sql语句在sqlserver中删除重复数据
题主可 参考下列例句:
删除表t1字段col1有重复的记录
delete from t1 where exists
(select 1 from (select col1 from t1 group by col1 having count(1)1) t where t.col1=t1.col1);
如果希望对于有重复的记录希望保留其中一条记录而不是全部删除,则可以运行下列语句,前提是数据表必须含有自增id列。
delete from t1 where exists
(select 1 from (select col1,max(id) as id from t1 group by col1 having count(1)1) t where t.col1=t1.col1 and t.idt1.id);
相关问答
Q1: SQLserver数据库中所有字段全部一样的重复数据如何删除?
找到最大的rowid即可。
Sql代码:
alter proc getNotDupData
as
--clear temp table
delete ODS.dbo.Agent
delete from stage.dbo.tmpDup
delete from stage.dbo.tmpRowNo
delete from stage.dbo.tmpMaxRowNo
--create dup table
insert into stage.dbo.tmpDup
select distinct AgentLogin,AgentSurName,AgentGivenName from stage.dbo.dAgentPerformanceStat
where AgentSurname is not null and agentlogin like '3%' order by AgentLogin
--add rowNo
insert into tmpRowNo
select *,ROW_NUMBER()over(order by AgentLogin) as rowno from tmpDup
--get max rowno
insert into stage.dbo.tmpMaxRowNo
select max(rowno) as 'rowno' from stage.dbo.tmpRowNo group by AgentLogin having count(*)1
--remove max rowno
delete from stage.dbo.tmpRowNo where rowno in (select * from stage.dbo.tmpMaxRowNo)
--insert into ods
insert into ODS.dbo.Agent select AgentLogin,AgentSurName,AgentGivenName from stage.dbo.tmpRowNo
Q2: sqlserver怎么去掉重复的数据
select distinct * into #tmp from s2
drop table s2
select * into s2 from #tmp
drop table #tmp
利用临时表删除重复数据







