关于查找重复记录
select * from T1 a
where (a.a,a.b) in (select a,b from T1 group by a,b having count(*) > 1)
查找重复记录,上面的语句错在哪? 有收获
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-11-27 14:03:25
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
--Dec 28 2012 20:23:12
--Copyright (c) Microsoft Corporation
--Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
--
----------------------------------------------------------------
--> 测试数据:[T1]
if object_id('[T1]') is not null drop table [T1]
go
create table [T1]([ID] int,[a] varchar(1),[b] varchar(1),[c] varchar(1))
insert [T1]
select 22,'q','w','e' union all
select 24,'q','w','e' union all
select 25,'q','w','e' union all
select 27,'s','d','f' union all
select 29,'a','s','d' union all
select 31,'a','s','d'
--------------开始查询--------------------------
select max(id) id ,a,b,c
from t1
group by a,b,c
----------------结果----------------------------
/*
id a b c
----------- ---- ---- ----
31 a s d
25 q w e
27 s d f
*/
create table t1(ID int, a varchar(10), b varchar(10), c varchar(10)
)
insert into T1
select 22 , 'q', 'w', 'e'
union all select 24 , 'q', 'w', 'e'
union all select 25 , 'q', 'w', 'e'
union all select 27 , 's', 'd', 'f'
union all select 29 , 'a', 's', 'd'
union all select 31 , 'a', 's', 'd'
delete t1
from t1
inner join
(
select a,b,c,min(ID) id
from T1
group by a,b,C
)tt
on tt.a = t1.a and
tt.b = t1.b and
tt.c = t1.c and
tt.id <> t1.id
select * from t1
/*
IDabc
22qwe
27sdf
29asd
*/