Skip to content
Advertisement

I have to get the total time spent for a question using SQL. I am unable to get the total time and provide the answer in minutes or H:M:S format

I am trying to find the total time taken per person and provide the answer in total minutes spent using SQL. The page_start_time is time data type.The data is shown below:

Data

This is the visitors table. I need to find the total time taken using the page_start_time field per USER_ID. I tried the following code:

select sum(datediff(minute, 0, page_start_time)) as Totalnumberofminutesspent, User_id
from visitors
GROUP   BY user_id
ORDER BY Totalnumberofminutesspent DESC;

I got the following answer:

answer

When I checked manually, the answer is incorrect.

Advertisement

Answer

I am guessing you want the difference between the minimum and maximum times for each user:

select user_id,
       datediff(minute, min(page_start_time), max(page_start_time) as Totalnumberofminutesspent
from visitors
group by user_id
order by Totalnumberofminutesspent desc;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement