string - HIVE在哪里日期过滤x天?字符串格式
问题描述
所以我们的 DBA 将我们的配置单元表设置为日期列作为分区列,但作为“字符串”YYYYMMDD 格式。
如何在 WHERE 中过滤此“日期”列以显示过去 30 天之类的内容?
解决方案
请使用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)
铸造不应该影响分区的好处,但你需要在使用它之前检查性能。请在这种情况下返回。
推荐阅读
- java - 为什么 Spring Batch 从 MASTER 而不是从用户定义的 Schema 中读取元数据表?
- awk - 2个文件,匹配列并从2列中减去
- python - 蜗牛算法 - 控制遍历的最大/最小位置
- python - 将计算变量中的值与 A 列匹配,并创建一个新的 B 列,其中包含 B 列中 A 列的所有匹配行
- javascript - 拆分字符串并在表中搜索
- angular - 有没有办法在为特定组件分离更改检测的同时进行角度重新渲染视图
- r - dplyr:让 top_n 打破关系
- c# - 如何获取 webview 当前持有的网页标题?
- jquery - JQuery Div中的三元运算符条件
- java - 如何进行不同类型的对象状态验证