Page 1 of 3 123 LastLast
Results 1 to 15 of 36
  1. #1
    BigMac4 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    30

    Count last 32 entries help please.

    Hi everyone,
    Got what might be a simple problem, here is what i have. A table with dates in column 1 and grade in column 2 what i need to do is count how many "pass" grades are in column 2 within the last 32 entries (not dates) in the date column i have got the same date multiple times so i cannot work on the last 32 dates it must be on entries.




    Hope that makes sense

    Cheers

    Mac

  2. #2
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Not sure it can be done via SQL. Anyone else?

    How's your VBA coding? I can imagine a fairly simple routine based on the absolute position of a record in a record set.

  3. #3
    BigMac4 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    30
    I was hoping for a nice simple query but i can do some VBA if thats the way forward.

    Mac

  4. #4
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    OK, something like this should do you.

    Code:
    Public Function CountLast32() As Long
        Dim rst As DAO.Recordset
        
        Set rst = CurrentDb.OpenRecordset("myQuery")
        rst.MoveLast
        If rst.AbsolutePosition >= 32 Then
            rst.Move -31
            Do Until rst.EOF
                If myCondition Then CountLast32 = CountLast32 + 1
                rst.MoveNext
            Loop
        End If
        Set rst = Nothing
        
    End Function
    Substitute your own names and code for the blue highlights.

  5. #5
    BigMac4 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    30
    I will give it a try. Thanks

    Mac

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    What field or combination of fields makes a record in your table unique? In other words, what is the primary key of this table?
    How do you identify the last record in the table?

  7. #7
    BigMac4 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    30
    I have an Autonumber field as my primary key, it is not shown to the users but it will always be my unique record.

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Research Select TOP

    I'd try
    Code:
    SELECT COUNT(*) FROM
    (SELECT TOP 32 FROM YourTableName)
    Where Grade = "PASS"

  9. #9
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Hi Orange,

    Something like that would work for BigMac if he could turn his result set upside down - that is reverse the sort order and guarantee that the records are in fact exactly in reverse sequence. Your inner query would be replaced by this new query. The outer query would have to group on 'Pass' or 'Fail' and count within those groupings.

    Never occurred to me to reverse sort. Doh!

    Maybe grouping is not necessary if the grades are numeric rather than two-state.

  10. #10
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,848
    Why Fail?
    what i need to do is count how many "pass" grades are in column 2 within the last 32 entries
    I agree that it should be DESCENDING.

    Sample of same/similar concept:
    I have an Employee Table (see attached) and have tested this sql

    Code:
    SELECT Count(*) AS Expr1, LNAME
    FROM (select top 4 * from employee  ORDER BY EMPID DESC)
    WHERE (((Left([lname],1))="J")) 
    GROUP BY LName;
    Attached Thumbnails Attached Thumbnails Count_Pass.jpg  
    Last edited by orange; 08-20-2012 at 02:08 PM. Reason: more info

  11. #11
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Why Fail?
    Agreed, not necessary, BigMac only wants "pass" grades that I assumed at first were simple two-state, Pass/Fail values.

  12. #12
    BigMac4 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    30
    The database is alot more complicated that i make it sound i just needed an easy way to explain what i was after, now i have a code to start from i can work it into what i need. I may upload an example in the next few days if i cant ge it to work properly and see if you guys can rejig it abit.

    Thanks for your help

    Mac

  13. #13
    BigMac4 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    30
    It works out just how i needed it to, thanks alot.

    Mac

  14. #14
    BigMac4 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2012
    Posts
    30
    Click image for larger version. 

Name:	Access Example.jpg 
Views:	4 
Size:	152.8 KB 
ID:	8893
    Seen as that was very easy for you to work out i would like to expand on this a little and make the next problem a bit harder. What i need to do now is what can only assume will be an IF function in an expression builder. As you can see from my picture i have pass fail and OCL what i need in each entry under OCL is - If within the last 10 entries i have 3 fails return an "A" and if i have 4 fails return a "B" and so and so forth, again once i have the basic formula working i will expand on it a bit. By the way this is all very easy in Excel but in Access it is a whole different kettle of fish.

    Lets see how good you guys are.

    Thanks

    Mac

    P.s lots more interesting problems to follow at some point.

  15. #15
    Rod is offline Expert
    Windows 7 32bit Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Lets see how good you guys are.
    Hm! I've stumbled at the first hurdle; I don't fully understand your requirement. Using your example, are you expecting a value for OCL on every line or just the line for ID=4? Actually there appears to be no logical sequence to the lines iin your example.

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

Similar Threads

  1. Replies: 8
    Last Post: 08-16-2012, 09:04 AM
  2. Replies: 1
    Last Post: 04-03-2012, 10:31 AM
  3. Replies: 8
    Last Post: 03-14-2012, 05:40 PM
  4. Count date entries from seperate tables/queries
    By krutoigoga in forum Queries
    Replies: 3
    Last Post: 06-30-2011, 12:48 PM
  5. Query to group and count field entries
    By newtoAccess in forum Queries
    Replies: 4
    Last Post: 11-29-2010, 09:19 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