Results 1 to 5 of 5
  1. #1
    naseerrahaman is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2013
    Location
    India
    Posts
    12

    SQL Count and countif in single Query

    HI Team,




    I am just trying to learn the SQL and have one question.

    In attached access file have single table where it contains fields(1) names and fields(2) comments.

    1. name (ABC) repeated 5 times so the count of names should be 5 and in comments word "Yes" repeated 5 time for name abc count of comment should also be 5
    2. name (XYZ) repeated 5 times so the count of names should be 5 and in comments word "Yes" repeated 4 time for name xyz count of comment should be 4

    i am trying to achieve the above result using access query and for better understanding please have a look on the attached excel file.

    Excel file : 1st sheet contains raw data
    2 and 3 sheet contains the result/output using the counta and countif formula.

    I request you to kindly advise how to achieve the expected result in access using query.

    Please Note : Cross posted in below site as well.

    link : https://www.excelforum.com/access-ta...ml#post5448050


    Thanks in advance for all your help and support.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    This could be one way

    Code:
    SELECT Sheet1.Names, Sheet1.Names, Count(Sheet1.Names) AS CountOfNames, Sum(IIf([Comments]='Yes',1,0)) AS Expr1
    FROM Sheet1
    GROUP BY Sheet1.Names, Sheet1.Names;
    And a Big thank you for notifying of the cross posting.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    naseerrahaman is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2013
    Location
    India
    Posts
    12
    Hi Welshgasman,

    Thank you so much for attending my request and solution offered is working fine as expected.

    Once again thank you so much for all your help and support.

    Regards,
    Rahaman.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,413
    you don't even need the iif statement since booleans return -1 or 0 (the answer you get to a comparison), -1=true, 0=false

    -Sum([Comments]='Yes')

    or

    sum(-([Comments]='Yes'))

  5. #5
    naseerrahaman is offline Novice
    Windows 10 Access 2013 64bit
    Join Date
    Feb 2013
    Location
    India
    Posts
    12
    HI Ajax,

    Yes, solution is working fine.

    Thank you so much for all your help and support.

    Regards,
    Rahaman

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

Similar Threads

  1. Replies: 1
    Last Post: 07-02-2018, 03:51 PM
  2. Replies: 2
    Last Post: 05-09-2018, 01:08 PM
  3. conditional countif or query on form
    By fowlerryan in forum Access
    Replies: 1
    Last Post: 05-18-2016, 03:42 PM
  4. How to add a COUNTIF type field to a select query?
    By Datamulcher in forum Queries
    Replies: 4
    Last Post: 04-27-2016, 05:53 PM
  5. Advanced CountIf Query
    By therzakid in forum Queries
    Replies: 2
    Last Post: 07-27-2011, 10:45 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