Results 1 to 12 of 12
  1. #1
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47

    Red face Count number of record to first occurence of a value

    Hi all,

    Here's a problem i m working on,


    emp date hour value
    B1 1feb 16 D
    A1 11feb 8 E
    A1 6feb 3 A
    A1 7feb 10 B
    A1 6feb 4 B
    A1 8feb 23 C

    I need a query to count the number of records from the table top (latest date and hour after sorting) to get to value "A". The result should be 5 after sorting the date time by descending order as below, please help.

    emp date hour value
    A1 11feb 8 E
    A1 8feb 23 C
    A1 7feb 10 B
    A1 6feb 4 B
    A1 6feb 3 A
    B1 1feb 16 D

  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,967
    That date field is not showing as a date/time value. It looks like text and will not sort chronologically.
    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
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    Sorry about the confusion, it should be in date format like 1-Feb-2014, but to make a miniature of my real table for posting purpose, i did simply the date and the time only show the hours in 24 hours format.

  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,967
    Don't really understand the requirements. You want to count how many "A1" records there are? Why A1?
    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
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    I actually want to get the position of "A" in "value" column of the sorted table, the 2nd table show the sorted version of the 1st, the date time is sorted in descending order and is grouped by emp, and the emp column is the employee name.

    On the 2nd table, the date time is latest on the top and earliest on the bottom, i need to know how many lines/records i have to go back from the latest date time to get to the line/record that has an "A" in the "value" column. So for emp A1, i know by counting from the top the 5th line will have an "A", for emp B1, the result will be nil(or whatever show no matches) cause there is no line/record matches the above criteria, and i want a query to do the job for each emp, please help.

  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,967
    So if there were a record: B1 7feb 9, what would be desired result - 2?

    Perhaps if you explained why you need this, could better advise.
    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
    trevor40's Avatar
    trevor40 is offline Advanced db Manager
    Windows XP Access 2003
    Join Date
    Feb 2014
    Location
    Australia
    Posts
    402
    You may need to use vba and loop through a recordset looking for the result
    Air code untested !!
    eg very basic sample, you will need to fix it up but gives the general idea

    Set rs = db.OpenRecordset("SELECT * FROM your_table ORDER BY Value_field")
    rs.movefirst

    Do While Not rs.value_filed not me.text1 ' text1 holds the value you are looking for
    count_lines=count_lines+1

    rs.MoveNext
    Loop
    me.tex1 = count_lines ' when it equals it put that value into text1
    rs.Close

  8. #8
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    Quote Originally Posted by June7 View Post
    So if there were a record: B1 7feb 9, what would be desired result - 2?

    Perhaps if you explained why you need this, could better advise.
    if there is an "A" on that line in the "value" column you suggest, the expecting result of the query should be

    emp RecCount
    A1 5
    B1 1

    June7,
    thanks for the patience and interest.
    Last edited by zx3; 02-26-2014 at 03:13 AM. Reason: wrong info

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    All that's doing is counting records by group. That is an aggregate (Totals) GROUP BY query. Access Help has info on using the query builder to build aggregate query.

    SELECT EmpID, Count([Date]) AS CountOfEmp FROM tablename GROUP BY EmpID;


    Or build a report and use its Grouping & Sorting features with aggregate calcs in group footer.
    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.

  10. #10
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    June7, How about the value "A", Or if the sorted table changed to
    emp date hour value
    A1 11feb 8 E
    A1 8feb 23 C
    A1 7feb 10 B
    A1 6feb 4 B
    A1 6feb 3 A
    B1 7feb 9 A
    B1 1feb 16 B

    and the criteria change to 1st B value from the top for each emp, the result will be as following
    emp RecCount
    A1 3
    B1 2

    please help.

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,967
    See trevor40 suggestion in post 7.
    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.

  12. #12
    zx3 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Aug 2011
    Posts
    47
    If there is no easy way to do it using sql queries then i will try vba like trevor40 suggested, thanks everyone for their contribution.

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

Similar Threads

  1. Using Date and Occurence count to create a name
    By DatabaseIntern in forum Forms
    Replies: 1
    Last Post: 07-11-2013, 11:08 AM
  2. Replies: 6
    Last Post: 02-13-2013, 04:54 AM
  3. Replies: 14
    Last Post: 02-23-2012, 06:32 PM
  4. Replies: 6
    Last Post: 07-25-2011, 01:54 PM
  5. How to track/count the occurence of a particular field
    By jessica.ann.meade in forum Reports
    Replies: 4
    Last Post: 02-09-2011, 01:41 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