Reputation: 321
I have field named "Time" in my table. I want to calculate total hours.
Time 3:00:00 4:00:00 5:00:00
Output should be 2hr. How can I achieve this? Please help me to solve it.
I tried below query but it did not work as expected.
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) AS timeSum
FROM YourTableName
Upvotes: 0
Views: 528
Reputation: 7023
suppose your table have an id as a primary key auto incremental, so you query will be:
SELECT FLOOR(sum( abs( SEC_TO_TIME(TIME_TO_SEC(t1.`timeSpent`)) - SEC_TO_TIME(TIME_TO_SEC(t2.`timeSpent`)) )/10000)) as total
FROM YourTableName t1
inner join YourTableName t2
on t1.id = t2.id + 1;
Upvotes: 0
Reputation: 2412
If you need the maximum time span between values, then try with:
SELECT MAX(timeSpent) - MIN(timeSpent) FROM YourTableName
Upvotes: 2