首页 > 解决方案 > HIVE在哪里日期过滤x天?字符串格式

问题描述

所以我们的 DBA 将我们的配置单元表设置为日期列作为分区列,但作为“字符串”YYYYMMDD 格式。

如何在 WHERE 中过滤此“日期”列以显示过去 30 天之类的内容?

标签: stringhivewhere-clause

解决方案


请使用date_format将 systemdate - 30 days 格式化为 YYYYMMDD,然后与您的分区列进行比较。请注意按原样使用分区列,以便 hive 可以选择正确的分区。
当您想选择前 30 天的数据时 -

select * 
from  mytable 
where partition_col = date_format( current_date() - interval '30' days, 'yyyyMMdd')

如果您想要过去 30 天以来的所有数据 -

select * 
from  mytable 
wherecast(partition_col as INT) >= cast(date_format( current_date() - interval '30' days, 'yyyyMMdd') as INT)

铸造不应该影响分区的好处,但你需要在使用它之前检查性能。请在这种情况下返回。


推荐阅读