繁体   English   中英

SQL DB2:需要 select 属于客户的所有记录在 B 列中具有唯一值(值 = P)

[英]SQL DB2: Need to select all records belonging to a client having a unique value in column B (value = P)

需要 select 属于客户的所有记录在 B 列中具有唯一值(值 = P)

这是我的 DB2 表:

Name      Column B

David         P
David         P
Stacy         A
Stacy         A
Curry         A
Curry         P
Curry         P
Kevin         P
Kevin         P

预期结果:

 Name Column B David P David P Kevin P Kevin P

如果 ColumnB 中没有任何空值,您可以使用NOT EXISTS

select t.* from tablename t
where not exists (select 1 from tablename where Name = t.Name and ColumnB <> 'P')  

请参阅演示
结果:

>  NAME | COLUMNB
> ----: | ------:
> David |       P
> David |       P
> Kevin |       P
> Kevin |       P

根据您的问题,您似乎想要:

select t.*
from t
where t.b = 'P' and
      not exists (select 1 from t t2 where t2.name = t.name and t2.b <> 'P');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM