I have a table of MySQL data. Each row has its own data. In that row there is start_time and end_time. Basically, when you started doing an objective and when you finished (inserted into the database). Like a timer of sorts.
How would I get the average of of taking the unix timestamp of start_time and end_time. I know you would minus the end_time by start_time to get the difference (in milliseconds?) and from there... not sure what else.
unix_timestamp of a date column returns its representation as seconds from the epoc, so subtracting two of these will give a difference in seconds. Like any other number, you can apply the aggregate avg function to it in order to get an average:
SELECT AVG (UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time))
FROM my_table
Once you have this result, you could manipulate it in any way you like. One useful manipulation would be to use sec_to_time to convert a number of seconds to a HH:MM:SS format (e.g., 183 seconds would be represented as 00:03:03 hours):
SELECT SEC_TO_TIME
(AVG (UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time)))
FROM my_table
Related
My table looks something like this:
I want to subtract end_date from start_date and it should also subtract hh:mm:ss. The expected output for above should be 00:04:01
I tried multiple ways, but could not figure this out.
How can I do this? I am doing in MySQL
You can use timestampdiff() to compute the difference between both datetimes in seconds, and then sec_to_time() to turn the result to a time:
sec_to_time(timestampdiff(second, start_date, end_date))
Note that the time datatype stores values up to about 840 hours.
Every minute I have a value in my database. Each record has a timestamp, and a value.
"30695","2015-09-06 18:10:09","693"
I want to get the average value for each minute for each day of the week, but I'm not sure how to build a query for this.
My thinking is that I first need to group all the records by DATEPART(weekday,[time]), which will put all my data into Sunday, Monday, Tuesday, etc. Next, I would group by minute, rounded. (So 2015/11/30 06:41:28 would get grouped with 2015/11/23 06:41:56 (both dates are Mondays)). Then, take the average of the value at each minute--7*24*60 = 10,080 total values.
There is no datepart() function in mysql, but you can use dayofweek() to get the day of the week and minute() to get the minute part:
select dayofweek(timestamp) as day, minute(timestamp) as minute, avg(value_field) as average
from table
group by dayofweek(timestamp), minute(timestamp);
Got it working, adapted from Shadow's answer, which wasn't quite right. Here's my query (MySQL 5.5)
SELECT weekday(timestamp) as day_of_week, avg(avg) as average, DATE_FORMAT(timestamp,'%H:%i') as hour_time
FROM audio
GOUP BY hour_time, day_of_week
ORDER BY `day_of_week` ASC
I need to pull a variable amount of days of data from a MySQL database that stores the date as a UNIX timestamp in an int column.
For example, if I need the last 5 days of data, what would my query be?
(All queries would start from current date and would go back x amount of days).
Timestamp is considered one of the Date and Time types and therefore any of the Date Time Functions can be used on it.
SELECT * FROM your_table
WHERE Ftimestamp_column > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 5 DAY));
I've never tried it but there's a MySQL function to convert unix timestamps into MySQL dates and then you can use DATE_SUB() or whatever. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
I wasn't able to find out (googling, reading mysql reference manual) how to get value of DATETIME in seconds in MySQL.
I dont mean to extract seconds from datetime, but to convert it into seconds.
If by "convert to seconds", you mean "convert to an UNIX Timestamp" (i.e. number of seconds since 1970-01-01), then you can use the UNIX_TIMESTAMP function :
select UNIX_TIMESTAMP(your_datetime_field)
from your_table
where ...
And, for the sake of completness, to convert from an Unix Timestamp to a datetime, you can use the FROM_UNIXTIME function.
If you want to have the difference between two DATETIME values, use TIMESTAMPDIFF:
TIMESTAMPDIFF(unit,datetime_expr1,datetime_expr2)
Returns datetime_expr2 – datetime_expr1, where datetime_expr1 and datetime_expr2 are date or datetime expressions. One expression may be a date and the other a datetime; a date value is treated as a datetime having the time part '00:00:00' where necessary. The unit for the result (an integer) is given by the unit argument. The legal values for unit are the same as those listed in the description of the TIMESTAMPADD() function.
mysql> SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01');
-> 3
mysql> SELECT TIMESTAMPDIFF(YEAR,'2002-05-01','2001-01-01');
-> -1
mysql> SELECT TIMESTAMPDIFF(MINUTE,'2003-02-01','2003-05-01 12:05:55');
-> 128885
unit can also be HOUR which is what you asked for in one of the comments.
The unit argument can be any of the following:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
The level of usefulness of some of the other options will of course be determined by the granularity of the data. For instance, "MICROSECOND" will only have limited use if you are not storing microseconds in your DATETIME values.
Use TIME_TO_SEC in previous versions for mysql
SELECT TIME_TO_SEC(time column) FROM table
i used in mysql
TO_SECONDS(your date goes here) method to convert date to seconds from year 0
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
The function UNIX_TIMESTAMP(datetime) returns the unix time, which happens to be the number of seconds since 1-Jan-1970 0000 UTC. That may be what you need, but not if you're dealing with dates of birth, historical dates, or dates after 2037.
Starting in mysql 5.5.0 you can use to_seconds()
TO_SECONDS(FIELD_NAME)
FIELD_NAME must be DATETIME type
I have created my own query for your problem:
SELECT HOUR(`colname`) * 3600 + MINUTE(`colname`) * 60 + SECOND(`colname`)
FROM widgets
WHERE id = 1;
Use id = 1 if you have to take a specific row.
The output will be in seconds.
I want to subtract between two date time values using SQL in MySQL such that I get the interval in minutes or seconds. Any ideas? I want to run a SQL query that retrieves uses from a database who have logged in like 10 minutes from the time.
There are functions TIMEDIFF(expr1,expr2), which returns the value of expr1-expr2, and TIME_TO_SEC(expr3), which expresses expr3 in seconds.
Note that expr1 and expr2 are datetime values, and expr3 is a time value only.
Check this link for more info.
TIMESTAMPDIFF is like TIMEDIFF which Matthew states, except it returns the difference of the two datetimes in whatever units you desire (seconds, minutes, hours, etc).
For example,
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
Would return the number of minutes the user was logged in (assuming you stored this kind of thing in a table like that).
I would do it like this - fetch where last activity is within 10 mins of now
SELECT * FROM table WHERE last_activity >= DATE_SUB(NOW(), INTERVAL 10 MINUTE)
SELECT TIMESTAMPDIFF(MINUTE,LogOutTime,LogInTime) AS TimeLoggedIn
FROM LogTable
This example shall ruin the time if its used by using millitary time. So for calculating millitairy time I do not recommend it Because it outputs negative values.
You can try and cast them to Unix Time stamp, and take the difference.