选择另一列中具有相同ID但具有相同值的行

时间:2020-10-11 08:41:08

标签: sql

我的数据如下表所示

sendrecv

我应该只得到9414363,因为它具有batch_source列中的所有值都包含RFF。尽管9010144具有包含RFF的列,但不应将其输出,因为它也具有其他值。

尝试计数(distinct(batch_source)),但是问题是我可以获取其他account_id,其所有列值都为“ AWS”,符合以下条件。我需要在batch_source列中所有值都包含RFF的行

Account_id Batch_source
_________________________
9010144 AWS-RFF    
9010144 AWS    
9010144 AWS    
9010144 AWS    
9010144 AWS    
9010144 AWS    
9010144 AWS    
9414363 AWS-RFF    
9414363 AWS-RFF    
9414363 AWS-RFF    
9414363 AWS-RFF    
9414363 AWS-RFF    
9414363 AWS-RFF

2 个答案:

答案 0 :(得分:0)

我在下面编辑了您的查询,并对其进行了更新,以使其返回预期结果。

 SELECT account_id, max(batch_source) AS id_group
 FROM MyTable group by account_id 
 having count(distinct(batch_source)) = 1 ;

答案 1 :(得分:0)

我建议这样写:

select account_id, batch_source AS id_group
from MyTable
group by batch_source,account_id 
having min(batch_source) = max(batch_source) and
       min(batch_source) like '%-RFF';

如果您想确保RFF后缀,但不关心前缀,请计算未命中次数并确保为零:

having sum(case when batch_source not like '%-RFF' then 1 else 0 end) = 0

= 0说该组中的所有行都有RFF后缀。

相关问题