日期:2014-05-17 浏览次数:20631 次
--> 测试数据:#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)
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