Data type and sum time duration - mysql

What I have:
A MySQL table consisting of a "duration" column.
05:00:00
00:30:00
00:30:00
00:15:00
01:45:00
00:30:00
01:30:00
02:30:00
01:00:00
01:30:00
Data Type:
I first used TIME but from what I've researched, TIME is for storing a point in time rather than a duration of time. TIME also represents a limitation in that it can't exceed 23 hours.
I've instead opted for the DATETIME data type. My column now looks as follows:
0000-00-00 05:00:00
0000-00-00 00:30:00
0000-00-00 00:30:00
0000-00-00 00:15:00
0000-00-00 01:45:00
0000-00-00 00:30:00
0000-00-00 01:30:00
0000-00-00 02:30:00
0000-00-00 01:00:00
0000-00-00 01:30:00
Sum:
I need the total duration. I.e. SUM of the "duration" column in HH:MM:SS.
Summing the column produces "134000".
SELECT sum(`duration`) FROM `my_table`
My question:
How do I sum the duration column in a HH:MM:SS format?

SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(`duration`)));
TIME_TO_SEC - will convert datetime to seconds. Then the SUM in your case will be 54000
SEC_TO_TIME - convert it back to datetime format
Output
15:00:00

Related

How to convert date range into whole number (integer) in SQL

Let's say I have a date 2013-03-01 and date 2013-04-02. How do I get a integer value between that date, for example in this case 2 days. In SQL kindly advise how to construct the query
day_date
---------------------
2005-07-29 00:00:00
2013-03-01 00:00:00
2013-04-02 00:00:00
2013-06-01 00:00:00
2013-10-19 00:00:00
2013-10-23 00:00:00
2013-12-31 00:00:00
The DATEDIFF() function returns the time between two dates.
SELECT DATEDIFF('2014-11-30','2014-11-29') AS DiffDate

Convert MySQL now() based on timezone of rows being searched

Suppose I have data in a table "events" structured like this:
eventid | datetime_start | datetime_end | timezone
001 | 2016-01-01 10:00:00 | 2016-01-01 14:00:00 | America/Los_Angeles
002 | 2016-01-03 19:00:00 | 2016-01-03 22:00:00 | America/Los_Angeles
003 | 2016-01-17 02:00:00 | 2016-01-17 06:00:00 | America/New_York
004 | 2016-01-31 23:00:00 | 2016-02-01 01:00:00 | America/Los_Angeles
The timezone column allows dates/times to be stored exactly as entered rather than normalized to UTC, GMT, etc.
I want to query the table to find eventids where "now()" falls between datetime_start and datetime_end:
SELECT eventid FROM events WHERE now() BETWEEN datetime_start AND datetime_end
However, since "now()" is based on a fixed timezone (UTC in my case), is there any way to convert "now()" to match the timezone column row by row as it searches? Maybe something in the spirit of the following:
SELECT eventid FROM events WHERE CONVERT_TZ(now(),'UTC',timezone) BETWEEN datetime_start
AND datetime_end

How to use DATE_SUB and hours interval in mysql using in vb.net

SELECT DATE_SUB('2014-01-01 01:00:00', INTERVAL 3 HOUR) ;
I want to use this concept to vb.net
-I have a textbox which store the datetime format
Example no1, I have 2014-01-01 08:00:00 in my textbox and I want to minus 3 hours
Expected Output should be 2014-01-01 05:00:00
Example no2, I have 2014-01-01 01:00:00 in my textbox and I want to minus 3 hours
Expected Output should be 2014-12-31 22:00:00
how about:
DateTime.Parse( '2014-01-01 01:00:00' ).AddHours( -3 );

Mysql order date by the m/h/s

How to order date, like this - 2012-02-01 00:00:00 by the hour,minutes,and the seconds, not by the year/moth/day.
If i have..
2012-02-01 02:00:00
2012-03-01 20:00:00
2012-04-01 12:00:00
2012-05-01 07:00:00
I wan't to get this output.
Column tipe is timestamp.
2012-02-01 02:00:00
2012-05-01 07:00:00
2012-04-01 12:00:00
2012-03-01 20:00:00
ORDER BY TIME(date_column)
This will, however, slow down your queries, as it isn't possible to index the on-the-fly calculation. If you have a lot of records, or if this query runs frequently, you should break the time portion of the date into its own column so you can index it for faster sorting.
Use the TIME() function to extract the time portion of the expression passed, e.g.
mysql> SELECT TIME('2012-02-01 02:00:00');
-> '02:00:00'
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time

finding date intervals start and end dates

I have a table like this (plus 10 more columns) containing more than 1 million of frequently updated records:
id pid start_date end_date
1 761 2011-07-25 00:00:00 2011-08-01 00:00:00
2 761 2011-08-01 00:00:00 2011-08-22 00:00:00
3 761 2011-08-22 00:00:00 2011-09-19 00:00:00
4 802 2011-08-22 00:00:00 2011-09-19 00:00:00
5 761 2011-06-05 00:00:00 2011-07-05 00:00:00
and would like to get result for a particular pid (761 in the example below) with all consecutive intervals combined:
id pid start_date end_date
1 761 2011-07-25 00:00:00 2011-09-19 00:00:00
5 761 2011-06-05 00:00:00 2011-07-05 00:00:00
Currently I am doing this in the code, but would like to move this functionality entirely to the db side.
Any ideas how to do this?
edit: start_date and end_date columns are of DATETIME type.
This is really much better done in code. Loop over the rows, when it's for the same product, update the end date, otherwise create a new array entry.
For an idea of how complex this is in SQL, see my attempt at solving this in SQL Server :)