Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123

    Count values that occur consecutively

    I need to come up with a query that will count Consecutive Rostered Days for ReliefCode(function Code)=1.
    Scenario
    Bob Is Rostered Monday, Tuesday, Friday, and Saturday
    He is rostered to Fctn1 on Monday, Tuesday and Friday (the count should be 2 (begins at 0)although not consecutive days by date)
    If he does another function on Saturday I want the count to reset to 0
    SQL Im not totally sure the join types are correct but here are the tables and how they join.
    SELECT
    FROM ((tblEmpDetails INNER JOIN tblRelief_Allot ON tblEmpDetails.EmpId = tblRelief_Allot.EmpId) INNER JOIN (tbl36hrWeekRosters INNER JOIN tblRosterRec ON tbl36hrWeekRosters.Date_ = tblRosterRec.Date_) ON tblEmpDetails.EmpId = tblRosterRec.EmpId) INNER JOIN tblRoster ON tblEmpDetails.Roster = tblRoster.Roster;


    Tables and Associated Fields

    tblEmpDetails
    EmpID
    Name
    EmpNo
    Roster
    Shift

    tbl36HrWeekRosters
    Date
    Day_Name
    Day_Code

    tblRoster
    Day_Code
    Roster
    Shift

    tblRosterRec (Data Added Daily)


    Name
    Date_
    FctnID
    EmpID

    Relief_Allot (lists all employees with various functions assigned to them
    EmpID
    ReliefCode (function ID)

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I don't see a good way to do this with queries. Are you open to attempting it with vba? it will likely involve creating a temp table (though I am loathe to use them).

  3. #3
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    I would like to use VBA for this if I can get the result I need

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    So let me make sure I have this correct

    If you have this:

    Code:
    FP_ID  Person_ID  Function_ID  Function_Date---> other data
    1     1          1            1/1/2013
    2     1          1            1/2/2013
    3     1          1            1/4/2013
    This person would have a value of 2, even though the most recent day is separated from the others by 1 day

    if you were to then add another record:


    Code:
    FP_ID  Person_ID  Function_ID  Function_Date---> other data
    1     1          1            1/1/2013
    2     1          1            1/2/2013
    3     1          1            1/4/2013
    4     1          2            1/5/2013
    This person's consecutive days would be 1

    So what happens if you have this:

    Code:
    FP_ID  Person_ID  Function_ID  Function_Date---> other data
    1     1          1            1/1/2013
    2     1          1            1/2/2013
    3     1          2            1/3/2013
    4     1          2            1/4/2013
    5     1          2            1/5/2013
    5     1          1            1/6/2013
    Would this person's value be 1 (because his most recent work was on function 1) or would it be 2 (because he worked that same function on the 1st and 2nd)

  5. #5
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Your Representation Is great.
    Do you know how I can get those Results?

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Well I wasn't sure about the third part of that and you didn't really answer it and that's sort of important as to how you would approach this problem.

    Look at the third set of data.

    What would your 'consecutive' number be, would it be 1? or would it be 2?

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    First I had two tables in my example:

    tbl_PersonFunction
    FP_ID (autonumber PK)
    Person_ID (number, assuming FK to an employee table or the like)
    Function_ID (number, assuming FK to a function table)
    Function_Date (the date the employee was on that function)

    NOTE: I assumed that an employee could not be assigned to the different functions on the same day

    tbl_Cons
    Cons_ID (autonumber PK)
    Person_ID (number, FK to emoployee table)
    Start_Date (date, entered by user)
    End_Date (date, figured by calculation)
    Function_ID (number, FK to function table)
    Cons_Days (maximum consecutive days)

    Code:
    Dim dStartDate As Date
    Dim iProjDays As Integer
    Dim dEndDate As Date
    
    Dim db As Database
    Dim sSQL As String
    Dim rst_People As Recordset
    Dim iPersonID As Long
    Dim rst_Functions As Recordset
    Dim dFunDate As Date
    Dim dFunDatePrev As Date
    Dim iFun As Long
    Dim iFunSearch As Long
    Dim iCons As Integer
    Dim iConsMax As Integer
    
    dStartDate = InputBox("Enter the Starting Date", "Enter the Starting Date of the Reporting Period", #1/1/2013#)
    iProjDays = InputBox("Report through how many days?", "Enter the Number of Days following the start date", 7)
    dEndDate = DateAdd("d", iProjDays, dStartDate)
    
    Set db = CurrentDb
    sSQL = "SELECT Person_ID FROM tbl_PersonFunction WHERE Function_Date Between #" & dStartDate & "# AND #" & dEndDate & "# GROUP BY Person_ID"
    Set rst_People = db.OpenRecordset(sSQL)
    If rst_People.RecordCount <> 0 Then
        db.Execute ("DELETE * FROM tbl_Cons")
        Do While rst_People.EOF <> True
            iPersonID = rst_People.Fields("Person_ID")
    'Debug.Print iPersonID
            sSQL = "SELECT * FROM tbl_PersonFunction WHERE Person_ID = " & iPersonID & " AND Function_Date Between #" & dStartDate & "# AND #" & dEndDate & "# ORDER BY Function_Date DESC"
            Set rst_Functions = db.OpenRecordset(sSQL)
            rst_Functions.MoveFirst
            iFunSearch = rst_Functions.Fields("Function_ID") 'sets the function ID we're looking to count consecutive days for
            dFunDatePrev = rst_Functions.Fields("Function_Date") 'initializes the date to start counting
            iCons = 1 'initializes the consecutive day count
            iConsMax = 1
            Do While rst_Functions.EOF <> True 'cycles through function records from the most recent to the least recent over the defined period
                dFunDate = rst_Functions.Fields("Function_Date")
                iFun = rst_Functions.Fields("Function_ID")
                If iFun = iFunSearch Then   'if the function date matches the function of the most recent day, keep going
                    If dFunDate = DateAdd("d", -1, dFunDatePrev) Then  'if the date of the current record is one day prior to the last record add 1 to the consecutive count
                        iCons = iCons + 1 'increments the consecutive days by 1 if it's a consecutive day
                        If iCons > iConsMax Then
                            iConsMax = iCons 'if the current consecutive days is greater than the currrent maximum consecutive days make the maximum consecutive days = current consecutive days
                        End If
                    Else
                        iCons = 1 'if the date of the current record is NOT one day prior the last record, reset the current consecutive days count to 1
                    End If
                    dFunDatePrev = dFunDate
                Else 'if the function date DOES NOT match the function of the most recent day, move to the last record to exit the function loop
                    rst_Functions.MoveLast
                End If
                rst_Functions.MoveNext
            Loop
            rst_Functions.Close
    
            sSQL = "INSERT INTO tbl_Cons ("
            sSQL = sSQL & "Person_ID, "
            sSQL = sSQL & "Start_Date, "
            sSQL = sSQL & "End_Date, "
            sSQL = sSQL & "Function_ID, "
            sSQL = sSQL & "Cons_Days"
            sSQL = sSQL & ") VALUES ("
            sSQL = sSQL & iPersonID & ", "
            sSQL = sSQL & "#" & dStartDate & "#, "
            sSQL = sSQL & "#" & dEndDate & "#, "
            sSQL = sSQL & iFunSearch & ", "
            sSQL = sSQL & iConsMax
            sSQL = sSQL & ")"
            db.Execute (sSQL)
            
            rst_People.MoveNext
        Loop
        rst_People.Close
    Else
        MsgBox "no records in the range specified", vbOKOnly, "error: no records in range"
    End If
    
    Set db = Nothing

  8. #8
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Wow! I cant believe you came up with that code so quick.
    Is it possible to create the tables needed from existing tables using querys from tables listed previously?
    I have a two textboxes to select the Dates [forms]![form1]![txtDate] and[forms]![form1]![txtDate2] (one for the date of the roster, and the other a previous date to count between)
    Do I need to have the days rostered for each team member or does it just look at the privious function assigned to that team member?

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    you don't need to create new tables, you just have to adapt it for your situation. Just substitute your table names and field names where appropriate.

    The dates I just put in something so you could have a variable range but you can substitute your form values for the start and end dates just as easily.

    The way this code works is it takes all the people who had at least 1 function in the period (between the dates supplied)

    as it cycles through these people it looks at all the functions they had scheduled in DESCENDING order. The 'first' (or most recent) service is the search value.

    if it reaches a service other than the search value, it exits updating the temp table with the maximum consecutive days found
    if it finds the search value and the date is one day less than the previous date it adds to the current consecutive days and if that value is larger than the maximum number of consecutive days, it updates the maximum number of consecutive days.

    if an employee did not have a function assigned in the period you're looking for they will not end up with a record in the 'temp' table (tbl_Cons)

  10. #10
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Thank you for your quick response I will give it a try and report back.
    one more thing what title do I put for the code?
    ie. Private Sub ..........._AfterUpdate()

  11. #11
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I just had it as a separate module but you can add it anywhere it makes sense in your process (button click, after update, on exit, whatever)

  12. #12
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    where does the tbl_cons come from?

  13. #13
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    That's my temp table, you can come up with your own that stores the variables that you want

  14. #14
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Ok thanks for all your help very Informative

  15. #15
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Sorry, one more thing.....
    How do I create the temp table?

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

Similar Threads

  1. Help for Count Values
    By bronson_mech in forum Queries
    Replies: 3
    Last Post: 11-26-2013, 10:11 AM
  2. Replies: 1
    Last Post: 08-01-2013, 12:24 PM
  3. Count of Certain Values In a Field
    By tsn.s in forum Queries
    Replies: 4
    Last Post: 08-06-2012, 08:32 AM
  4. Control when Saves occur
    By caddcop in forum Forms
    Replies: 0
    Last Post: 04-05-2011, 04:15 PM
  5. Replies: 1
    Last Post: 06-10-2010, 10:47 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