Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
POSTGRESQL仅选择至少有一个重复电话号码的重复帐户_Sql_Database_Postgresql - Fatal编程技术网

POSTGRESQL仅选择至少有一个重复电话号码的重复帐户

POSTGRESQL仅选择至少有一个重复电话号码的重复帐户,sql,database,postgresql,Sql,Database,Postgresql,我想获得以下示例数据(POSTGRESQL): 并消除所有: 不重复的电子邮件条目(示例中uuid=6) 包含重复电子邮件的行,但所有电话号码都不同 (在示例中,uuid=7,8,9) 曼坦: 具有重复电子邮件条目且所有电话号码相等的行之一(在示例中,uuid=4,5) 具有重复电子邮件条目且至少两个电话号码相等的行之一(在示例中,uuid=1,2,3) 结果数据将是 email phone_number a@gmail.com 111

我想获得以下示例数据(POSTGRESQL):

并消除所有:

  • 不重复的电子邮件条目(示例中uuid=6)
  • 包含重复电子邮件的行,但所有电话号码都不同 (在示例中,uuid=7,8,9)
  • 曼坦:

  • 具有重复电子邮件条目且所有电话号码相等的行之一(在示例中,uuid=4,5)
  • 具有重复电子邮件条目且至少两个电话号码相等的行之一(在示例中,uuid=1,2,3)
  • 结果数据将是

          email       phone_number        
       a@gmail.com        111
       b@gmail.com        222
    

    分组依据
    子句与
    具有

    select email, phone_number 
    from table t
    group by email, phone_number  
    having count(*) > 1;
    

    根据您的描述,您希望:

    select email, max(phone_number)
    from t
    group by email
    having count(*) > count(distinct phone_number) and
           count(*) > 1;
    

    到目前为止,您尝试了什么?我尝试过在电话和cpf相等时按电话和cpf进行过滤,但我损失了很多,因为当有2个重复帐户时,这一切正常,但如果有3个或更多,则不准确
    select email, max(phone_number)
    from t
    group by email
    having count(*) > count(distinct phone_number) and
           count(*) > 1;