Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    FJM is offline Competent Performer
    Windows 98/ME Access 2003
    Join Date
    Jun 2010
    Posts
    117

    Count(*) function

    I am using the Count(*) function in a report to show me how many times any particular part appears during a selected period.
    Quite a few entries only give the result as 1 and I would like to restrict what appears on the report to only show where the Count(*) total for each part in question is greater than 2 (or any other number I choose).
    Can anyone help with the solution ?

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If you do your counts in query (using the Aggregate Function), simply add "> 2" (without the quotes) in the Criteria section of that Aggregate field.

  3. #3
    FJM is offline Competent Performer
    Windows 98/ME Access 2003
    Join Date
    Jun 2010
    Posts
    117
    That was one of the first ways I tried ( I think - I tried the Sigma option on the Group by in the query), but as it was not successful, I used the wizard to construct the report which then automatically added the Count(*) function.
    Any thoughts ?

  4. #4
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Let's see if we can get your query to work out. Take another crack at it, then change your query to SQL View and post the SQL code here.
    And explain what it is returning that isn't quite right, and we'll see if we can help you straighten it out.

  5. #5
    FJM is offline Competent Performer
    Windows 98/ME Access 2003
    Join Date
    Jun 2010
    Posts
    117
    SELECT NCRFORM.NCRNO, NCRFORM.Date, NCRFORM.CPEPARTNO, NCRFORM.QTYFROM NCRFORM
    GROUP BY NCRFORM.NCRNO, NCRFORM.Date, NCRFORM.CPEPARTNO, NCRFORM.QTY
    HAVING (((NCRFORM.Date) Between [Forms]![Non Conf Reports (Switchboard page)]![Combo91] And [Forms]![Non Conf Reports (Switchboard page)]![Combo93]) AND ((NCRFORM.CPEPARTNO)>"2"));

    The report I run asks for 2 dates (Combo91 and Combo93).It then lists out all of the instances of CPEPARTNO (and sub orders by NCRNO) and shows a Count for the number of times the CPEPARTNO has appeared. There are many items where the count is low at 1 or 2 and I wanted to eliminate these from the report.
    Hope this makes sense

  6. #6
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    A few issues. I think you are returning too many fields. If you group on all of these fields, you are going to have a separate entry for each date, instead of a total for that date range (unless that is what you want?).

    Also, you want to check on the Aggregate (Count) is greater than 2, not the field before it is aggregated.

    See if this returns what you want:
    Code:
    SELECT 
    	NCRFORM.CPEPARTNO, 
    	COUNT(NCRFORM.CPEPARTNO)
    FROM 
    	NCRFORM
    WHERE
    	NCRFORM.Date Between [Forms]![Non Conf Reports (Switchboard page)]![Combo91] And [Forms]![Non Conf Reports (Switchboard page)]![Combo93]
    GROUP BY 
    	NCRFORM.CPEPARTNO
    HAVING 
    	COUNT(NCRFORM.CPEPARTNO)>2;

  7. #7
    FJM is offline Competent Performer
    Windows 98/ME Access 2003
    Join Date
    Jun 2010
    Posts
    117
    That works good for the numbers, but ideally, I would like to keep the NCRNO field as well as this tells me where each of the entries comes from. Is that possible ? .

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    That depends on your relationship between CPEPARTNO and NCRNO. If it is a one-to-one relationship, adding it in like this should be fine:
    Code:
    SELECT 
            NCRFORM.NCRNO,	
            NCRFORM.CPEPARTNO, 
    	COUNT(NCRFORM.CPEPARTNO)
    FROM 
    	NCRFORM
    WHERE
    	NCRFORM.Date Between [Forms]![Non Conf Reports (Switchboard page)]![Combo91] And [Forms]![Non Conf Reports (Switchboard page)]![Combo93]
    GROUP BY 
            NCRFORM.NCRNO,	
            NCRFORM.CPEPARTNO
    HAVING 
    	COUNT(NCRFORM.CPEPARTNO)>2;
    If it is not a one-to-one relationship, it may break down your counts into more subcatergories that you do not want.
    If that is the case, how do you decide which value of NCRNO you want to return, since there can be multiple NCRNO values for each CPEPARTNO value?

  9. #9
    FJM is offline Competent Performer
    Windows 98/ME Access 2003
    Join Date
    Jun 2010
    Posts
    117
    I think this is the root of the problem. The relationship is a one-many. The value returned for the total of each CPEPARTNO comes from the individual times that it appears in NCRNO.
    To put it in english, what I am trying to achieve is this.
    I have a Quality database, part of which lists what is known as an NCR (a Non Conformance Report. This is the NCRNO).
    Each NCR is then made up of a Partnumber (This is the CPEPARTNO).
    Over the course of any period, I want to find out if any particular part keeps on repeating to see if I have a problem. I am not bothered if the total number of NCR's against any part is either 1 or 2 but I would be interested in numbers higher than that.
    I want to then run a report which shows the part number, the total number of times it appears (i.e the Count figure) and then so I can further investigate, what the individual NCR numbers are which make up the total.

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    You want to know if the CPEPARTNO is associated with multiple NCR? So you want to count the number of NCR for each CPEPARTNO?

    Consider a report instead of aggregate query. Use report Grouping & Sorting with aggregate calc in footer. Report allows display of detail info as well as summary calcs.
    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.

  11. #11
    FJM is offline Competent Performer
    Windows 98/ME Access 2003
    Join Date
    Jun 2010
    Posts
    117
    I understand that the group can give me a total - I have used this in other reports. The problem I am having is that in order to restrict the query to totals of 3 or more using the code you showed me , the query then doesn't include the field NCRNO which means I cannot group on it - or am I missing something ?

  12. #12
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    What happens when you use the code I posted in post #8? Does it do what you want?

    If not, it might be really helpful to see a small example of your data, along with what you would like your expected output to look like.

  13. #13
    Mahendra1000 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    101
    Quote Originally Posted by JoeM View Post
    A few issues. I think you are returning too many fields. If you group on all of these fields, you are going to have a separate entry for each date, instead of a total for that date range (unless that is what you want?).

    Also, you want to check on the Aggregate (Count) is greater than 2, not the field before it is aggregated.

    See if this returns what you want:
    Code:
    SELECT 
        NCRFORM.CPEPARTNO, 
        COUNT(NCRFORM.CPEPARTNO)
    FROM 
        NCRFORM
    WHERE
        NCRFORM.Date Between [Forms]![Non Conf Reports (Switchboard page)]![Combo91] And [Forms]![Non Conf Reports (Switchboard page)]![Combo93]
    GROUP BY 
        NCRFORM.CPEPARTNO
    HAVING 
        COUNT(NCRFORM.CPEPARTNO)>2;

    I will get back to you on this soon..

  14. #14
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I think that it should be like this
    It really doesn't matter which field you choose to count, as long as you choose a populated field (blanks/nulls could cause issues).
    The Count is just returning the number of records returned by the Grouping, so you can put any populated field in there. I often choose the same field you are Grouping on.

  15. #15
    Mahendra1000 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2013
    Posts
    101
    Thanks for letting me know on this.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. COUNT Group Count Records COUNT FUNCTION
    By PMCOFFEY in forum Access
    Replies: 9
    Last Post: 11-09-2012, 09:40 PM
  2. Count function grouping.
    By Tabix09 in forum Queries
    Replies: 3
    Last Post: 08-23-2011, 01:43 PM
  3. Question; Count function ..
    By efleming in forum Queries
    Replies: 4
    Last Post: 05-27-2011, 08:05 AM
  4. Count function on query
    By yousillygoose in forum Queries
    Replies: 1
    Last Post: 02-15-2010, 09:58 PM
  5. Query using count function
    By wasim_sono in forum Queries
    Replies: 0
    Last Post: 11-28-2007, 03:16 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