SQL:选择具有条件的行?

SQL:选择具有条件的行?,sql,Sql,假设我有下表: 我搜索的是: select count(id) where "colX is never 20 AND colY is never 31" 预期结果: 3 (= id numbers 5,7,8) 1 (= id number 2) 及 预期结果: 3 (= id numbers 5,7,8) 1 (= id number 2) 非常感谢您的帮助第一个: select count(distinct id) from mytable where id not in (s

假设我有下表:

我搜索的是:

select count(id) where "colX is never 20 AND colY is never 31"
预期结果:

3 (= id numbers 5,7,8)
1 (= id number 2)

预期结果:

3 (= id numbers 5,7,8)
1 (= id number 2)
非常感谢您的帮助第一个:

select count(distinct id)
from mytable
where id not in (select id from mytable where colX = 20 or colY = 31)
第二个:

select count(distinct id)
from mytable t1
join mytable t2 on t1.id = t2.id and t2.coly = 30
where t1.colx = 20    
第一个:

select count(distinct id)
from mytable
where id not in (select id from mytable where colX = 20 or colY = 31)
第二个:

select count(distinct id)
from mytable t1
join mytable t2 on t1.id = t2.id and t2.coly = 30
where t1.colx = 20    
这是“集合中的集合”子查询的示例。最好的方法是将聚合与
having
子句一起使用,因为这是最通用的方法。这将生成此类ID的列表:

select id
from t
group by id
having SUM(case when colX = 20 then 1 else 0 end) = 0 and  -- colX is never 20
       SUM(case when colY = 31 then 1 else 0 end) = 0      -- colY is never 31
您可以使用子查询计算数字:

select count(*)
from (select id
      from t
      group by id
      having SUM(case when colX = 20 then 1 else 0 end) = 0 and  -- colX is never 20
             SUM(case when colY = 31 then 1 else 0 end) = 0      -- colY is never 31
     ) s
对于第二种情况,您需要:

select count(*)
from (select id
      from t
      group by id
      having SUM(case when colX = 20 then 1 else 0 end) > 0 and  -- colX has at least one 20
             SUM(case when colY = 31 then 1 else 0 end) > 0      -- colY has at least one 31
     ) s
这是“集合中的集合”子查询的示例。最好的方法是将聚合与
having
子句一起使用,因为这是最通用的方法。这将生成此类ID的列表:

select id
from t
group by id
having SUM(case when colX = 20 then 1 else 0 end) = 0 and  -- colX is never 20
       SUM(case when colY = 31 then 1 else 0 end) = 0      -- colY is never 31
您可以使用子查询计算数字:

select count(*)
from (select id
      from t
      group by id
      having SUM(case when colX = 20 then 1 else 0 end) = 0 and  -- colX is never 20
             SUM(case when colY = 31 then 1 else 0 end) = 0      -- colY is never 31
     ) s
对于第二种情况,您需要:

select count(*)
from (select id
      from t
      group by id
      having SUM(case when colX = 20 then 1 else 0 end) > 0 and  -- colX has at least one 20
             SUM(case when colY = 31 then 1 else 0 end) > 0      -- colY has at least one 31
     ) s
给你:

Select COUNT(distinct ID)
from Test_Table1 A
WHERE NOT EXISTS ( SELECT 1 from Test_Table1 c 
        WHERE c.id = a.id 
        AND colX =20 AND coly= 31)

Select COUNT(distinct ID)
from Test_Table1 A
WHERE EXISTS ( SELECT 1 from Test_Table1 c 
        WHERE c.id = a.id 
        AND colX =20 AND coly= 31)
给你:

Select COUNT(distinct ID)
from Test_Table1 A
WHERE NOT EXISTS ( SELECT 1 from Test_Table1 c 
        WHERE c.id = a.id 
        AND colX =20 AND coly= 31)

Select COUNT(distinct ID)
from Test_Table1 A
WHERE EXISTS ( SELECT 1 from Test_Table1 c 
        WHERE c.id = a.id 
        AND colX =20 AND coly= 31)

? 这是家庭作业吗?这不是家庭作业。我在另一个select查询中尝试了select语句,但没有得到我想要的结果?这是家庭作业吗?这不是家庭作业。我在另一个select查询中尝试了select语句,但没有得到我想要的结果——对于第一种情况;对于第二种情况,将
=0
条件更改为
>0
。-对于第一种情况;第二次将
=0
条件更改为
>0
。我非常接近这一点,我不知道“distinct”。所以两个版本(你的和戈登的)都很好,我不知道该选哪一个accept@tombomOP至少询问了两个条件一次。@Anonymous选择了最优雅的一个。。。我的!:)不过,说真的,我的查询更优雅,因此更容易阅读,这使它们成为“更好的代码”。好的编码最重要的衡量标准之一是易读性,简洁和整洁直接导致易读性。@Bohemian是的,是的,一开始没有正确理解。但老实说,戈登的解决方案对我来说更容易阅读和理解:)无意冒犯…@tombom no take:)我一点也不介意。我承认我确实喜欢简洁而不是易读,但这只是因为我做这件事已经很久了,我仍然很清楚。我完全明白,另一种风格可能更适合更广泛的受众。我非常接近这一点,我不知道“独特”。所以两个版本(你的和戈登的)都很好,我不知道该选哪一个accept@tombomOP至少询问了两个条件一次。@Anonymous选择了最优雅的一个。。。我的!:)不过,说真的,我的查询更优雅,因此更容易阅读,这使它们成为“更好的代码”。好的编码最重要的衡量标准之一是易读性,简洁和整洁直接导致易读性。@Bohemian是的,是的,一开始没有正确理解。但老实说,戈登的解决方案对我来说更容易阅读和理解:)无意冒犯…@tombom no take:)我一点也不介意。我承认我确实喜欢简洁而不是易读,但这只是因为我做这件事已经很久了,我仍然很清楚。我完全明白,另一种风格可能更适合更广泛的受众。