在where语句中使用多个条件

时间:2018-04-09 17:50:12

标签: sql sas where

我正在尝试确定所有3个“触摸”中存在的人数(基于ID)并打开(打开= 1)所有3个电子邮件。我使用下面的代码,但它返回值0,我不相信是真的。所以,我不确定我是否在数据中正确表示了这一点。

proc sql;
    select count (ID) 
    from TableX 
    where (Touch='Touch1' and Open=1) and (Touch='Touch2' and Open=1) and (Touch='Touch3' and Open=1);
    quit;

谢谢!

1 个答案:

答案 0 :(得分:4)

您可以使用以下方式识别人员:

proc sql;
    select id
    from TableX 
    where Open = 1 and Touch in ('Touch1', 'Touch2', 'Touch3')
    group by id
    having count(distinct Touch) = 3;
    quit;

要计算它们,请使用子查询:

proc sql;
    select count(*)
    from (select id
          from TableX 
          where Open = 1 and Touch in ('Touch1', 'Touch2', 'Touch3')
          group by id
          having count(distinct Touch) = 3
         ) x
    quit;