Results 1 to 7 of 7
  1. #1
    Sam_NY is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    17

    Need basic advice on using Count in a query

    Hello,

    I'm trying to create a query that includes a count of records related to it from a child table. I did try to find answers on this, but something is eluding me. I'm pretty much a novice with Access, so hoping someone can help.



    I have a parent table and a related child table (one-to-many). The child table has a text field used (with a lookup) to categorize reach child record by one of three categories (let's say CatA, CabB, and CatC for this). Let's say I have 100 records in the parent table, and I want the query to produce one row for each of the parent records, each with three separate columns corresponding to the three categories. These columns would display the count of the related records in that category.

    What I did in query design view:
    • Added the two tables
    • Changed the join type to display all records from parent table
    • Added the fields I needed from the parent table
    • Added the category field from the child table
    • Clicked on the Totals button
    • In the grid, changed the Totals row to "Count" for the category field (and left the other fields as "Group By")
    • Changed the criteria to be ="CatA"


    Then I ran the query before adding columns for the two other categories. I got this error message:
    "Datatype mismatch in criteria expression"

    Clearly, Count doesn't work like I think it should (i.e. adding criteria to filter what each column actually counts). Am I on the wrong track here? Is there another way to go about this and get the same resulting file?

    Thanks for any advice!

  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
    What you want sounds like a CROSSTAB query. There is a wizard for that.

    Or to emulate a CROSSTAB without the issues they have 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
    Sam_NY is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    17
    Thanks. That video is very helpful, June7. I think a crosstab query is what I want. Having some problems with it though.

    According to the video, the trick is to create a new column based on an IIF() statement. Here's what is used for my Field in the query grid:

    CatA: IIF([CategoryTextField]="CatA",CategoryTextField,0)

    And then I changed the "Total:" field to "Count"

    Interestingly, when I save the query, Access automatically changes the above field to:

    CatA: COUNT(IIF([CategoryTextField]="CatA",CategoryTextField,0))

    and the Total: field to "Expression"

    However, when I run the query I get some weird behavior. First off, every single Parent record that doesn't have an associated record from the child table gets a value of 1. As a test, I tried it on a child table with no records at all--still 1 for all the parent records. Why would it do this?

    Secondly, for those parent table records that DO have associated records from the child table, the query counts ALL the records for that row, including CatB and CatC. When there are these records, it doesn't add them to the 1. It gives the correct number of total records, it's just not evaluating the contents of the cell the way I would expect.

    Any ideas? The main difference in my case vs. the example in the video is that I'm dealing with two tables rather than one, but maybe it doesn't have anything to do with that.

    Thanks again!

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Post the entire SQL statement for analysis.

    If you want to provide db, follow instructions at bottom of my post.

    What you are doing is emulating a CROSSTAB. If you want a real CROSSTAB then use the wizard.
    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
    Sam_NY is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    17
    Thanks, here's the SQL:

    SELECT Bills.IntroNumber, Count(IIf([ActionType]="Vote",[ActionType],0)) AS Votes
    FROM Bills LEFT JOIN Bills_Policymakers ON Bills.ID = Bills_Policymakers.BillID
    GROUP BY Bills.IntroNumber;

    I didn't go the wizard route because at the beginning of the video, the narrator says that with the wizard you can't put in more than one field at a time. I admit I didn't experiment with it and maybe that's changed with 2010.

    I'll take a look at that.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Hasn't changed. Only one field can be specified as Value for aggregation.

    Did not need the full SQL after all, should have seen the issue with expression.

    Note that the expression in the example involves 2 fields and Sum. Yours has only one and you want Count.

    The Count() counts all data results of the IIf() - 0 is data. What you need is Null. Nulls are not counted.

    Count(IIf([ActionType]="Vote",[ActionType],Null))

    or

    Sum(IIf([ActionType]="Vote",1,0))
    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.

  7. #7
    Sam_NY is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2015
    Posts
    17
    That worked perfectly! Thanks.

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

Similar Threads

  1. Year Count and Month Count Same Query
    By NateSmith in forum Queries
    Replies: 1
    Last Post: 05-13-2015, 08:23 AM
  2. Count Function Improvement Advice
    By asmolow in forum Queries
    Replies: 5
    Last Post: 03-22-2015, 08:45 AM
  3. Query for Reconciliation - Help/advice needed
    By Chatholo in forum Queries
    Replies: 4
    Last Post: 02-02-2015, 03:03 PM
  4. Replies: 7
    Last Post: 09-11-2014, 11:48 AM
  5. Replies: 2
    Last Post: 04-15-2014, 01:59 PM

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