ID colA colB colC colD
1 1 a d abc
2 2 p o dfg
3 null a d ert
4 2 m m hh
5 z x c n
...
要求查询结果如下:
ID colA colB colC colD
3 null a d ert
4 2 m m hh
5 z x c n
...
colA,colB,colC 3列中只要有一列其内容相同即视为重复数据,查询所有记录时只取重复记录中ID值最大的那条
sql优化简单或其他方案也可,忘各位不吝赐教, 谢谢!
4 个解决方案
#1
--> 测试数据:#tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
create table #tb([ID] int,[colA] varchar(1),[colB] varchar(1),[colC] varchar(1),[colD] varchar(3))
insert #tb
select 1,'1','a','d','abc' union all
select 2,'2','p','o','dfg' union all
select 3,null,'a','d','ert' union all
select 4,'2','m','m','hh' union all
select 5,'z','x','c','n'
select * from #tb t
where not exists (select * from #tb where (colA=t.colA or colB=t.colB or colC = t.colC) and ID>t.ID)
#2
create table tb(ID int,colA varchar(10),colB varchar(10),colC varchar(10),colD varchar(10))
insert into tb values(1 ,'1' ,'a', 'd', 'abc')
insert into tb values(2 ,'2' ,'p', 'o', 'dfg')
insert into tb values(3 ,null ,'a', 'd', 'ert')
insert into tb values(4 ,'2' ,'m', 'm', 'hh')
insert into tb values(5 ,'z' ,'x', 'c', 'n')
go
select t.* from tb t where not exists(select 1 from tb where
(colA = t.colA or colA = t.colB or colA = t.colC or colB = t.colB or colB = t.colC or colc = t.colc) and id > t.id)
/*
ID colA colB colC colD
----------- ---------- ---------- ---------- ----------
3 NULL a d ert
4 2 m m hh
5 z x c n
(所影响的行数为 3 行)
*/
select t.* from tb t where id = (select max(id) from tb where
(colA = t.colA or colA = t.colB or colA = t.colC or colB = t.colB or colB = t.colC or colc = t.colc))
/*
ID colA colB colC colD
----------- ---------- ---------- ---------- ----------
3 NULL a d ert
4 2 m m hh
5 z x c n
(所影响的行数为 3 行)
*/
drop table tb
#3
OK,其实先测出了,没考虑到列为空而相同的情况,改改即是。结贴!
#4
学习了 只是想问下 2楼的 两天语句在数量多的情况下
是后者更好点吗?还是前者 速度快点?
是后者更好点吗?还是前者 速度快点?
#1
--> 测试数据:#tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
create table #tb([ID] int,[colA] varchar(1),[colB] varchar(1),[colC] varchar(1),[colD] varchar(3))
insert #tb
select 1,'1','a','d','abc' union all
select 2,'2','p','o','dfg' union all
select 3,null,'a','d','ert' union all
select 4,'2','m','m','hh' union all
select 5,'z','x','c','n'
select * from #tb t
where not exists (select * from #tb where (colA=t.colA or colB=t.colB or colC = t.colC) and ID>t.ID)
#2
create table tb(ID int,colA varchar(10),colB varchar(10),colC varchar(10),colD varchar(10))
insert into tb values(1 ,'1' ,'a', 'd', 'abc')
insert into tb values(2 ,'2' ,'p', 'o', 'dfg')
insert into tb values(3 ,null ,'a', 'd', 'ert')
insert into tb values(4 ,'2' ,'m', 'm', 'hh')
insert into tb values(5 ,'z' ,'x', 'c', 'n')
go
select t.* from tb t where not exists(select 1 from tb where
(colA = t.colA or colA = t.colB or colA = t.colC or colB = t.colB or colB = t.colC or colc = t.colc) and id > t.id)
/*
ID colA colB colC colD
----------- ---------- ---------- ---------- ----------
3 NULL a d ert
4 2 m m hh
5 z x c n
(所影响的行数为 3 行)
*/
select t.* from tb t where id = (select max(id) from tb where
(colA = t.colA or colA = t.colB or colA = t.colC or colB = t.colB or colB = t.colC or colc = t.colc))
/*
ID colA colB colC colD
----------- ---------- ---------- ---------- ----------
3 NULL a d ert
4 2 m m hh
5 z x c n
(所影响的行数为 3 行)
*/
drop table tb
#3
OK,其实先测出了,没考虑到列为空而相同的情况,改改即是。结贴!
#4
学习了 只是想问下 2楼的 两天语句在数量多的情况下
是后者更好点吗?还是前者 速度快点?
是后者更好点吗?还是前者 速度快点?