Results 1 to 2 of 2
  1. #1
    bryan0 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Connecticut
    Posts
    19

    Using Criteria to Count Unique Values

    Hello, I've searched for a couple of hours and am stuck. I have 3 tables, and need to count the unique values in one of them...

    Table: SUPPLIER
    SUPPLIER_ID SUPPLIER_NAME
    S-91 IBM
    S-92 Dell
    S-93 Lenovo
    S-94 Lenovo

    Table: POS
    SUPPLIER_ID SITE PO_NUMBER ORDER_VALUE
    S-91 Location A 451 $5
    S-92 Location B 452 $10
    S-93 Location C 453 $12
    S-93 Location B 454 $7
    S-93 Location D 455 $18
    S-92 Location D 456 $20
    S-93 Location C 457 $6
    S-94 Location C 458 $9

    Table: SITE
    SITE_DESCRIPTION COUNTRY
    Location A USA
    Location B Switzerland
    Location C Switzerland
    Location D Brazil

    I want to be able to use the SUPPLIER as a criteria, and count the number of unique Buying Locations being used for each country. For example, if I was looking at Lenovo, I'd want to see that Switerland is buying Lenovo products from 2 sites (Locations B and C) and Brazil is buying from 1 site (Location D):
    Switzerland - 2
    Brazil - 1

    I've gotten it to work using a nested SELECT statement, but it's counting all of the times the supplier name is listed in the PURCHASE_ORDERS table (it's giving me 4 for Switzerland, not 2.) Here's what I'm using:


    Code:
    SELECT DISTINCT SUPPLIER.SUPPLIER_NAME, SITE.COUNTRY, Count(*) AS Number_of_Sites
    FROM (POS LEFT JOIN (SITE LEFT JOIN (SELECT DISTINCT SITE_DESCRIPTION FROM SITE)  
    AS T ON SITE.SITE_DESCRIPTION = T.SITE_DESCRIPTION) 
    ON POS.SITE_ID = SITE.SITE_ID) 
    RIGHT JOIN SUPPLIER ON POS.SUPPLIER_ID = SUPPLIER.SUPPLIER_ID
    GROUP BY SUPPLIER.SUPPLIER_NAME, SITE.COUNTRY
    HAVING (((SUPPLIER.SUPPLIER_NAME)="Lenovo"));
    Any help would be greatly appreciated!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    There is no SITE_ID fields shown in POS or SITE tables. Is this autonumber primary key in SITE and foreign key in POS?

    SELECT Q.Supplier_Name, Q.Country, Count(Q.Site_Description) AS Number_of_Sites
    FROM (SELECT DISTINCT Site.Site_Description, Supplier.Supplier_Name, Site.Country
    FROM (POS INNER JOIN Site ON POS.Site_ID = Site.Site_ID) INNER JOIN Supplier ON POS.Supplier_ID = Supplier.Supplier_ID) AS Q
    GROUP BY Q.Supplier_Name, Q.Country;
    Last edited by June7; 07-21-2014 at 04:53 PM.
    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. DCount to count unique values
    By nlkehl in forum Queries
    Replies: 3
    Last Post: 06-09-2014, 10:46 AM
  2. Replies: 5
    Last Post: 03-17-2014, 04:02 PM
  3. Replies: 3
    Last Post: 02-24-2014, 02:19 PM
  4. Count Unique Values in a Category
    By fionny in forum Queries
    Replies: 2
    Last Post: 06-28-2013, 02:28 AM
  5. Count of Unique Values
    By vsmk in forum Queries
    Replies: 2
    Last Post: 03-14-2010, 12:07 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