简体   繁体   English

用于查找每个组中成员相同的记录的 SQL 查询

[英]SQL query for finding records where members within each group are the same

I am sorry for the basic question.我很抱歉基本问题。 I have a table with two columns and I would like to extract Gene with Class = M (my expected output below).我有一个包含两列的表格,我想提取 Class = M 的 Gene(我的预期输出如下)。 I tried where Class = 'P' and Group by Gene, but still I have both Gene a and b as outputs.我尝试过 where Class = 'P' 和 Group by Gene,但仍然有 Gene a 和 b 作为输出。 Can you help me please?你能帮我吗?

Gene    Class
a   m
a   m
a   m
b   p   
b   m
b   p
c   p
c   p   
c   p

This is what I expect to have..这是我期望的..

Gene    Class
a   m

Thank you very much in advance!非常感谢您提前!

You can use aggregation and having if class always has to have the same value:您可以使用聚合并having if class始终必须具有相同的值:

select gene, min(class)
from t
group by gene
having min(class) = max(class) and min(class) = 'm';

Here is a db<>fiddle. 是一个 db<>fiddle。

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

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