Skip to content
Advertisement

Calculating avg time of activity in the morning

I am creating a query to keep track of students’ activity at school on MS Acces 2010. And I have a table as follows:

    ID       | Date     |  ActivityCode | Starttime | Endtime | Duration
    ----------------------------------------------------------
    1234     | 1/1/2020 |        a       | AM       | AM      | #(= 3Hours / Total number of AM per person per day) 
    1234     | 1/1/2020 |        a       | AM       | AM      | #In this case, Duration = 3/2 = 1.5 hr
    9876     | 1/1/2020 |        a       | AM       | AM      |#(= 3Hours / Total number of AM per person per day) 
    9876     | 1/1/2020 |        a       | AM       | AM      | #In this case, Duration = 3/3 = 1 hr
    9876     | 1/1/2020 |        a       | AM       | AM      |  #In this case, Duration = 3/3 = 1 hrs
    9876     | 1/1/2020 |        a       | 1200     | 1500    |  #In this case, Duration = 1500-1200 = 3 hrs 

My current attempt on getting the duration

IIF(starttime="AM" or starttime="PM",
#(= 3Hours / Total number of AM per person per day),
CDate(TimeSerial(Val([EndTime])100,Val([EndTime]) Mod 100,0)-
TimeSerial(Val([starttime])100,Val([starttime]) Mod 100,0))) 
AS Duration, 

But I am not sure how to write the #(= 3Hours / Total number of AM per person per day) part in SQL. Any help is appreciated.

Advertisement

Answer

Try with:

Select ID, [Date], 3/Count(*) As Duration
From YourTable
Group By ID, [Date]

Edit – with time values:

Select 
    ID, 
    [Date], 
    IIF(
        [StartTime] = 'AM' Or [StartTime] = 'PM', 
        3/Count(*),
        (TimeSerial(Val([EndTime])100,Val([EndTime]) Mod 100,0)-TimeSerial(Val([starttime])100,Val([starttime]) Mod 100,0))*24
        ) As Duration
From 
    YourTable
Group By 
    ID, 
    [Date]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement