Results 1 to 5 of 5
  1. #1
    justphilip2003 is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ohio
    Posts
    125

    Counting various value occurance with Query

    In my table, a field has 5 different possible values. I would like to know if a single Query can be coded to count each value occurrences in the table. I've tried to do so by creating "group by" count, creating five columns with field names Expr1: [Count1] through Expr5: [Count5]. Each field column has a Total field. I tried using total by group, by count and by expression. Each field has a starting criteria of "PoliticalPreference" =1 through "PoliticalPreference"=5. Each attempt failed with a too complex code statement. Also Access will ask for values to use for each column and then it wipes out all of the coded criteria and changes the total by count to expression if I input same. Am I reaching too far with Query? TIA Phil

  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,626
    A GROUP BY should work.

    SELECT Fieldname, Count(*) AS CountFieldname FROM tablename GROUP BY Fieldname;

    Or try a CROSSTAB with the wizard.

    Here is how to simulate CROSSTAB with expressions http://datapigtechnologies.com/flashfiles/crosstab.html.
    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
    justphilip2003 is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ohio
    Posts
    125
    I tried to follow the datapig video to simulate a CrossTab Querry. I created five columns with field names {DEM: Count(IIf([PPref]="1",1,0,}, {IND: Count(IIf([PPref]="2",1,0},...,REP: Count(IIf([PPref]="5",1,0}. I Get the same message "Cannot have aggregate function in {Group By, Sum, or Count} clause {DEM: Count(IIf([PPref]="1",1,0,}. I am thinking my table structure doesn't lend itself to this Query form since PPref isn't a sub field for a group (like a region or segment) as shown in the video. Am I correct or is there a coding error? TIA Phil

  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,626
    The expressions you show are not constructed according to the video. The aggregate functions do not go in the expressions as you show. Look at the tutorial again. Use Sum instead of Count.

    The resulting SQL statement would be like:

    SELECT Sum(IIf([PPref]=1,1,0)) AS DEM FROM tablename GROUP BY something;

    If there are only the calculated fields and no GROUP BY clause, the result will be a single record with the 5 calculated fields.
    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
    justphilip2003 is offline Competent Performer
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2013
    Location
    Ohio
    Posts
    125
    I did try crosstabs but my tables have no groupings, hence they do not lend themselves to crosstabs. Thanks for all of the assistance. Phil

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

Similar Threads

  1. Counting TOP/first 10 in Query
    By undee69 in forum Queries
    Replies: 5
    Last Post: 11-20-2012, 09:20 AM
  2. Counting Records in a Query
    By bomich in forum Access
    Replies: 2
    Last Post: 11-16-2012, 03:00 AM
  3. Counting in Query
    By EdwinLawrence in forum Queries
    Replies: 3
    Last Post: 03-05-2012, 12:21 PM
  4. Search string for 2nd, 3rd, 4th... occurance
    By broecher in forum Queries
    Replies: 10
    Last Post: 10-26-2010, 12:03 AM
  5. Help with counting Query
    By metalhead22 in forum Queries
    Replies: 8
    Last Post: 04-29-2009, 02:07 AM

Tags for this Thread

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