Presto - where子句中的静态日期和时间戳

Tal*_*ffe 32 presto

我很确定以下查询曾经在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;
Run Code Online (Sandbox Code Playgroud)

现在,当我运行它(在EMR上的Presto 0.147上)时,我收到错误,试图将varchar分配给日期/时间戳.

我可以使用它:

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;
Run Code Online (Sandbox Code Playgroud)

但感觉很脏......有没有更好的方法呢?

Dav*_*ips 58

与其他一些数据库不同,Presto不会自动在varchar和其他类型之间进行转换,即使对于常量也是如此.转换工作,但更简单的方法是使用类型构造函数:

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'
Run Code Online (Sandbox Code Playgroud)

您可以在此处查看各种类型的示例:https://prestodb.io/docs/current/language/types.html

  • 谢谢,在字符串日期之前添加关键字`date`。我的查询`SELECT * FROM db.table_1 WHERE date_col > date '2018-01-01' LIMIT 100` (2认同)