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

时间:2016-06-26 10:49:14

标签: 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;

现在当我运行它时(在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;

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

2 个答案:

答案 0 :(得分:42)

与其他一些数据库不同,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'

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

答案 1 :(得分:-3)

快速思考..你试过在你的约会中省略破折号吗?尝试20160623而不是2016-06-23

我遇到过与SQL Server类似的东西,但未使用Presto。