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:
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:
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;