SQL -- 多行,一行中的相似值,不需要显示特定值



这是问题所在:表名 = a

1       2     3      
123    1      A
123    1      A
123    2      A
332    1      A
332    1      A
321    2      B
321    2      A
321    1      A

到目前为止,我拥有的是:

select distinct 1,2,3 from a where a.2='1' and a.3='B';

它返回的是每个结果(321 除外)。

我只想选择第 1 列的值,只要该值不在第 2 列中有 2 或第 3 列中有 B 的行中。这可能吗?

"不在第 2 列中有 2 或第 3 列中有 B 的行中"可以表示为

select distinct 1,2,3 from a where a.2!='2' or a.3!='B';

select distinct 1,2,3 from a where a.2 <> '2' or a.3 <> 'B';

我会使用group byhaving

select col1
from t
group by col1
having sum(case when col2 = 2 then 1 else 0 end) = 0 and
       sum(case when col3 = 'B' then 1 else 0 end)  = 0;

相关内容

最新更新