grooove
grooove

Reputation: 85

SQL summing columns values

I've got a table like this:

ID    Departure              Duration(in min)         
==============================================
1     2017-12-31 11:30:45     120
2     2017-06-16 22:31:46     240

And i want to sum last two columns into:

ID    Departure              Duration(in min)   Arrival       
============================================================
1     2017-12-31 11:30:45     120        2017-12-31 13:30:45  
2     2017-06-16 22:31:46     240        2017-06-17 02:31:46

How can I do this?

Departure is TIMESTAMP

And Duration INT

Upvotes: 0

Views: 61

Answers (4)

Gordon Linoff
Gordon Linoff

Reputation: 1269443

Just add using interval:

select t.*, (departure + interval duration minute) as arrival
from t;

If the above doesn't work (and it should) you can always use date_add():

select t.*, date_add(departure, interval duration minute) as arrival
from t;

Upvotes: 0

Stephan Lechner
Stephan Lechner

Reputation: 35154

You can use the DATE_ADD-function in conjunction with INTERVAL MINUTE:

SELECT flights.*, DATE_ADD(departure, INTERVAL duration MINUTE) as arrival
from (
  SELECT '2017-12-31 11:30:45' AS departure, 120 as duration
  UNION SELECT '2017-06-16 22:31:46', 240) flights

Output from rextester/mysql:

    departure            duration   arrival
1   2017-12-31 11:30:45  120        2017-12-31 13:30:45
2   2017-06-16 22:31:46  240        2017-06-17 02:31:46

Upvotes: 0

reza
reza

Reputation: 1507

You need to use ADDTIME and SEC_TO_TIME function to do your job

SELECT ADDTIME(Departure,SEC_TO_TIME(Duration*60)) as Arrival

for more http://www.w3resource.com/mysql/date-and-time-functions/mysql-addtime-function.php

and http://www.w3resource.com/mysql/date-and-time-functions/mysql-sec_to_time-function.php

Upvotes: 1

Tanmoy Saha
Tanmoy Saha

Reputation: 48

Following MySQL query should work:

SELECT DATE_ADD("2017-12-31 11:30:45", INTERVAL 120 MINUTE);   

Upvotes: 0

Related Questions