Results 1 to 4 of 4
  1. #1
    bbxrider is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    69

    count only once per group

    I thought this would be easy but all the count functions, and IIF etc, seem to count all the records per group. I need to count only once per group.
    here's the recs being fed to report from a query, note the 'USE CODE Y/N' will always be same for each rec for a given ID. I'm using a group by ID, so I list all the codes per group ID and NAME, in the detail section
    ID NAME CODE USE CODE Y/N
    1a name 1 a y
    1a name 1 b y
    1a name 1 c y
    1b name 2 d n
    1b name 2 e n
    1b name 2 f n
    1c name 3 g y
    1c name 3 h y
    1c name 3 i y
    1d name 4 j n
    1d name 4 j n
    I can get all the counts of codes, codes per ID, etc but need to get a count


    of how many ID's use codes vs not using codes
    so for this group of recs it would be:
    Total ID's using Codes = 2
    Total ID's not using Codes = 2
    I've been looking for something like to identify the first or last rec of a group, that would enable me to do a SUM based on the 'USE CODE Y/N' value with a SUM(IFF..
    but can't seem to find the way to do this

  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
    Need 2 queries.

    Query1
    SELECT ID, [Name], [USE CODE] FROM table GROUP BY ID, [Name], [USE CODE];
    or
    SELECT DISTINCT ID, [Name], [USE CODE] FROM table;

    Query2
    SELECT [Use Code], Count(ID) AS CountID FROM Query1 GROUP BY [Use Code];

    Or nested subquery.
    SELECT Query1.[Use Code], Count(Query1.ID) AS CountID FROM (SELECT ID, [Name], [USE CODE] FROM table GROUP BY ID, [Name], [USE CODE]) AS Query1 GROUP BY Query1.[Use Code];


    Advise not to use spaces and special characters/punctuation (underscore is exception) in naming convention. Also, Name is a reserved word and should avoid reserved words as names.
    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
    bbxrider is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Apr 2010
    Posts
    69
    I need to have ID, NAME, and CODE, in the query output, not seeing that in your example. The report group header lists the ID and NAME, then the detail section lists all the CODE(s) for that group, the layout I listed is actually a query output from 2 tables. I see how the distinct works if I only have ID, NAME, and USE_CODE_Y_N (changed name with underscores for readability) but having to combine it with all the CODES for that group is still my bottleneck

  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, then in the final query can have First([Name]) and First(ID).

    Including Code field in the aggregate queries does not make sense. Would have to join the final aggregate query back to the original table, linking on the ID.
    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: 4
    Last Post: 07-24-2013, 02:34 PM
  2. COUNT Group Count Records COUNT FUNCTION
    By PMCOFFEY in forum Access
    Replies: 9
    Last Post: 11-09-2012, 09:40 PM
  3. First Group, Second count, and third sum
    By hoachen in forum Access
    Replies: 2
    Last Post: 07-30-2012, 06:03 PM
  4. GROUP and COUNT?
    By TheMoodyFiles in forum Queries
    Replies: 2
    Last Post: 02-10-2011, 05:50 PM
  5. SQL Count and Group By
    By Tyork in forum Programming
    Replies: 11
    Last Post: 01-24-2011, 09:06 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