Impala date subtraction timestamp and get the result in equivalent days irrespective of difference in hours or year or days or seconds - oracle

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.

Related

Want to make the output human readable

Hello, I am trying to run some queries but my output is not right. Could anyone please help me understand what I'm doing wrong?
I am trying to find the difference between these two DATETIME values as minutes.
SELECT TO_CHAR(booking_StartTime - booking_EndTime) AS Diff FROM Booking;
Assuming that booking_StartTime and booking_EndTime are DATE fields - when doing arithmetic using DATE values in Oracle the result is a number of DAYS. Thus, to get minutes you have to multiply by the number of minutes in a day, i.e. by 24 * 60, or 1440. Also - you probably want to subtract the start time from the end time, in order to get a positive value as the result.
SELECT TO_CHAR((booking_EndTime - booking_StartTime) * 1440) AS Diff FROM Booking;
should get you what you want.

Howe to count an event by minute in Big Query

Many years ago I knew SQL quite well but apparently it's been so long I lost my skills and knolwedge.
I have a number of tables that each track a given event with additional metadata. One piece of Metadata is a timestamp in UTC format(2021-08-11 17:27:27.916007 UTC).
Now I need to count how many times the event occurred per minute.
Col 1, Col2
EventName, Timestamp in UTC
I am trying to recall my past knowledge and also how to apply that to BQ. Any help is appreciated.
If I'm understanding well, you could transform your Timestamp into minutes and then group by it.
SELECT count(*) AS number_events,
FLOOR(UNIX_SECONDS(your_timestamp)/60) AS minute
FROM your_table
GROUP BY FLOOR(UNIX_SECONDS(your_timestamp)/60)
So it transforms your timestamps to unix_seconds, then divide by 60 to get minutes and floor() to skip decimals after the division.
If you have multiple type of events in the same table, just add the name of the event to the select and to the group by
The first step would be to group by event column.
Then the Timestamp events can be counted.
Select Col2_EventName, count(Timestamp )
group by 1
Depending on your data, some more transformation have to be done. E.g. ignore the seconds in the timestamp and hold only the full minutes, as done in the answer from Javier Montón.

Time difference in HIVE

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.

Is there a data type for time format hh:mm:ss in Hive

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

combining date and time and changing it to GMT

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!!

Resources