Есть несколько способов, которые вы можете получить результат.
Использование предложения HAVING с агрегатной функцией:
select personid, race
from yourtable
where personid in
(select personid
from yourtable
group by personid
having
sum(case when race = 'white' then 1 else 0 end) > 0
and sum(case when race = 'asian' then 1 else 0 end) > 0
and sum(case when race not in('asian', 'white') then 1 else 0 end) = 0);
См SQL Fiddle with Demo
Или вы можете использовать count(distinct race)
в имеющей п:
;with cte as
(
select personid
from yourtable
where race in ('white', 'asian')
and personid not in (select personid
from yourtable
where race not in ('white', 'asian'))
group by personid
having count(distinct race) = 2
)
select personid, race
from yourtable
where personid in (select personid
from cte);
См SQL Fiddle with Demo
Спасибо! Мне нравится второе решение, так как это выглядит, чтобы легко вставлять значения для гонки и подсчитывать через некоторые входные параметры. – gooseman