Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time diff in Amazon Athena / Presto (seconds and minutes )

I have a list of creation time stamps and ending time stamps , i would like to get the amount of seconds last from creation to ending . could not find any way to do that without using UNIX time stamp (which i dont have at the moment) .

something like that :

datediff('second',min(creation_time),max(ending_time))

creation_time = '2017-03-20 10:55:00' ..

like image 969
Latent Avatar asked Mar 20 '17 08:03

Latent


People also ask

What is the difference between Presto and Athena?

Athena is a great choice for getting started with analytics if you have nothing set up yet. Redshift Spectrum is great for Redshift customers. Presto is for everything else, including large data sets, more regular analytics, and higher user concurrency.

Is AWS Athena same as Presto?

AWS Athena is an interactive query service based on Presto that makes it easy to analyze data in Amazon S3 using standard SQL. Athena is serverless, so there is no infrastructure to manage.

How do you get the last day of the month in Athena?

LAST_DAY returns the date of the last day of the month that contains date . The return type is always DATE , regardless of the datatype of date .

How do you get Athena yesterday's date?

SELECT current_date - interval '1' day AS yesterday_in_iso; I love the interval datatype, it reminds me of PostgreSQL.


2 Answers

date_diff

date_diff('second', min(creation_time),max(ending_time))
like image 88
David דודו Markovitz Avatar answered Sep 27 '22 23:09

David דודו Markovitz


unix_timestam() function converts date to seconds passed from 1970-01-01

SELECT 
  (unix_timestamp('2017-03-20 10:55:00') - unix_timestamp('2017-03-20 10:56:00'))

OK
-60

Divide by 60 to get minutes

Edit: The solution above works in Hive. Presto does not have unix_timestamp as @nclark mentioned in the comment. There is to_unixtime function in Presto, it returns DOUBLE, so you need to cast it to bigint. The same logic in Presto:

CAST(to_unixtime(max(ending_time)) AS BIGINT) - CAST(to_unixtime(min(creation_time)) AS BIGINT)
like image 45
leftjoin Avatar answered Sep 27 '22 21:09

leftjoin