sql - HiveQL 选择特定日期范围内的生日
问题描述
我有下表persons
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
birthdays
在这些日期之间选择
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')
)
基本上,您只需要比较月份和日期,而不是年份。然后,您必须考虑期限何时超过一年。
推荐阅读
- matlab - 有没有办法在 MATLAB/Psychtoolbox 中覆盖下划线?
- python - 数组 python || 时间.strftime
- jenkins - 如何将 Jenkins 凭据传递给 gradle?
- neo4j - 使用 apoc load jdbc 进行嵌套 apoc 迭代返回“无法以 CALL 结束”消息
- java - 在java中同步一个没有在类中使用的Object类对象
- scala - 如何在scala中合并多个导入?
- r - 如何从包含R中另一个向量的子字符串的向量中提取元素?
- javascript - 导航到不同的路线然后返回后,具体化 css 视差图像消失
- machine-learning - HMM - 如果所有转换都可能,Foward-Backward 算法是否具有与 Viterbi 相同的结果?
- elasticsearch - 如何在不过滤结果的情况下影响 Elasticsearch 中的结果分数?