Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presto - static date and timestamp in where clause

Tags:

presto

I'm pretty sure the following query used to work for me on Presto:

select segment, sum(count)
from modeling_trends
where segment='2557172' and date = '2016-06-23' and count_time between '2016-06-23 14:00:00.000' and '2016-06-23 14:59:59.000';
group by 1;

now when I run it (on Presto 0.147 on EMR) I get an error of trying to assigning varchar to date/timestamp..

I can make it work using:

select segment, sum(count)
from modeling_trends
where segment='2557172' and date = cast('2016-06-23' as date) and count_time between cast('2016-06-23 14:00:00.000' as TIMESTAMP) and cast('2016-06-23 14:59:59.000' as TIMESTAMP)
group by segment;

but it feels dirty... is there a better way to do this?

like image 367
Tal Joffe Avatar asked Jun 26 '16 10:06

Tal Joffe


People also ask

How do I cast a timestamp to date in Presto?

You can convert timestamp to date with cast(col as date) or date(col) .

How do I do a timestamp in SQL?

The basic syntax of “timestamp” data type in SQL is as follows : Timestamp 'date_expression time_expression'; A valid timestamp data expression consists of a date and a time, followed by an optional BC or AD.

How do I check my Presto data type?

Presto has a typeof() function to make finding out data types of values easy. This is particularly useful when you are getting values from nested maps for example and the data types need to be determined. Armed with this info you should now be able to find out the data types of values.


1 Answers

Unlike some other databases, Presto doesn't automatically convert between varchar and other types, even for constants. The cast works, but a simpler way is to use the type constructors:

WHERE segment = '2557172'
  AND date = date '2016-06-23'
  AND count_time BETWEEN timestamp '2016-06-23 14:00:00.000' AND timestamp '2016-06-23 14:59:59.000'

You can see examples for various types here: https://prestosql.io/docs/current/language/types.html

like image 110
David Phillips Avatar answered Oct 08 '22 04:10

David Phillips