Results 1 to 6 of 6
  1. #1
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199

    How to do Grouping with Select Distinct


    Hello,

    I found a good tip about how to get distinct from a field in Access here (http://stackoverflow.com/questions/1...n-access-query) but could someone help me modify that so that I can see distinct by FIELD1, FIELD2 and FIELD3. I tried Group By in Access but that wasn't giving me what I need.

    SELECT Count(*) AS [Distinct FIELD1]
    FROM (SELECT DISTINCT FIELD1 FROM data) AS [%$##@_Alias];
    Thanks!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Maybe:
    SELECT
    (SELECT Count(*) AS [F1] FROM (SELECT DISTINCT FIELD1 FROM data)) AS CountDistinctF1,
    (SELECT Count(*) AS [F2] FROM (SELECT DISTINCT FIELD2 FROM data)) AS CountDistinctF2,
    (SELECT Count(*) AS [F3] FROM (SELECT DISTINCT FIELD3 FROM data)) AS CountDistinctF3
    FROM data;
    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
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    JUNE7, that got me closer but what happened was that it just counted the distinct of those three fields. Really I just want to count the distinct of Field 1 but show "Grouped By" Field2 and Field3. Something line this

    Field 1: 27 (count of all instances APPLES AND JANUARY)
    Field 2: Apples
    Field 3: January

    does that make sense?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Okay, try:
    SELECT Count(Field1) AS CountF1, Field2, Field3 FROM (SELECT Field1, Field2, Field3 FROM tablename GROUP BY Field1, Field2, Field3) AS Q1 GROUP BY Field2, Field3;

    What is Field1 data?
    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.

  5. #5
    accessmatt is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    199
    WoW, June7 - A+ for that! I wish I knew what was going on here but no matter, it does work! Field 1 data is a USER ID.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Inner query retruns unique combinations of UserID with Field2 and Field3. The outer query then counts how many UserIDs for each combination of Field2 and Field3.

    If it helps to understand, could build two query objects. First build the inner query then build another query that references the first.
    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. Select Distinct Help
    By fionny in forum Queries
    Replies: 2
    Last Post: 06-28-2013, 08:24 AM
  2. Select distinct
    By Paul-NYS in forum Access
    Replies: 1
    Last Post: 07-09-2012, 09:03 AM
  3. SELECT DISTINCT or equivalent
    By kpo in forum Programming
    Replies: 13
    Last Post: 05-20-2012, 01:45 PM
  4. How to use variable in SELECT DISTINCT
    By celtics11 in forum Access
    Replies: 1
    Last Post: 11-18-2011, 04:28 PM
  5. Replies: 8
    Last Post: 04-21-2011, 05:29 PM

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