日期:2014-05-16  浏览次数:20589 次

这个怎么写啊?
列A    列B
张三    1001
张三    1002
李四    1001
李四    1001
王五    1003
王五    1006
赵六    1003
赵六    1003
我要找出列A有对应多笔列B的数据,就是要找出张三,王五这样的数据,要怎么写啊!
------解决方案--------------------
试试这个:

--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)
*/