日期:2014-05-16 浏览次数:20633 次
--drop table tb
create table tb(A varchar(10),B int)
insert into tb
select '张三', 1001 union all
select '张三', 1002 union all
select '李四', 1001 union all
select '李四', 1001 union all
select '王五', 1003 union all
select '王五', 1006 union all
select '赵六', 1003 union all
select '赵六', 1003
go
select *
from tb
where a in (select A from tb group by a having COUNT(B) = COUNT(distinct B))
/*
A B
张三 1001
张三 1002
王五 1003
王五 1006
*/
create table test (A nvarchar(10),B int)
insert into test
select '张三',1002 union all
select '李四',1001 union all
select '李四',1001 union all
select '王五',1003 union all
select '王五',1006 union all
select '赵六',1003 union all
select '赵六',1003
select x.A,x.B
from (
select A,B,[COUNT]=COUNT(*)
from test
group by A,B
)x
where x.COUNT=1
/*
张三 1002
王五 1003
王五 1006
*/
create table xh
(列A varchar(10),列B int)
insert into xh
select '张三',1001 union all
select '张三',1002 union all
select '李四',1001 union all
select '李四',1001 union all
select '王五',1003 union all
select '王五',1006 union all
select '赵六',1003 union all
select '赵六',1003
select * from xh a
where exists
(select 1 from xh b
where b.列A=a.列A and b.列B<>a.列B)
/*
列A 列B
---------- -----------
张三 1001
张三 1002
王五 1003
王五 1006
(4 row(s) affected)
*/