返回共享电子邮件地址但具有不同地址,名字和姓氏的所有记录

时间:2016-12-13 14:06:18

标签: sql oracle

有一个带有记录的oracle表,其中一些记录共享电子邮件地址但具有不同的名字,姓氏或地址。如何只获取共享电子邮件地址但其名字,姓氏或地址的记录。到目前为止我所拥有的:

SELECT unique_id, firstname, lastname, email ,address
FROM customers
WHERE email in (SELECT email
                FROM customers
                GROUP BY email
                HAVING COUNT(email) > 1)

这会返回一个记录列表sharings email但在某些行中firstname,lastname和address是相同的

2 个答案:

答案 0 :(得分:0)

您应检查姓名,姓氏和地址的不同计数,以查看其中是否有> 1。

SELECT unique_id,firstname,lastname,email,address
FROM customers 
WHERE email in (SELECT email 
                FROM customers 
                GROUP BY email 
                HAVING COUNT(DISTINCT FIRSTNAME) > 1
                OR COUNT(DISTINCT LASTNAME) > 1
                OR COUNT(DISTINCT ADDRESS) > 1) 

答案 1 :(得分:0)

要在单个表扫描中执行此操作,您可以使用分析函数:

SELECT unique_id,
       firstname,
       lastname,
       email,
       address
FROM   (
  SELECT unique_id,
         firstname,
         lastname,
         email,
         address,
         COUNT( DISTINCT firstname ) OVER ( PARTITION BY email ) AS num_fn,
         COUNT( DISTINCT lastname  ) OVER ( PARTITION BY email ) AS num_ln,
         COUNT( DISTINCT address   ) OVER ( PARTITION BY email ) AS num_ad
  FROM customers
)
WHERE    num_fn > 1 OR num_ln > 1 OR num_ad > 1