Oracle SQL Query用于标识与多个帐户ID关联的唯一电子邮件地址

时间:2017-04-26 15:12:34

标签: sql oracle

还在学习SQL,我在创建一个会返回电子邮件地址列表的查询时遇到了一些问题。电子邮件与多个帐户关联时的帐户ID。

服务器是Oracle SQL,我正在一张桌子上工作," EMAIL"和两个领域" UID& " EMAIL_ADDRESS"

我只想在多个帐户ID上列出相同的电子邮件时包含电子邮件和帐号。帐户ID可以包含多个电子邮件,同一个电子邮件可以在同一帐户ID上多次列出。那没问题。所以,我想排除那些我遇到麻烦的结果。

我有一个查询,当前与多个帐户相关联时,会返回与电子邮件关联的帐户ID的数量。但是,我无法将其转换为可以查看帐户ID号的列表。

我目前使用的查询是

SELECT COUNT(EMAIL.UID), EMAIL.EMAIL_ADDRESS
FROM EMAIL
GROUP BY EMAIL.EMAIL_ADDRESS 
HAVING COUNT(DISTINCT EMAIL.UID) > 1

返回如下列表:

Count of UID      EMAIL_ADDRESS
2                 asmith@...
3                 bjones@...
5                 cbaker@...

我需要的是:

UID               EMAIL_ADDRESS
67890             asmith@...
75236             asmith@...
15689             bjones@...
56748             bjones@...
89523             bjones@...
98542             cbaker@...
00245             cbaker@...
etc, etc          etc, etc

我还需要通过电子邮件地址订购,以便将相同的电子邮件列在一起。

感谢任何帮助!

谢谢, 麦克

4 个答案:

答案 0 :(得分:0)

试试这个:

内部查询会为那些EMAIL_ADDRESS个人提供至少两个或更多不同UI_D的内容。然后简单地将这些EMAIL_ADDRESS加入整个表格。

SELECT EMAIL.* FROM EMAIL INNER JOIN (
      select  EMAIL_ADDRESS from EMAIL
      group by  EMAIL_ADDRESS
      having min(UI_D) <> max(UI_D)
) TT
on EMAIL.EMAIL_ADDRESS = TT.EMAIL_ADDRESS
order by EMAIL.EMAIL_ADDRESS, EMAIL.UI_D

答案 1 :(得分:0)

在这里,我使用count(*)分析函数来提供内部查询或视图中具有相同地址的记录计数。然后,我用一个查询来包装它,该查询仅过滤计数为&gt;的行。 1。

select email_id, email_address from 
        (with email (id, email_address) as
         (select 67890 as id, 'asmith@...' as email_address from dual union all
          select 75236, 'asmith@...' from dual union all
          select 15689, 'bjones@...' from dual union all
          select 56748, 'bjones@...' from dual union all
          select 89523, 'bjones@...' from dual union all
          select 98542, 'cbaker@...' from dual union all
          select 00245, 'cbaker@...' from dual union all
          select 54321, 'unique1@...' from dual union all
          select 65432, 'unique2@...' from dual)
    select to_char(id,'00009') as email_id, email_address,
           count(*) over (partition by email_address) as c
 from email) email_data
 where email_data.c > 1
 order by email_data.email_address;

答案 2 :(得分:0)

这样的事情会起作用吗?

选择不同的uid,电子邮件 从电子邮件 通过电子邮件订购;

答案 3 :(得分:0)

使用分析函数执行此操作非常简单:

0.0000001

例如,如果您运行此查询,请参阅以下数据:

select ID, EMAIL_ADDRESS from 
  (select ID, EMAIL_ADDRESS, count(*) over (partition by EMAIL_ADDRESS) count from EMAIL) 
where count > 1 order by EMAIL_ADDRESS;

结果是:

create table EMAIL (ID number primary key, EMAIL_ADDRESS varchar2(255));
insert into EMAIL values (1, 'test@test.de');
insert into EMAIL values (2, 'test@test.de');
insert into EMAIL values (3, 'test2@test.de');