Results 1 to 4 of 4
  1. #1
    John_jones is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    2

    Query to count number of unique entries in a field for each value in another field

    Hi all
    First post so please be gentle. I'm new to access and trying to do what I assumed was a simple query but have hit a roadblock and not found any other solutions online. I have a table setup in access with the following format:
    Click image for larger version. 

Name:	Dataset_Structure.PNG 
Views:	10 
Size:	7.8 KB 
ID:	30484



    I am wanting to create a query which generates a table summarising how many sites each company is at. I.e. it will return something like this:

    Alpha - 2
    Bravo - 2
    Charlie - 2
    Delta - 2

    I have tried creating a query which is simply Counts the Site field but this counts the same site multiple times if it is repeated (I.e. Alpha returns a value of 4). My aim is to count each site only once.

    Does anyone have a simple way to achieve this in Access' Query Design?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Requires two queries.

    First query:

    SELECT Company, Site FROM table GROUP BY Company, Site; OR SELECT DISTINCT Company, Site FROM table;

    Second query:

    SELECT Company, Count(Site) AS CountSite FROM Query1 GROUP BY Company;

    All in one SQL:

    SELECT Company, Count(Site) AS CountSite FROM (SELECT DISTINCT Company, Site FROM table) AS Query1 GROUP BY Company;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    John_jones is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Sep 2017
    Posts
    2
    THanks June but I can't seem to get your SQL query to work.

    My table name is 'Licence'
    Company field name is actually 'Organisation_Name'
    Site is actually 'Site_ID'

    I have entered this:
    SELECT Licence.Organisation_Name, Count(Licence.Site_ID) AS CountSite_ID
    FROM (SELECT DISTINCT Licence.Organisation_Name, Licence.Site_ID FROM table) AS [Query1], Licence
    GROUP BY Licence.Organisation_Name;


    I get a Syntax Error in the FROM clause but it looks like it should be fine to me.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Remove Licence from the outer query because you are not aggregating the table, you are aggregating the inner query. Use your table name in the inner query.

    SELECT Organisation_Name, Count(Site_ID) AS CountSite_ID
    FROM (SELECT DISTINCT Organisation_Name, Site_ID FROM Licence) AS [Query1]
    GROUP BY Organisation_Name;
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 10-12-2016, 05:48 PM
  2. Replies: 5
    Last Post: 05-05-2016, 01:56 PM
  3. Replies: 8
    Last Post: 08-16-2012, 09:04 AM
  4. Replies: 8
    Last Post: 03-14-2012, 05:40 PM
  5. Query to group and count field entries
    By newtoAccess in forum Queries
    Replies: 4
    Last Post: 11-29-2010, 09:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums