How to sum rows of VARCHAR datatype or TIME datatype in MySQL?


Let us first create a −

mysql> create table DemoTable1442
   -> (
   -> DueTime time
   -> );
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1442 values('00:08:00');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable1442 values('00:04:00');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable1442 values('12:55:00');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select −

mysql> select * from DemoTable1442;

This will produce the following output −

+----------+
| DueTime  |
+----------+
| 00:08:00 |
| 00:04:00 |
| 12:55:00 |
+----------+
3 rows in set (0.00 sec)

Following is the query to sum rows of VARCHAR datatype or TIME datatype in MySQL −

mysql> select sec_to_time(sum(time_to_sec(DueTime) ) ) as TotalTime from DemoTable1442;

This will produce the following output −

+-----------+
| TotalTime |
+-----------+
| 13:07:00  |
+-----------+
1 row in set (0.03 sec)

Updated on: 12-Nov-2019

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements