sql - 如何在 Presto SQL 中将 unix 时间戳转换为日期并从中提取月份
问题描述
我有以下查询:
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 列的月份吗?
谢谢你。
解决方案
我建议不要cast
在where
子句中使用:这是低效的,因为该函数需要在过滤之前应用于每一行。
相反,您可以计算对应于月初的时间戳,是否用于直接过滤:
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)
推荐阅读
- string - awk - 使用来自另一个文件的行范围从不同文件中的两个字符串之间的任何内容替换每个 X nth
- sql - mysql创建一个treewalking列表
- regex - Notepad++ 正则表达式在同一行中搜索重复值
- mongodb - 仅当该属性存在时,如何在 mongo 中更新嵌套对象?
- filter - 如何创建过滤器以引入启用了高可用性的 VM
- c# - 在不同的端口上运行 ASP.net 应用程序
- spring-batch - 如何在 Spring 集成中使用控制总线从另一个 JVM 启动/停止集成组件
- join - JPA 存储库通过连接多个列来获取数据
- android - 运行 Electron 应用程序时发生 JavaScript 错误:错误:spawn adb ENOENT
- python-3.x - Selenium navigator:webdriver false 未在 Firefox 94 上启用