当我们按列 (C1) 分组时,选择列 (C1) 中缺少特定值 (V1) 的行 (C2)

  • 本文关键字:C1 V1 C2 的行 我们 选择 sql postgresql
  • 更新时间 :
  • 英文 :


我有一个这样的postgres表:

C1                          C2     
-------------------------------------
apple                      No Thorns
apple                      No Thorns
orange                     Thorns
apple                      No Thorns
pineapple                  No Thorns
pineapple                  Thorns
guava                      No Thorns
guava                      Thorns

现在我想要那些never在单个查询中(在 C2 中(的水果的名称(C1(。 如果这很微不足道,我提前道歉。

上面的答案是apple

我会使用group byhaving来解决这个问题:

select c1
from t
group by c1
having sum( (c2 = 'Thorns')::int) = 0;

我会尝试

Select distinct c1 from table1
Where not exists (
Select 1 from table1  t2 where table1.c1 = t2.c1 and t2.c2 = 'Thorns')

最新更新