-1

I am having issues with writing a query that can look for unique counts / duplicates under certain conditions. I am trying to get counts at one time from a table that is similar to this one:

|-P_key-|-----email-----|-act_no-|--Client--|
|   1   | [email protected]  |    1   |   Jets   |
|   2   | [email protected]  |    2   |   Jets   |
|   3   | [email protected]  |  NULL  |   Jets   |
|   4   | [email protected]  |    1   |   Bills  |
|   5   | [email protected]  |    2   |   Bills  |
|   6   | [email protected]  |    2   |   Giants |
|   7   | [email protected]  |    2   |   Giants |
|   8   | [email protected]  |    5   |   Pats   |

What I need are the counts by client as below:

  1. count of total records for each client
  2. count of good records with duplicate emails across clients removed
  3. count of good records with duplicate account no's within client removed
  4. count of good records with duplicate account no's across clients removed
  5. count of good records with blank account no's within client removed

Where as any duplicates are removed after/from the total row counts.

So [email protected] and [email protected] from the Jets client is removed/not counted from the good after/from good after/from unique emails across projects because they exist on other clients, leaving 1 email address ([email protected]) that is from the Jets client that does not exist on the other clients.

Also for the within client, for the Giants client, acct_no 2 is the only acct_no, so I would not want to count either record, regardless if the acct_no 2 is on other clients(see how the acct_no are unique for all other clients within themselves). Whereas when looking for acct_nos across clients, for the Jets client, all 3 acct_no are unique within the client, but not across all of the clients (only the null value is unique across all clients). Therefore out of the 3 Jets acct_no's, 3 are good within the client, but only 1 is good across clients.

The out put I was hoping to achieve was was follows (albeit the final data doesn't necessarily need to be pivoted as such):

 |                                             |  Jets  |  Bills | Giants |  Pats |
 | Total emails                                |   3    |    2   |    2   |   1   |
 | good after unique emails across clients     |   1    |    0   |    1   |   1   |
 | good after unique account_no across clients |   1    |    0   |    0   |   1   |
 | good after unique account_no within clients |   3    |    2   |    0   |   1   |
 | good after blank account_no within clients  |   2    |    0   |    0   |   0   |

OR

 |        |  tot unique emails |  good emails        | etc...
 | Jets   |   3                |    1                |   
 |Bills   |   2                |    0                |   
 | Giants |   2                |    1                |    
 | Pats   |   1                |    1                |   

Also of note, if I wanted to add another layer than client, such as counts for agents within a client, so it would be adding another field to the group by on client, how would I approach that as well?

I was thinking one way would be to use a where not exists statement such as:

 SELECT COUNT(x.act_no)
 FROM x
 WHERE NOT EXISTS (SELECT 'x' FROM t WHERE t.act_no = x.act_no)
 AND x.client = 'Jets'

However, I wanted to be able to return all counts in one query, instead of piecing it out if possible.

Thanks for any and all help in advance!

2
  • Define "after". Note that the fact that ids are autogenerated does not guarantee order - especially if rows have been updated with new information later. It's best to consider the actual value of an id to be random (no inherent value). Your criteria is also slightly opaque - what have you tried so far, to help us see what you're going for? Mar 30, 2014 at 0:46
  • Thanks for the response, I have updated my question with an example of what I am trying and have hopefully better defined "after"
    – Yikes
    Mar 30, 2014 at 1:10

2 Answers 2

0

It is probably clearest to piece it together from multiple selects:

DECLARE @tbl TABLE (id INT, email VARCHAR(256), act_no INT NULL, client VARCHAR(10))
INSERT INTO @tbl VALUES
 (   1   , '[email protected]'  ,    1   ,   'Jets'   )
,(   2   , '[email protected]'  ,    2   ,   'Jets'   )
,(   3   , '[email protected]'  ,  NULL  ,   'Jets'   )
,(   4   , '[email protected]'  ,    1   ,   'Bills'  )
,(   5   , '[email protected]'  ,    2   ,   'Bills'  )
,(   6   , '[email protected]'  ,    2   ,   'Giants' )
,(   7   , '[email protected]'  ,    2   ,   'Giants' )
,(   8   , '[email protected]'  ,    5   ,   'Pats'   )     

SELECT 'Total emails',client, COUNT(*)
  FROM @tbl 
 GROUP BY client

SELECT 'good after unique emails across clients',client, COUNT(*)
  FROM @tbl a
 WHERE NOT EXISTS (SELECT 1 FROM @tbl b WHERE b.email=a.email AND b.client <> a.client)
 GROUP BY client

SELECT 'good after unique account_no across clients',client, COUNT(*)
  FROM @tbl a
 WHERE NOT EXISTS (SELECT 1 FROM @tbl b WHERE b.act_no=a.act_no AND b.client <> a.client)
 GROUP BY client

SELECT 'good after unique account_no within clients',client, COUNT(*)
  FROM @tbl a
 WHERE NOT EXISTS (SELECT 1 FROM @tbl b WHERE b.act_no=a.act_no AND b.client = a.client GROUP BY b.act_no,b.client HAVING COUNT(*) > 1)
 GROUP BY client

You can then UNION the results. You can also LEFT JOIN from a listing of clients to make sure the null counts appear.

However, you can also do it all at once fairly succinctly. Excuse the excessively long aliases.

;WITH cte AS (
       SELECT client
             ,CASE WHEN NOT EXISTS (SELECT 1 FROM @tbl b WHERE b.email=a.email AND b.client <> a.client) THEN 1 ELSE 0 END [good after unique emails across clients]
             ,CASE WHEN NOT EXISTS (SELECT 1 FROM @tbl b WHERE b.act_no=a.act_no AND b.client <> a.client) THEN 1 ELSE 0 END [good after unique account_no across clients]
             ,CASE WHEN NOT EXISTS (SELECT 1 FROM @tbl b WHERE b.act_no=a.act_no AND b.client = a.client GROUP BY b.act_no,b.client HAVING COUNT(*) > 1) THEN 1 ELSE 0 END [good after unique account_no within clients]
         FROM @tbl a
     )
     SELECT client
           ,COUNT(*) [Total emails]
           ,SUM([good after unique emails across clients]) [good after unique emails across clients]
           ,SUM([good after unique account_no across clients]) [good after unique account_no across clients]
           ,SUM([good after unique account_no within clients]) [good after unique account_no within clients]
      FROM cte a
     GROUP BY client

Note, I left the 'good after blank account_no within clients' off as I figure you can easily add that.

0

Not completely sure if I'm interpreting the aggregation rules correctly but here's what I came up with. It's a vertical result that you could pivot if you wanted it to be horizontal..

SELECT  Client, result, CountType
    FROM    (SELECT        Client, COUNT(*) AS result, 'Total emails' AS CountType
                FROM            ComplexCounting
                GROUP BY Client
                UNION
                SELECT        Client, COUNT(*) AS result, 'good after unique emails across clients' AS CountType
                FROM            ComplexCounting
                WHERE        (NOT EXISTS
                                            (SELECT        Id, email, act_no, Client
                                                FROM            ComplexCounting AS c
                                                WHERE        (Client <> ComplexCounting.Client) AND (email = ComplexCounting.email)))
                GROUP BY Client
                UNION
                SELECT        Client, COUNT(*) AS result, 'good after unique account_no across clients' AS CountType
                FROM            ComplexCounting
                WHERE        (NOT EXISTS
                                            (SELECT        Id, email, act_no, Client
                                                FROM            ComplexCounting AS c
                                                WHERE        (Client <> ComplexCounting.Client) AND (act_no = ComplexCounting.act_no)))
                GROUP BY Client
                UNION
                SELECT        Client, COUNT(*) AS result, 'good after unique account_no within clients' AS CountType
                FROM            ComplexCounting
                WHERE        (NOT EXISTS
                                            (SELECT        Id, email, act_no, Client
                                                FROM            ComplexCounting AS c
                                                WHERE        (Client = ComplexCounting.Client) AND (act_no = ComplexCounting.act_no) AND (Id <> ComplexCounting.Id)))
                GROUP BY Client
                UNION
                SELECT        Client, COUNT(DISTINCT act_no) AS result, 'good after blank account_no within clients' AS CountType
                FROM            ComplexCounting
                WHERE        EXISTS
                                            (SELECT        Id, email, act_no, Client
                                                FROM            ComplexCounting AS c
                                                WHERE        (act_no IS NULL) AND (Client = ComplexCounting.Client)) AND (act_no IS NOT NULL)
                GROUP BY Client) AS Results
    ORDER BY CountType

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.