您可以通过添加一个来做到这一点WHERE
子句将返回行US
or CN
:
select distinct Masterid
from yourtable
where cc in ('US', 'CN')
and NLCLA = 1
AND NLDES = 1
See SQL 摆弄演示 http://sqlfiddle.com/#!3/4758e/4
如果您希望结果同时包含CN
and US
,那么你可以使用:
select Masterid
from yourtable
where cc in ('US', 'CN')
and NLCLA = 1
AND NLDES = 1
group by masterid
having count(distinct cc) = 2
See SQL 摆弄演示 http://sqlfiddle.com/#!3/968a1/2.
另一种方法是使用EXISTS
获取 MasterId 列表US
and CN
。然后将其他过滤器放入WHERE
子句而不是在子查询中。
select distinct masterid
from yourtable t1
where exists (select Masterid
from yourtable t2
where cc in ('US', 'CN')
and t1.masterid = t2.masterid
group by masterid
having count(distinct cc) = 2)
and NLCLA = 1
and NLDES = 1;
See SQL 摆弄演示 http://sqlfiddle.com/#!3/968a1/9