首页 > 解决方案 > 计算蜂巢中两个日期之间的月份

问题描述

我试图找出 currentdate 和列 (emp_joined) 之间的月份差异小于 24 个月。emp_joined 列的数据类型是 int。为此,我编写了以下查询来获取记录。

select id,emp_joined from table where (abs(month_between(cast(from_unixtime(unix_timestamp(current_date,'yyyy-MM-dd'),'yy-MM-dd'),from_unixtime(unix_timestamp(cast(emp_joined as string),' yyMMdd'),'yy-MM-dd')as int)))<24)

但是该查询返回当前日期和列 emp_joined 之间不少于 24 个月的所有记录。谁能帮我查询。

在此处输入图像描述

标签: datehivehiveql

解决方案


months_between 接受 yyyy-MM-dd 格式的日期。尝试这个

where abs(cast(months_between(current_date, 
                              from_unixtime(unix_timestamp(cast(emp_joined as string),'yyMMdd'),'yyyy-MM-dd')
              )as int)) < 24

推荐阅读