Results 1 to 8 of 8
  1. #1
    ceebee is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2017
    Posts
    12

    Problems with Count(*)

    Hello

    This is a very basic question, but I have to ask it.

    I am trying to do a count(*) using the where clause.

    For example:

    SELECT count(*)
    FROM table
    WHERE id = 5

    I do not think I can use a count(*) with a where clause.



    Can someone please advise?

    Thank You

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Create the query in query design, Access will do the SQL for you, which you can then copy and paste.

  3. #3
    ceebee is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2017
    Posts
    12
    Yes I tried that, but that is where I got the error message stating I can't use a 'where' clause when using a count(*)

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Use HAVING in place of the WHERE because filter is applied to an aggregate calc field. Query Designer should do this for you, not SQL View.
    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
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    It must be something else, because I can use

    SELECT Count(*) AS Expr1
    FROM competency_instance
    WHERE (((competency_instance.MA_cde)=1));

    in the query definition SQL and it works fine.

    In what context are you using that SQL?

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Ooops, if it had been something like Sum([field]) and wanted to apply filter to that calc, use HAVING.
    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
    ceebee is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Apr 2017
    Posts
    12
    OK Thank You

    I tried using the same count/where clause as in your example, and it worked.

    I did not have the ((())) the last time. I am guessing that is what was wrong?!

  8. #8
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I did not have the ((())) the last time. I am guessing that is what was wrong?!
    Access goes a little crazy with the parentheses. Many times, it adds a lot more than it actually needs.
    For example, in this line here:
    Code:
    WHERE (((competency_instance.MA_cde)=1));
    since there is only one simple condition, it doesn't really need any and can simplified to:
    Code:
    WHERE competency_instance.MA_cde=1;
    It doesn't hurt to have them in there, I just hate the way it looks.

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

Similar Threads

  1. Replies: 1
    Last Post: 03-06-2017, 06:48 AM
  2. Replies: 2
    Last Post: 06-30-2016, 06:38 PM
  3. Year Count and Month Count Same Query
    By NateSmith in forum Queries
    Replies: 1
    Last Post: 05-13-2015, 08:23 AM
  4. Replies: 2
    Last Post: 04-15-2014, 01:59 PM
  5. COUNT Group Count Records COUNT FUNCTION
    By PMCOFFEY in forum Access
    Replies: 9
    Last Post: 11-09-2012, 09:40 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