Sql 在另一列中选择id相同但值相同的行

Sql 在另一列中选择id相同但值相同的行,sql,Sql,我的数据如下表所示 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-RF

我的数据如下表所示

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
我应该只得到9414363,因为它包含列batch_source contains RFF中的所有值。虽然9010144有一个包含RFF的列,但它不应该输入输出,因为它还有其他值

尝试计数(不同(批处理源)),但问题是我可以获得其他帐户id,其所有列值均为“AWS”,符合以下条件。我需要在batch_source列中包含RFF的所有值的行

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

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

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

我建议这样写:

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后缀。

您是否正在尝试查找只有一个批处理值的帐户?这也很有用,但与我的原始查询类似,在我的环境中,我无法配置like子句