Timestamp operations in hive - sql

How can I subtract 2 timestamp columns in hive and store the result in a separate column in its equivalent hours format?

Let's say if you have timestamp in the given format : 2016-10-16 10:51:00.000
You can try following:
SELECT
cast(
round(
cast((e-s) as double) * 1000
) as int
) time_difference
FROM (SELECT cast(starttime as double) s, cast(endtime as double) e from table1) q;
It will give you the difference of both timestamps in millisecond. Then you can convert it to your expected format(hours,days etc.) .

Use the unix_timestamp function to convert a hive timestamp to a number of seconds since epoch. Subtracting the unix timestemp results gives an answer in seconds.
SELECT start_dttm,
(unix_timestamp(end_dttm) - unix_timestamp(start_dttm))/60 AS duration_in_minutes
FROM dev_edw.audit_history_hb
WHERE script_name LIKE '%0_hive_d%'
AND parent_audit_id is null
ORDER BY start_dttm desc

Related

SQL difference between two datetime columns

I have a dataset with 2 columns of datetime datatype as shown here:
I want to take the difference between the two dates and I try it with this code:
Select
*,
original_due_date - due_date as difference
from
Table
However I'm not sure if the same would suffice as this is a datetime and not just date.
Any inputs would be much appreciated.
Desired output
The question was originally tagged Postgres, so this answers the original question.
Presumably, you are storing the values as timestamps. If you just want the results in days, then convert to dates and take the difference:
Select t.*,
(t.original_due_date::date - t.due_date::date) AS difference
from Table t;
If you want fractional days, then a pretty simple method is to extract the "epoch", which is measured in seconds, and use arithmetic:
Select t.*,
( extract(epoch from t.original_due_date -
extract(epoch from t.due_date
) / (24.0 * 60 * 60) AS decimal_days
from Table t;
transform timestamps to seconds (unix_timestamp), calculate difference and divide by (60*60*24) to get days
select (unix_timestamp(original_due_date, 'MM-dd-yyyy HH:mm')-unix_timestamp(due_date, 'MM-dd-yyyy HH:mm'))/(60*60*24) as difference_days
from (select '07-01-2021 00:00' as due_date, '02-10-2020 00:00' as original_due_date) t
Result:
-507

Convert seconds to days, hours, minutes in Bigquery

i'm having trouble in converting seconds in Bigquery, is there any function to convert seconds to hour:minute:second format in Bigquery? i already tried the TIMESTAMP_SECONDS() function but it also returns some date and i can't use it if the hour more than 23.
for example:
second= 100000
result= 27:46:40
or maybe as 1 day 3 hour 46 minute 40 second
and i also want it in timestamp datatype so i can order it ascending or descending.
Below is for BigQuery Standard SQL
#standardSQL
select seconds,
regexp_replace(
cast(time(ts) as string),
r'^\d\d',
cast(extract(hour from time(ts)) + 24 * unix_date(date(ts)) as string)
) as option1,
format(
'%i day %i hour %i minute %i second',
unix_date(date(ts)),
extract(hour from time(ts)),
extract(minute from time(ts)),
extract(second from time(ts))
) as option2
from `project.dataset.table`,
unnest([timestamp_seconds(seconds)]) ts
if to apply to sample data from your question as in
with `project.dataset.table` AS (
select 100000 seconds union all
select 200000 union all
select 300000
)
the output is
With recently introduced INTERVAL data type and respective functions - such conversion becomes much easier
select seconds,
make_interval(second => seconds) result,
justify_interval(make_interval(second => seconds)) normalized_result
from `project.dataset.table`
with output like

Select entries created after a date, which have big integer timestamp

The table has a created_date column which has big integer as time stamp values. One of the time stamp looks like this 1596007131121. How can I query this?
select count(*) from user where created_date: date >='2020-08-30';
I need to query this.
You can convert that to a proper timestamp using the to_timestamp() function:
select *
from the_table
where to_timestamp(created_date/1000::bigint) >= date '2020-08-30';
But I would highly recommend to convert that column to a proper timestamp column.
I think you want:
select '1970-01-01'::timestamp + (created_date / 1000) * interval '1 second'
If you want this in a where clause, then use:
where created_date >= extract(epoch from '2020-08-30') * 1000
This has the nice feature that you can use an index.

How to Sum (Time(n)) in Sql Server?

How want to use the sum of the time(n) operator so that i can calculate the overall total of the time but Sql server saying can't add the Time(n) column
i have a casted column which contain difference of two dates, and being casted as Time(n) by me. Now i want to add those column to get how much time i had used in total How much hours minute and seconds so i apply
select Sum(cast ((date1-date2) as Time(0))) from ABC_tbl
where date1 is reaching time and date2 is startingtime in Date format and i want to total of all hours
Convert the time to an integer value before you sum it (for example, seconds):
SELECT SUM(
datediff(second, '00:00:00', [TimeCol])
)
FROM
...
Replace [TimeCol] with the name of the Time(n) column. This gives you the total time in seconds, which you can then easily convert to minutes, hours, etc...
Hope this example help you.
DECLARE #A TABLE (SD TIME(0),ED TIME(0))
INSERT INTO #A VALUES
('09:01:09','17:59:09'),
('09:08:09','16:10:09'),
('08:55:05','18:00:00')
SELECT SUM(DATEDIFF(MINUTE,SD,ED)) SUM_IN_MINUTES,
SUM(DATEDIFF(HOUR,SD,ED)) SUM_IN_HOURS
FROM #A
Result:
SUM_IN_MINUTES | SUM_IN_HOURS
---------------------------------------
1505 | 25
select Sum(DATEDIFF(Minute,date1,date2)) AS TIME from ABC_tbl
u have to calculate the date difference with DATEDIFF function then use SUM function to calculate your sum of time.
you can change Minute to Second-Hour-month etc..
Try this:
DECLARE
#MidnightTime TIME = '00:00:00.0000000',
#MidnightDateTime DATETIME2 = '0001-01-01 00:00:00.0000000';
SELECT SumOfTime = DATEADD(SECOND, SUM ( DATEDIFF(SECOND, #MidnightTime, x.Col1) ), #MidnightDateTime)
FROM (VALUES
(1, CONVERT(TIME, '10:10:10.0000001')),
(2, CONVERT(TIME, '00:00:05.0000002')),
(3, CONVERT(TIME, '23:59:59.0000003'))
) x(ID, Col1)
/*
SumOfTime
---------------------------
0001-01-02 10:10:14.0000000 = 1 day (!), 10 hours, 10 minutes, 14 seconds
*/
Note: instead of SECOND you could use another precision: MINUTE, HOUR or ... NANOSECOND (see section Arguments > datepart). Using a higher precision could leads to Arithmetic overflow errors (use CONVERT(BIGINT|NUMERIC(...,0), ...).
Note #2: because the precision is SECOND the result (SumOfTime) has 0000000 nanoseconds.

How to convert Epoch time to date?

Hi I have a column with number datatype
the data like 1310112000 this is a date, but I don't know how to make it in an understandable format:
ex: 10-mar-2013 12:00:00 pm
Can any one please help me.
That is EPOCH time: number of seconds since Epoch(1970-01-01). Use this:
SELECT CAST(DATE '1970-01-01' + ( 1 / 24 / 60 / 60 ) * '1310112003' AS TIMESTAMP) FROM DUAL;
Result:
08-JUL-11 08.00.03.000000000 AM
Please try
select from_unixtime(floor(EPOCH_TIMESTAMP/1000)) from table;
This will give the result like E.g: 2018-03-22 07:10:45
PFB refence from MYSQL
In Microsoft SQL Server, the previous answers did not work for me. But the following does work.
SELECT created_time AS created_time_raw,
dateadd( second, created_time, CAST( '1970-01-01' as datetime ) ) AS created_time_dt
FROM person
person is a database table, and created_time is an integer field whose value is a number of seconds since epoch.
There may be other ways to do the datetime arithmetic. But this is the first thing that worked. I do not know if it is MSSQL specific.