sqlserver刪除重復
① sqlserver刪除重復記錄
delete from friend where id in (select id from
(select id,userid, row_number() over(partition by userid order by id) as rn from friend ) as a where rn > 1)
② SQLserver資料庫中所有欄位全部一樣的重復數據如何刪除
找到最大的rowid即可。
Sql代碼:
alterprocgetNotDupData
as
--cleartemptable
deleteODS.dbo.Agent
deletefromstage.dbo.tmpDup
deletefromstage.dbo.tmpRowNo
deletefromstage.dbo.tmpMaxRowNo
--createptable
insertintostage.dbo.tmpDup
selectdistinctAgentLogin,AgentSurName,AgentGivenNamefromstage.dbo.dAgentPerformanceStat
'3%'orderbyAgentLogin
--addrowNo
insertintotmpRowNo
select*,ROW_NUMBER()over(orderbyAgentLogin)asrownofromtmpDup
--getmaxrowno
insertintostage.dbo.tmpMaxRowNo
selectmax(rowno)as'rowno'fromstage.dbo.(*)>1
--removemaxrowno
deletefromstage.dbo.tmpRowNowhererownoin(select*fromstage.dbo.tmpMaxRowNo)
--insertintoods
insertintoODS.dbo.AgentselectAgentLogin,AgentSurName,AgentGivenNamefromstage.dbo.tmpRowNo
③ 菜鳥求教,sqlserver中刪除重復數據的sql語句怎麼寫
--查出重復的數據,通過distinct去重,保存到臨時表
select distinct * into #aaa from 表
where id in (select id from 表 group by having count(id) > 1)
--刪除實表中的重復數據
delete from 表
where id in (select id from 表 group by having count(id) > 1)
--將刪除掉的重復數據插入表中,保證表中只有一條,而沒有重復
insert into 表(列)
select 列 from #aaa
--如果所有重復數據,一條都不需要保留,直接刪除即可
④ sqlserver 去掉重復記錄
首先設定表tb_a 唯一關鍵欄位 xh,以及要查詢的重復欄位 mc 則查詢mc重復的sqlserver語句如下
select mc from tb_a where xh not in (select min(xh) xh from tb_a group by mc)
⑤ sql中刪除重復數據
SQL
Server刪除重復行是我們最常見的操作之一,下面就為您介紹六種適合不同情況的SQL
Server刪除重復行的方法,供您參考。
1.如果有ID欄位,就是具有唯一性的欄位
delect
table
where
id
not
in
(
select
max(id)
from
table
group
by
col1,col2,col3...
)
group
by
子句後跟的欄位就是你用來判斷重復的條件,如只有col1,那麼只要col1欄位內容相同即表示記錄相同。
2.
如果是判斷所有欄位也可以這樣
select
*
into
#aa
from
table
group
by
id1,id2,....
delete
table
insert
into
table
select
*
from
#aa
3.
沒有ID的情況
select
identity(int,1,1)
as
id,*
into
#temp
from
tabel
delect
#
where
id
not
in
(
select
max(id)
from
#
group
by
col1,col2,col3...)
delect
table
inset
into
table(...)
select
.....
from
#temp
4.
col1+','+col2+','...col5
聯合主鍵
select
*
from
table
where
col1+','+col2+','...col5
in
(
select
max(col1+','+col2+','...col5)
from
table
where
having
count(*)>1
group
by
col1,col2,col3,col4
)
group
by
子句後跟的欄位就是你用來判斷重復的條件,如只有col1,那麼只要col1欄位內容相同即表示記錄相同。
5.
select
identity(int,1,1)
as
id,*
into
#temp
from
tabel
select
*
from
#temp
where
id
in
(
select
max(id)
from
#emp
where
having
count(*)>1
group
by
col1,col2,col3...)
6.
select
distinct
*
into
#temp
from
tablename
delete
tablename
go
insert
tablename
select
*
from
#temp
Sqlclub
go
drop
table
#temp
以上就是SQL
Server刪除重復行的方法介紹。
⑥ SQL Server 2000 刪除重復記錄問題
你可以重新建立一個新表,然後將原表中的信息不重復的選出來,復制到新表中,再將原表刪除,就ok了。
⑦ sqlserver怎麼刪除重復數據
1、查找表中多餘的重復記錄,重復記錄是根據單個欄位(peopleId)來判斷
select
* from people
where peopleId in (select peopleId from
people group by peopleId having count(peopleId)
> 1)
2、刪除表中多餘的重復記錄,重復記錄是根據單個欄位(peopleId)來判斷,只留有rowid最小的記錄
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)
3、查找表中多餘的重復記錄(多個欄位)
select * from vitae a
where (a.peopleId,a.seq)
in (select peopleId,seq from vitae group by peopleId,seq having
count(*) > 1)
4、刪除表中多餘的重復記錄(多個欄位),只留有rowid最小的記錄
delete from vitae a
where
(a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from
vitae group by peopleId,seq having count(*)>1)
5、查找表中多餘的重復記錄(多個欄位),不包含rowid最小的記錄
select * from vitae a
where
(a.peopleId,a.seq) in (select peopleId,seq from vitae group by
peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from
vitae group by peopleId,seq having count(*)>1)
(二)
比方說
在A表中存在一個欄位「name」,
而且不同記錄之間的「name」值有可能會相同,
現在就是需要查詢出在該表中的各記錄之間,「name」值存在重復的項;
Select
Name,Count(*) From A Group By Name Having Count(*) > 1
如果還查性別也相同大則如下:
Select Name,sex,Count(*) From A Group By Name,sex Having
Count(*) > 1
⑧ sqlserver裡面怎麼刪除重復的記錄
新建一個表,然後用
select distinct id,其它欄位 into newtable FROM oldtable
舊表就扔了