日期:2014-05-19  浏览次数:20762 次

求一个 统计SQL
ID   访问员编号   问卷编号     内容
1         A01                 001           SH
2         A01                 001           男
3         A02                 002           BJ
4         A02                 002           男
5         A03                 003           SH
6         A03                 003           男

求Sql   得到   同时满足在一个   问卷编号中   内容为SH,并且等于男的记录数
比如   ID   =1   问卷编号001   内容是SH
          ID   =2   问卷编号001   内容男
          统计出来就是符合的一条记录

比如   ID   =5   卷编号003   内容SH
          ID   =6   卷编号003   内容男
统计出来就是符合的另外一条记录

所以根据上面的表记录
最终得到2条符合条件的记录,条件为(同一个问卷编号中=SH   并且=男)

这个sql怎么写

谢谢

------解决方案--------------------
create table T(ID varchar(100),访问员编号 varchar(100),问卷编号 varchar(100),内容 varchar(100))

insert into T select '1 ', 'A01 ', '001 ', 'SH '
insert into T select '2 ', 'A01 ', '001 ', '男 '
insert into T select '3 ', 'A02 ', '002 ', 'BJ '
insert into T select '4 ', 'A02 ', '002 ', '男 '
insert into T select '5 ', 'A03 ', '003 ', 'SH '
insert into T select '6 ', 'A03 ', '003 ', '男 '


--返回满足条件的记录数
select count(*)
from (select * from T where 内容= 'SH ') AS A
inner join
(select * from T where 内容= '男 ') as B on A.问卷编号=B.问卷编号


drop table T


/*

返回:2

*/
------解决方案--------------------
create table T(ID varchar(100),访问员编号 varchar(100),问卷编号 varchar(100),内容 varchar(100))

insert into T select '1 ', 'A01 ', '001 ', 'SH '
insert into T select '2 ', 'A01 ', '001 ', '男 '
insert into T select '3 ', 'A02 ', '002 ', 'BJ '
insert into T select '4 ', 'A02 ', '002 ', '男 '
insert into T select '5 ', 'A03 ', '003 ', 'SH '
insert into T select '6 ', 'A03 ', '003 ', '男 '


--返回满足条件的记录数
select count(*)
from (select * from T where 内容= 'SH ') AS A
inner join
(select * from T where 内容= '男 ') as B on A.问卷编号=B.问卷编号


--或

select count(*)
from T as A
where 内容= 'SH '
and exists (select * from T where 问卷编号=A.问卷编号 and 内容= '男 ')

drop table T


/*

返回:2

*/
------解决方案--------------------
create table t1(id int identity,visNum nvarchar(10),aNum nvarchar(10),sContent nvarchar(10))
insert into t1
select 'A01 ', '001 ', 'SH &#