【问题标题】:Presto - static date and timestamp in where clausePresto - where 子句中的静态日期和时间戳
【发布时间】:2016-10-28 12:12:24
【问题描述】:

我很确定以下查询曾经在 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;

但是感觉很脏... 有没有更好的方法来做到这一点?

【问题讨论】:

    标签: presto


    【解决方案1】:

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

    【讨论】:

    • 谢谢,在字符串日期之前添加关键字date。我的查询SELECT * FROM db.table_1 WHERE date_col > date '2018-01-01' LIMIT 100
    • 嗨 - 我有一个date 类型的列,但是当我运行SELECT COUNT(*) FROM <table> where dt >= DATE '2019-01-01' 时,我看到Mismatched Domain types: date vs integer;还有其他解决方法可以提供帮助吗?
    【解决方案2】:

    想一想..您是否尝试过在约会中省略破折号?试试20160623 而不是2016-06-23

    我在 SQL server 上遇到过类似的情况,但没有使用 Presto。

    【讨论】:

    • nope 不起作用.. 现在它将日期解释为整数('=' 不能应用于日期,整数)
    猜你喜欢
    • 2023-03-21
    • 2010-12-29
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多