如何编写查询以显示名称重复的客户的详细信息?

时间:2016-10-06 20:49:11

标签: sql oracle

我一直在寻找相当多的东西,似乎找不到符合我需要的解决方案。

我有一个包含许多客户的表,问题是此表中有许多重复项。

我已经能够显示具有重复记录的客户以及具有相同名称的表中有多少记录,但现在我试图将其拆分并显示所有信息,以便我们确认当我们查找时,客户是正确的。

我正在使用此代码:

SELECT COUNT(NAME), NAME
  FROM DEV.ALL_CUSTOMER
  GROUP BY NAME
  HAVING COUNT(NAME) > 1;

其结果显示如下:

COUNT(NAME) |       NAME
------------|-------------------
     3      | Smith, John
     2      | Doe, Jane
     2      | Doe, Joe
     2      | Smith, Jane

然后我添加了我需要的所有信息领域:

SELECT COUNT(NAME), NAME, TOTAL_PURCHASED, ADDRESS, CITY, STATE_PROV, POSTAL_CODE, COUNTRY, HOME_PHONE, WORK_PHONE, WORK_EXT, OTHER_PHONE, EMAIL_ADDRESS
  FROM DEV.ALL_CUSTOMER
  GROUP BY NAME, TOTAL_PURCHASED, ADDRESS, CITY, STATE_PROV, POSTAL_CODE, COUNTRY, HOME_PHONE, WORK_PHONE, WORK_EXT, OTHER_PHONE, EMAIL_ADDRESS
  HAVING COUNT(NAME) > 1;

但是仍然将它们分组并且不显示每条记录的信息:

COUNT(NAME) |    NAME     | TOTAL_PURCHASED | ADDRESS |  CITY ...
------------|-------------|-----------------|---------|------- ..
     3      | Smith, John |        0        | (null)  | (null) ..
     2      | Doe, Jane   |        0        | (null)  | (null) ..
     2      | Doe, Joe    |        0        | (null)  | (null) ..

但我知道五个人之一约翰·史密斯"客户购买了东西。

相反,我希望结果如下:

NAME        | TOTAL_PURCHASED | ADDRESS       |  CITY ...
------------|-----------------|---------------|------- ..
Smith, John |       250       | 123 Fake St.  | (null) ..
Smith, John |        0        | (null)        | Oshawa ..
Smith, John |       300       | (null)        | Toronto .
Doe, Jane   |        0        | (null)        | (null) ..
Doe, Jane   |       300       | 456 Fake St.  | Toronto .
Doe, Joe    |      11235      | (null)        | (null) ..
Doe, Joe    |        0        | (null)        | (null) ..

1 个答案:

答案 0 :(得分:3)

使用窗口功能:

SELECT c.*
FROM (SELECT c.*, COUNT(*) OVER (PARTITION BY NAME) as cnt
      FROM DEV.ALL_CUSTOMER c
     ) c
WHERE cnt > 1
ORDER BY NAME;

这将为您提供NAME上重复的行。