合并重复的行

时间:2017-10-30 03:06:04

标签: sql sql-server

我有一个Customer表,其中包含ID和Email字段。我已编写以下查询以使用相同的电子邮件返回所有重复的客户:

SELECT ID, Email 
FROM Customer a
WHERE EXISTS (SELECT  1
              FROM Customer b
              WHERE a.Email = b.Email
              GROUP BY Email
              HAVING COUNT(Email) = 2)
ORDER BY Email

这将返回如下所示的记录:

ID    Email
1     a@hotmail.com
2     a@hotmail.com
3     b@gmail.com
4     b@gmail.com

虽然这有效,但实际上我需要以下格式的数据:

ID1    Email1          ID2    Email2
1      a@hotmail.com   2      a@hotmail.com
3      b@gmail.com     4      b@gmail.com

实现这一目标的最佳方法是什么?

3 个答案:

答案 0 :(得分:1)

一种方法是条件聚合。 。 。假设您最多有两封电子邮件:

select max(case when seqnum = 1 then id end) as id_1,
       email as email_1,
       max(case when seqnum = 2 then id end) as id_2,
       email as email_2
from (select t.*, row_number() over (partition by email order by id) as seqnum
      from t
      ) t
group by email;

实际上,为什么不这样做:

select email, count(*) as num_dups, min(id) as id_1,
       (case when count(*) > 1 then max(id) end) as id_2
from t
group by email;

答案 1 :(得分:0)

您的布局假定您总共只能有2个重复项。

可能会在下面列出ID吗?

declare @Duplicates table (Email varchar(50), Customers varchar(100))
insert @Duplicates select Email, '' from Customer group by Email having count(*) > 1

UPDATE d
SET
    Customers= STUFF((  SELECT ','+ cast(ID as varchar(10)) 
                        FROM Customer c
                        WHERE c.Email = d.Email            
                        FOR XML PATH(''), TYPE).value('.','VARCHAR(max)'), 1, 1, '')
FROM @Duplicates AS d 

select * from @Duplicates 
order by Email

答案 2 :(得分:0)

尝试:

SELECT MIN(ID) ID, Email, MAX(ID) ID2, Email AS EMAIL2
FROM Customer GROUP BY Email

如果你想要HUNT COUNT(电子邮件)= 2,它就像这样

SELECT MIN(ID) ID, Email, MAX(ID) ID2, Email AS EMAIL2
FROM Customer GROUP BY Email
HAVING COUNT(Email) = 2