English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
--Elaborazione di record duplicati della tabella (ricerca e eliminazione)
/******************************************************************************************************************************************************
1、Registri duplicati con Num e Nome uguali, senza relazione di grandezza, mantenere solo uno
2、Quando il nome è uguale, mantenere uno dei record con ID più grande o più piccolo
Curatore: Chinese Style (Roy)
Data: 2008.06.06
******************************************************************************************************************************************************/
--1、Utilizzato per cercare record duplicati di elaborazione (se le colonne non hanno una relazione di grandezza, utilizzare la generazione di colonne auto-incrementabili e tabelle temporanee in SQL2000, utilizzare la funzione row_number in SQL2005)
-- --> (Roy) Genera dati di test
se non object_id('Tempdb..#T') è nullo elimina la tabella #T Esegui Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2)) inserisci #T select 1, 'A', 'A1' union all select 2, 'A', 'A2' union all select 3, 'A', 'A3' union all select 4, N'B', N'B1' union all select 5, N'B', N'B2' Esegui
--I、Il record con il minimo ID per nome uguale (raccomandato usare 1,2,3), metodo 3 in SQL05 è più efficiente rispetto a 1,2
Metodo 1:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID<a.ID)
Metodo 2:
select a.* from #T a join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID
Metodo 3:
select * from #T a where ID=(select min(ID) from #T where Name=a.Name)
方法4:
select a.* from #T a join #T b on a.Name=b.Name and a.ID>=b.ID group by a.ID,a.Name,a.Memo having count(1)=1
Metodo 5:
select * from #T a group by ID,Name,Memo having ID=(select min(ID)from #T where Name=a.Name)
Metodo 6:
select * from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)=0
Metodo 7:
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID)
方法8:
select * from #T a where ID!>all(select ID from #T where Name=a.Name)
方法9(注:ID为唯一时可用):
select * from #T a where ID in(select min(ID) from #T group by Name)
-- SQL2005:
方法10:
select ID,Name,Memo from (select *,min(ID)over(partition by Name) as MinID from #T a)T where ID=MinID
方法11:
select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID) as MinID from #T a)T where MinID=1
生成结果:
/*
ID Name Memo
----------- ---- ----
1 A A1
4 B B1
(2 righe influenzate)
*/
--II、Name相同ID最大的记录,与min相反:
Metodo 1:
Select * from #T a where not exists(select 1 from #T where Name=a.Name and ID>a.ID)
Metodo 2:
select a.* from #T a join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID order by ID
Metodo 3:
select * from #T a where ID=(select max(ID) from #T where Name=a.Name) order by ID
方法4:
select a.* from #T a join #T b on a.Name=b.Name and a.ID<=b.ID group by a.ID,a.Name,a.Memo having count(1)=1
Metodo 5:
select * from #T a group by ID,Name,Memo having ID=(select max(ID)from #T where Name=a.Name)
Metodo 6:
select * from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)=0
Metodo 7:
select * from #T a where ID=(select top 1 ID from #T where Name=a.name order by ID desc)
方法8:
select * from #T a where ID!<all(select ID from #T where Name=a.Name)
方法9(注:ID为唯一时可用):
select * from #T a where ID in(select max(ID) from #T group by Name)
-- SQL2005:
方法10:
select ID,Name,Memo from (select *,max(ID)over(partition by Name) as MinID from #T a)T where ID=MinID
方法11:
select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID desc) as MinID from #T a)T where MinID=1
生成结果2:
/*
ID Name Memo
----------- ---- ----
3 A A3
5 B B2
(2 righe influenzate)
*/
-- 2、删除重复记录有大小关系时,保留大或小其中一个记录
-- --> (Roy) Genera dati di test
se non object_id('Tempdb..#T') è nullo
elimina la tabella #T
Esegui
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
inserisci #T
select 1, 'A', 'A1' union all
select 2, 'A', 'A2' union all
select 3, 'A', 'A3' union all
select 4, N'B', N'B1' union all
select 5, N'B', N'B2'
Esegui
--I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条
Metodo 1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID<a.ID)
Metodo 2:
delete a from #T a left join (select min(ID)ID, Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
Metodo 3:
delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)
Metodo 4 (nota: ID è unico):
delete a from #T a where ID not in(select min(ID)from #T group by Name)
Metodo 5:
delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0
Metodo 6:
delete a from #T a where ID>(select top 1 ID from #T where Name=a.name order by ID)
Metodo 7:
delete a from #T a where ID>any(select ID from #T where Name=a.Name)
select * from #T
生成结果:
/*
ID Name Memo
----------- ---- ----
1 A A1
4 B B1
(2 righe influenzate)
*/
--II、Name相同ID保留最大的一条记录:
Metodo 1:
delete a from #T a where exists(select 1 from #T where Name=a.Name and ID>a.ID)
Metodo 2:
delete a from #T a left join (select max(ID)ID, Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null
Metodo 3:
elimina a da #T a dove ID non è in (seleziona max(ID) da #T dove Name=a.Name)
Metodo 4 (nota: ID è unico):
elimina a da #T a dove ID non è in (seleziona max(ID) da #T raggruppato per Name)
Metodo 5:
elimina a da #T a dove (seleziona count(1) da #T dove Name=a.Name e ID>a.ID)>0
Metodo 6:
elimina a da #T a dove ID>>(seleziona top 1 ID da #T dove Name=a.name ordinato per ID decrescente)
Metodo 7:
elimina a da #T a dove ID<seleziona ID da #T dove Name=a.Name
select * from #T
/*
ID Name Memo
----------- ---- ----
3 A A3
5 B B2
(2 righe influenzate)
*/
--3. Gestisci i valori duplicati quando non ci sono relazioni di dimensione tra i record duplicati
-- --> (Roy) Genera dati di test
se non object_id('Tempdb..#T') è nullo
elimina la tabella #T
Esegui
crea una tabella #T([Num] int,[Name] nvarchar(1))
inserisci #T
seleziona 1, N'A' union all
seleziona 1, N'A' union all
seleziona 1, N'A' union all
seleziona 2, N'B' union all
seleziona 2, N'B'
Esegui
Metodo 1:
se object_id('Tempdb..#') non è nullo
elimina la tabella #
seleziona distinct * in # da #T--Escludi le registrazioni duplicate e genera un elenco temporaneo #
svuota la tabella #T--Svuota la tabella
inserisci #T selezionando * da #--Inserisci la tabella temporanea #T nella tabella #T
--Visualizza i risultati
select * from #T
/*
Num Nome
----------- ----
1 A
2 B
(2 righe influenzate)
*/
--Esegui di nuovo i dati di test utilizzando il metodo 2
Metodo 2:
aggiungi una colonna identificativa ID int identity all'elenco delle tabelle #T--Aggiungi una colonna identificativa ID int identity all'elenco delle tabelle #T
go
delete a from #T a where exists(select 1 from #T where Num=a.Num and Name=a.Name and ID>a.ID)--Rimani con una sola registrazione
go
alter table #T drop column ID--Elimina la colonna di identificazione
--Visualizza i risultati
select * from #T
/*
Num Nome
----------- ----
1 A
2 B
(2 righe influenzate)
*/
--Esegui di nuovo i dati di test utilizzando il metodo 3
Metodo 3:
declare Roy_Cursor cursor local for
select count(1)-1,Num,Name from #T group by Num,Name having count(1)>1
declare @con int,@Num int,@Name nvarchar(1)
open Roy_Cursor
fetch next from Roy_Cursor into @con,@Num,@Name
while @@Fetch_status=0
begin
set rowcount @con;
delete #T where Num=@Num and Name=@Name
set rowcount 0;
fetch next from Roy_Cursor into @con,@Num,@Name
end
close Roy_Cursor
deallocate Roy_Cursor
--Visualizza i risultati
select * from #T
/*
Num Nome
----------- ----
1 A
2 B
(2 righe influenzate)