Sql HiveQL选择特定日期范围内的生日
我有下表的Sql HiveQL选择特定日期范围内的生日,sql,hive,hiveql,Sql,Hive,Hiveql,我有下表的人员 id name location dob 1 John NY 1978-08-02 2 Nancy LA 1993-10-12 3 Smith GA 1986-09-25 4 Emily MN 1988-04-14 5 Lily MN 1978-11-02 如何编写HiveQL以选择当前日期和2个月前的生日: 比如说, select current_date >> 2018-09
人员
id name location dob
1 John NY 1978-08-02
2 Nancy LA 1993-10-12
3 Smith GA 1986-09-25
4 Emily MN 1988-04-14
5 Lily MN 1978-11-02
如何编写HiveQL以选择当前日期和2个月前的生日:
比如说,
select current_date
>> 2018-09-27
select add_months(current_date,-2);
>> 2018-07-27
选择这些日期之间的生日
id name location dob
1 John NY 1978-08-02
3 Smith GA 1986-09-25
生日问题很棘手:
select t.*
from t
where (year(current_date) = year(add_months(current_date, -2)) and
date_format(dob, 'MM-DD') between date_format(add_months(current_date, -2), 'MM-DD') and date_format(current_date, 'MM-DD')
) or
(year(current_date) > year(add_months(current_date, -2)) and
date_format(dob, 'MM-DD') not between date_format(current_date, 'MM-DD') and date_format(add_months(current_date, -2), 'MM-DD')
)
基本上,您只需要对月份和日期进行比较,而不是对年份进行比较。然后,当周期超过一年边界时,您必须考虑