Search code examples
ms-accessms-access-2016jet-sql

Jet SQL/MS Access - Query, count of tasks, sum of time, by user


I would really appreciate help in achieving the correct output, I want to understand how it works and self teach.

I have one table:

tasks table

I would like to achieve the below results from one query:

user_name  |   task   |   CountOftask  |   SumOftime
    U1           1            1              00:15
    U2           1            1              00:25
    U2           2            1              00:05
    U3           2            1              00:20
    U3           3            2              00:40

re: the table 'tasks':

The time field is date/time hh:nn. The date field is irrelevant for the query at this stage - I want all historic data returned.

re: the query:

I would like to see a Count of task, SUM of time and Group by user_name & task, using one query with a sub select, instead of two separate queries.

Thanks for your time

Edit:

I can create this output, but I use two queries (Q1) to convert the time to integer, summing it. (Q2) using Q1 convert the integer back to time hh:mm.

Q1:

SELECT 
user_name
, task
, Count(task) AS CountOftask
, Sum(CLng(24*60*CDate([time]))) AS time_mins
FROM tasks
GROUP BY user_name, task;

Q2:

SELECT 
[301 SF count of task].user_name
, [301 SF count of task].task
, [301 SF count of task].CountOftask
, [time_mins]\60 & Format([time_mins] Mod 60,"\:00") AS Time_sum
FROM [301 SF count of task]
;

I would like to be able to do this in one query, any advice is appreciated.


Solution

  • Try with:

    Select
        user_name,
        Max(task) As max_task,
        Count(*) As task_count,
        CDate(Sum([time])) As total_time
    From
        YourTable
    Group By    
        user_name,
        task
    

    Edit - if time is text:

        Sum(CDate([time])) As total_time
    

    or forced to date value:

        CDate(Sum(CDate([time]))) As total_time