sarpay
7/31/2015 - 3:02 PM

[sql] Finds email accounts that were entered more than once and their count.

[sql] Finds email accounts that were entered more than once and their count.

SELECT
[TotalCount],
[ExcessCount],
([TotalCount] - [ExcessCount]) [DistinctsCount]
FROM
(
  SELECT SUM([TotalCount]) AS [TotalCount]
  FROM 
  (
    SELECT COUNT(*) AS [TotalCount]
    FROM [Contacts]
    WHERE [Email] IS NOT NULL
    GROUP BY [Email]
    HAVING COUNT(*) > 1
  ) tt1
) t1,
(
  SELECT
  SUM([ExcessCount]) AS [ExcessCount]
  FROM 
  (
    SELECT COUNT(*) - 1 AS [ExcessCount]
    FROM [Contacts]
    WHERE [Email] IS NOT NULL
    GROUP BY [Email]
    HAVING COUNT(*) > 1
  ) tt2
) t2