【问题标题】:How to cast unix timestamp as date and extract month from it in Presto SQL如何在 Presto SQL 中将 unix 时间戳转换为日期并从中提取月份
【发布时间】:2020-06-25 01:33:49
【问题描述】:

我有以下疑问:

    select cast(ov.ingestion_timestamp as date), date(ov.date_id), cast(ov.main_category as varchar), 
    sum(cast(ov.order_target as int)),
    sum(cast(ov.gmv_target as int))
    from tableA ov 
    inner join tableB cb
    on date(ov.date_id) = date(cb.ingestion_timestamp)
    inner join tableC loc  
    on date(ov.date_id) = date(loc.ingestion_timestamp)
    where MONTH(date(ov.ingestion_timestamp)) = month(current_date)
group by 1,2,3

我想获取 ingestion_timestamp 列的月份等于当前月份的记录。所有列值都存储为对象,因此我需要转换为它们各自的数据类型。我可以知道如何检索 ingestion_timestamp 列的月份吗?

谢谢。

【问题讨论】:

    标签: sql date where-clause presto


    【解决方案1】:

    我建议不要在where 子句中使用casting:这是低效的,因为该函数需要在过滤之前应用于每一行。

    相反,您可以计算对应于月初的时间戳,是否用于直接过滤:

    where ingestion_timestamp >= to_unixtime(date_trunc(month, current_date))
    

    如果您有未来的日期,您可以添加上限

    where 
        ingestion_timestamp >= to_unixtime(date_trunc(month, current_date))
        and ingestion_timestam < to_unixtime(date_trunc(month, current_date) + interval '1' month)
    

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 2018-03-07
      • 2015-05-23
      • 1970-01-01
      • 2020-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多