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怎么写谢谢

解决方案 »

  1.   

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

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

  3.   

    create table t1(id int identity,visNum nvarchar(10),aNum nvarchar(10),sContent nvarchar(10))
    insert into t1
    select 'A01','001','SH' union all
    select 'A01','001','男' union all
    select 'A02','002','BJ' union all
    select 'A02','002','男' union all
    select 'A03','003','SH' union all
    select 'A03','003','男'
    go--下边这句可以找出符合你要求的记录的问卷编号(aNum),之后你可以进行其他的操作
    select aNum
    from t1 where sContent = 'SH' or sContent = '男' 
    group by aNum having count(*) = 2
    order by aNum
      

  4.   

    select A.访问员编号,A.问卷编号,A.内容,B.内容
    from (select * from T where 内容='SH') AS A
          inner join (select * from T where 内容='男') AS B
            on A.问卷编号=B.问卷编号
      

  5.   

    可以直接用關聯寫,不用子查詢
    Select Distinct A.问卷编号
    From 表 A
    Inner Join 表 B
    On A.问卷编号 = B.问卷编号
    Where A.内容 = 'SH' And B.内容 = '男'
      

  6.   

    或者
    Select Distinct A.问卷编号
    From 表 A, 表 B
    Where A.问卷编号 = B.问卷编号
    And A.内容 = 'SH' And B.内容 = '男'