I am trying to find the difference between two timestamps in Hive. But the date_time field is STRING, so I need to convert it to date_time format before finding the time difference.
This is the code I am using, but I get NULL.
SELECT UNIX_TIMESTAMP(TO_DATE("2016-12-30 10:39:46"),'HH:MM:SS') - UNIX_TIMESTAMP(TO_DATE("2016-12-30 10:39:31"),'HH:MM:SS');
I would need the difference to be 15 seconds.
Any suggestions would be great !!
Please try this:
select UNIX_TIMESTAMP('2016-12-30 10:39:46') - UNIX_TIMESTAMP('2016-12-30 10:39:31');
It should give time difference in seconds.
Related
I want to subtract two date in impala. I know there is a datediff funciton in impala but if there is two timestamp value how to deal with it, like consider this situation:
select to_date('2022-01-01 15-05-53','yyyy-mm-dd HH24-mi-ss')-to_date('2022-01-01 15-04-53','yyyy-mm-dd HH24-mi-ss') from dual;
There is 1 minute difference and oracle would put the result as 0.000694444 days.
My requirement is if there is any such functionality in impala where I can subtract two timestamp value in the manner 'yyyy-mm-dd HH24-mi-ss', and get the result in equivalent days irrespective of if there is difference in days , year, hours, minute or seconds. Any difference should reflect in equivalent number of days.
Any other way where I can achieve the same thing, I am open to that as well.
Thank you in advance.
You can use unix_timestamp(timestamp) to convert both fields to unixtime (int) format. This is actually seconds from 1970-01-01 and very suitable to calculate date time differences in seconds. Once you have seconds from 1970-01-01, you can easily minus them both to know the differences.
Your sql should be like this -
select
unix_timestamp(to_timestamp('2022-01-01 15-06-53','yyyy-MM-dd HH-mm-ss')) -
unix_timestamp(to_timestamp('2022-01-01 15-05-53','yyyy-MM-dd HH-mm-ss')
) diff_in_seconds
Once youhave difference in seconds, you can easily convert them to minutes/hours/days - whatever format you want it.
I have InfluxDB records that look like this:
Some_Measurement:
---------------------
time field value
----- ------ -----
1630686612 myfieldA 123
1630686612 myfieldB 456
For some reason when I try to graph these in Grafana, or even to a select query like:
SELECT * FROM Some_Measurement WHERE "time" > now() - 60m
I get nothing back. It's almost as if it does not recognize the timestamps as timestamps. I have a feeling this might be because I'm writing these from my source as strings, but I have no idea what the correct data type should be. Could someone please help me out?
This is by default in the terminal query, but you can change this by the below command :
precision rfc3339
After applying the above command it gives the properly formatted time in the select commands
I am processing the files that contains the call details of different users. In the data file, there is a field call_duration which contains the value in the format hh:mm:ss. eg: 00:49:39, 00:20:00 etc
I would like to calculate the the total call duration of each user per month.
I do not see a data type in hive which can stock the time format in hh:mm:ss. ( Currently I have this data as string in my staging table).
I am thinking of writing a UDF which converts the time into seconds, so that i can do a sum(call_duration) grouping by user.
Did any one face a similar situation? Should I go with writing a UDF for is there a better approach?
Thanks a lot in advance
Storing duration as an Integer number of seconds seems like the best option for efficiency and for being able to do calcuations. I don't think you need a custom UDF to convert from your String to an Int. It can be done by combining existing UDFS:
Select 3600 * hours + 60 * minutes + seconds as duration_seconds
FROM (
Select
cast(substr(duration,1,2) as Int) as hours,
cast(substr(duration,4,2) as Int) as minutes,
cast(substr(duration,7,2) as Int) as seconds
From(
Select "01:02:03" as duration) a
) b;
Hive provides built-in date functions to extract hour, minutes and seconds.
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
But if these functions doesn't help you directly and you use many combination of builtin function then i would suggest you to write your own UDF (in case this is very frequent utility and you run over large number of rows). You will see query performance difference.
Hope this helps
I have read many answers for combining date and time and nothing worked so far. I am working in Oracle SQL developer version 3.1.06 and I am trying to combine date and time stamps together. Date is in format dd-mmm-yy. And time is in the following 3 formats-
1. 0348A-- meaning 3:48 am
2. 03:48:00
3. 228 -- meaning minutes from midnight, calculated as (3*60)+48.
And for all these timestamps, I want a query that gets me to this format --
mm/dd/yyyy hh:mm:ss .
I can change the dates and times to string and attach them, but then when I work in powerpivot I am not able to change them to the required format. So, I want to do it in the query itself.
I have already tried something like this-
1. CAST(deptdt as DATETIME)+CAST(time as DATETIME)
2. CAST(depdt AS TIMESTAMP(0)) + (depdt - TIME '00:00:00' HOUR TO SECOND) AS DATETIME
Please help!!
I'm not sure if this is a question or more of an exploration of a possible bug or a question about a better way to do handle this.
I have a rollup report that uses the
select column1id, column2date
from table1
where to_char(column2date,'yyyy-iw') = to_char(to_date('2012-12-31','yyyy-mm-dd'),'yyyy-iw')
The line: to_char(to_date('2012-12-31','yyyy-mm-dd'),'yyyy-iw') is converting to 2012-01, wrapping back to the beginning of the year.
Digging a bit further I find that the date 2012-12-31 is neither included in week: 2012-52 nor is it included in 2013-01, and 2012-53 doesn't return any data either... so I'm at a loss of what's going on here.
https://forums.oracle.com/forums/thread.jspa?threadID=995899
Ravi Kumar wrote: you need to use IYYY in format.
BluShadow wrote: ... When it calculates the YYYY and IW these are independant of each other so it won't reduce the YYYY output to [2013] just because you have included IW in the format mask. It looks at components of the mask and not the whole thing in combination.
select to_char(to_date('2012-12-31','yyyy-mm-dd'),'iyyy-iw') from dual;
returns 2013-01.
I think your WHERE clause should be:
where to_char(column2date,'iyyy-iw') = to_char(to_date('2012-12-31','yyyy-mm-dd'),'iyyy-iw')