8

I have a table in MS Access 2003 that looks like this:

    *url            id*
    example.com    red
    example.com    blue
    example.com    blue
    other.com      red
    other.com      orange
    more.com       blue

For each URL I want to know how many unique id there are. so in this case the result should be:

    *url             count of distinct id*
    example.com          2           (because red and blue are the only values)
    other.com            2
    more.com             1

This is very similar to SQL query to count number of different values except the solution in that case does not work because it relies on COUNT DISTINCT, but that is not supported in Access. I have tried to look up alternative ways of doing count distinct in Access but I'm afraid I don't understand the answers.

So I guess this problem can be summarized as "how to simulate count distinct in msaccess".

I would greatly appreciate it if someone can give me a hint.

2
  • 1
    +1 For a well phrased question (and searching the archives for answers first! :). Commented Dec 13, 2012 at 1:49
  • Good question, I was struggling with this exact problem. Commented Dec 13, 2012 at 13:54

2 Answers 2

5

This should work in MS Access 2003 (I just tested it):

SELECT url, count(id) as CountOfId
FROM
(
   SELECT distinct id, url
   FROM yourtable
) x
GROUP BY url

In this you get the distinct id and url in a subquery and then you count that result.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much, that does work. I removed the "x" which I believe is a typo (?) and it does the job great. I appreciate your help.
The x was an alias for the subquery. I am used to SQL server which requires it, but it did work in access with the alias
1

Here is another way of doing it without using the distinct keyword. (Although it looks like later versions of access would support it):

SELECT t.url, Count(*) AS distinct_id_count
FROM
(
    SELECT url
    FROM *source_table_name_here*
    GROUP BY url, id
) AS t
GROUP BY t.url
ORDER BY COUNT(*) DESC, t.url;

1 Comment

Thank you, I will try this if I have any issues with the previous solution.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.