Link to home
Start Free TrialLog in
Avatar of rporter45
rporter45

asked on

Access 2007 - How do I count unique ID's

I have a table with two columns of data.

Account ID
Contact ID

Please provide me with the SQL statement to use in Access 2007 to output a new table where each account ID appears only once and in the second column there is a column indicating the number of unique contact ID's for each account.
Avatar of GRayL
GRayL
Flag of Canada image

SELECT AccountID, Count(ContactID) as CountOfContacts FROM myTable GROUP BY AccountID;
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rporter45
rporter45

ASKER

This one provided the exact syntax.
Oops:

SELECT a.AccountID Count(a.ContactID) AS CountUniqContacts FROM (
SELECT DistinctROW AccountID, ContactID FROM myTable) AS a GROUP BY a.AccountID;
Well, if your current structure allows you to have multiple records with the same accountID and ContactID, then neither of the above will work.  You will first need to get DISTINCT combinations of AccountID and ContactID, so try:

SELECT T.[Account ID], Count(T.[Contact ID]) as UniqueContacts
FROM (SELECT DISTINCT [Account ID], [Contact ID] FROM your Table) as T
GROUP  BY T.[Account ID]
I believe you selected answer will count multiple ContactID's.
I have run tests and fyed's solution at http:#a33712457 is correct.  My solution in the previous post is not correct as I used DISTINCTROW instead of DISTINCT.