Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate a sum of type time using sql

Tags:

sql

mysql

How to caculate sum of times of my colonne called "timeSpent" having this format: HH:mm in SQL? I am using MySQL.

the type of my column is Time.

it has this structure

 TimeFrom like  10:00:00           12:00:00     02:00:00 TimeUntil      08:00:00           09:15:00     01:15:00 Time spent        total time 03:15:00 
like image 428
kawtousse Avatar asked Jun 16 '10 14:06

kawtousse


People also ask

How do you find the sum of a value in SQL?

The aggregate function SUM is ideal for computing the sum of a column's values. This function is used in a SELECT statement and takes the name of the column whose values you want to sum. If you do not specify any other columns in the SELECT statement, then the sum will be calculated for all records in the table.


2 Answers

SELECT  SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) AS timeSum   FROM YourTableName   

This worked for me. Hope this helps.

like image 99
Siri Avatar answered Oct 04 '22 22:10

Siri


100% working code to get sum of time out of MYSQL Database:

SELECT SEC_TO_TIME( SUM(time_to_sec(`db`.`tablename`)))  As timeSum FROM `tablename` 

Try and confirm. Thanks.

like image 20
Developerscentral Avatar answered Oct 04 '22 21:10

Developerscentral