Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在一列上选择仅匹配/和条件的记录_Sql_Sql Server - Fatal编程技术网

Sql 在一列上选择仅匹配/和条件的记录

Sql 在一列上选择仅匹配/和条件的记录,sql,sql-server,Sql,Sql Server,我希望能够选择只有亚洲人和白人的人。例如,在我的下表中,我只想检索Personid1和Personid2(期望的结果)的记录 表: 预期结果: 这应该可以 select * from table where personID in ( select personID from table group by personID having count(distinct race) = 2 and min(race) = 'Asian' and max(race) = 'White') 这应该可

我希望能够选择只有亚洲人和白人的人。例如,在我的下表中,我只想检索Personid1和Personid2(期望的结果)的记录

表:

预期结果:

这应该可以

select * from table where personID in (
select personID from table
group by personID having count(distinct race) = 2 and min(race) = 'Asian'
and max(race) = 'White')
这应该可以

select * from table where personID in (
select personID from table
group by personID having count(distinct race) = 2 and min(race) = 'Asian'
and max(race) = 'White')

有几种方法可以得到结果

将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);

或者您可以在having子句中使用
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);

请参见

有几种方法可以获得结果

将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);

或者您可以在having子句中使用
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);

参见

非常幼稚,但应该有效

select * from yourtable t1 join yourtable t2 on t1.id = t2.id where ((t1.race = 'Asian' and t2.race = 'White') OR (t1.race = 'White' and t2.race = 'Asian')) and t1.id not in (select id from yourtable where race not in ('Asian','white'));

很幼稚,但应该管用

select * from yourtable t1 join yourtable t2 on t1.id = t2.id where ((t1.race = 'Asian' and t2.race = 'White') OR (t1.race = 'White' and t2.race = 'Asian')) and t1.id not in (select id from yourtable where race not in ('Asian','white'));

这是可行的,但不是使用最小和最大限制我只使用两个种族?如果我想做相同的查询,但有3场或更多的比赛,该怎么办?(这绝对是我的情况)。@leo.t,在这种情况下,我建议你参考蓝脚的解决方案。这是可行的,但使用min和max不是限制我只使用两种比赛吗?如果我想做相同的查询,但有3场或更多的比赛,该怎么办?(这绝对是我的情况)。@leo.t,在这种情况下,我建议你参考蓝脚怪的解决方案谢谢!我喜欢第二个解决方案,因为它看起来可以通过一些输入参数轻松插入race和count的值。谢谢!我喜欢第二种解决方案,因为它看起来可以通过一些输入参数轻松插入race和count的值。