I want the query to calculate the difference between two date time fields
to calculate total time taken
two date time fields are in same table
as start_time and stop_time
And I want to calculate total start duration and stop duration
You can use MySQL's UNIX_TIMESTAMP() function to convert your datetime expressions to seconds since the UNIX epoch, then taking the sum of all differences will yield the total duration in seconds:
SELECT SUM(UNIX_TIMESTAMP(stop_time) - UNIX_TIMESTAMP(start_time)) FROM my_table
See it on sqlfiddle.
Note that UNIX_TIMESTAMP() is limited to the range '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
HINT:
SELECT TIMESTAMPDIFF(SECOND,'2012-12-30 12:01:01','2012-12-31 10:02:00');
-- result: 79259 the difference in seconds with the time.
See this link for more details on DateTime functions.
Try this
SELECT TIMEDIFF(STOP_TIME - START_TIME) AS INTERVAL
FROM ......
Related
I have: (this example works)
SELECT *
FROM messages m
WHERE
m.create_timestamp > STR_TO_DATE('2016-01-28 05:06:38', '%Y-%m-%d %H:%i:%s')
ORDER BY m.create_timestamp
LIMIT 100;
but I have this '2016-01-28 05:06:38' datetime in UNIX FORMAT.
I tried:
m.create_timestamp > FROM_UNIXTIME(1453946798, '%Y-%m-%d %H:%i:%s')
But this doesn't work.
I want to compare date in UNIX FORMAT: m.create_timestamp > 1453946798.
Help please!
UNIX format is just the number of milliseconds since 1970-01-01 00:00:00 UTC. So you can just compare the values between them. The highest value will be the point that is later in time.
A simple:
m.create_timestamp > 1453946798
> 1453946798 means it's after 2016-01-28 05:06:38
< 1453946798 means it's before 2016-01-28 05:06:38
Make sure the unix timestamps have the same precision though. Sometimes it's the number of seconds since epoch instead of milliseconds for precision and storage size reasons.
If you have a timestamp in milliseconds to compare with your 1453946798, divide it by 1000 and floor it before comparing the timestamps.
To convert a DATETIME to a TIMESTAMP, you can use the following:
UNIX_TIMESTAMP('2015-01-15 12:00:00');
Of course, replace '2015-01-15 12:00:00' with the date to convert.
Try this
SELECT * FROM messages m
WHERE
m.create_timestamp > FROM_UNIXTIME(create_timestamp)
ORDER BY m.create_timestamp
LIMIT 100
I think you want unix_timestamp(); this command will convert a datetime to a unix timestamp: unix_timestamp('YYYY-MM-DD HH:MM:SS').
I have the following query from Group OHLC-Stockmarket Data into multiple timeframes - Mysql.
SELECT
FLOOR(MIN(`timestamp`)/"+period+")*"+period+" AS timestamp,
SUM(amount) AS volume,
SUM(price*amount)/sum(amount) AS wavg_price,
SUBSTRING_INDEX(MIN(CONCAT(`timestamp`, '_', price)), '_', -1) AS `open`,
MAX(price) AS high,
MIN(price) AS low,
SUBSTRING_INDEX(MAX(CONCAT(`timestamp`, '_', price)), '_', -1) AS `close`
FROM transactions_history -- this table has 3 columns (timestamp, amount, price)
GROUP BY FLOOR(`timestamp`/"+period+")
ORDER BY timestamp
In my select statement, FLOOR(MIN(timestamp)/"+period+")*"+period+" AS timestamp,
I am trying to understand what it is doing. and
I need to convert this back to a mysql date/time Y-M-D H:i:s string or a UTC timestamp for parsing via javascript.
Let's assume that +period+ is 86400 (The number of seconds in a day)
Let's assume that the timestamp is '2015-12-08 20:58:58'
From what I can see, it takes the timestamp, which internally is stored as an integer and divides by 86400.
'2015-12-08 20:58:58' / 86400 = 233231576.4566898000
It then uses the FLOOR operation which would make it 233231576 then multiplies by 86400 again (I assume that this is to ensure rounding to the day)
I end up with 20151208166400.
So that's the 8th December 2015 but I also have 166400 which I have no idea what it is?
So now the second part of the question is, how to convert this integer to 2015-12-08 %H:%i:%s or even a UTC timestamp for parsing via Javascript.
I mentioned the problem in the comment, but not a fix. The problem is that the proposed code is for a unix timestamp, not a datetime value.
This can be fixed by doing appropriate conversions
SELECT FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(MIN(timestamp)) / $period) * $period)
This gives you the flexibility of have arbitrary numbers of seconds for the groupings.
You're right that FLOOR(timestamp / 86400) * 86400 is a crude way of rounding a UNIX-style timestamp (seconds since 1970-01-01 00:00UTC) to midnight on the present day UTC.
If that's what you're trying to do, I suggest you try this kind of MySQL code:
SELECT DATE_FORMAT(DATE(`timestamp`), '%Y-%m-%d'),
...
GROUP BY DATE(`timestamp`)
This uses MySQL's built in date arithmetic to turn a timestamp into midnight.
But you should be careful of one thing. Those timestamps are all stored in UTC (f/k/a Greenwich Mean Time). When you do date arithmetic with them, or pull them out of the database to use them, they're automatically converted to local time according to your MySQL time zone settings.
It is rounding timestampt to period (e.g day).
DATE_FORMAT( DATE( FLOOR(MIN(timestamp)/"+period+")*"+period+" ) , '%Y-%m-%d %H:%i:%s' )
If period==day consider using only MySQL period rounding by DAY().
Convert a Date object to a string, according to universal time:
var d = new Date();
var n = d.toUTCString();
The result of n will be:
Mon, 28 Dec 2015 12:57:32 GMT
I need to get the timestamp of interval of 7 days from the current time in milliseconds. I tried date_sub using now() but didn't work for me. How do we do this in hive. I need exactly the interval current_timestamp(unix) and interval of 7 days from the current in my query. Also is there any provision to select the time zone like UTC + 5:30 hrs like that?
I could not find information about millisecond based time calculations in HIVE.
unix_timestamp() is the current timestamp, but it does not have milliseconds.
The offset is 7 days*24 hours/day*3600 secs/hour = 604800 milliseconds
So the timestamp of the current time plus 7 days would be unix_timestamp() + 604800
The UTC part is trickier; you can use to_utc_timestamp, giving it your calculated timestamp, and the timezone it is coming from (as a date). It will return a date string, which you will pass through unix_timestamp()
In other words, assuming it is coming from PST, you should use:
select unix_timestamp(to_utc_timestamp(from_unixtime(unix_timestamp() + 604800), 'PST')) from dual;
See the documentation here:
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF
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
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.