select (cast(year('2017-01-22') as int) - cast(year('2016-01-01') as int) )*12 + (cast(month('2017-01-22') as int) - cast(month('2016-01-01') as int) ) + IF(day('2017-01-22') > day('2016-01-01'), 1, 0 )
不考虑白天
select (cast(year('2017-01-22') as int) - cast(year('2016-01-01') as int) )*12 + (cast(month('2017-01-22') as int) - cast(month('2016-01-01') as int) )
3条答案
按热度按时间xv8emn3q1#
你能用下面的。。。考虑到白天
不考虑白天
zlwx9yxi2#
可能是写你自己的自定义项,它会做一些像-
将日期转换为unix时间戳
从较低的时间戳中减去较高的时间戳
结果是两个日期之间的持续时间
将结果除以(60秒60分钟24小时*30天)得到月份
z8dt9xmd3#
自hive 1.2.0起,udf之间的间隔为个月
https://issues.apache.org/jira/browse/hive-9518
手册在这里:https://cwiki.apache.org/confluence/display/hive/languagemanual+udf
两个月之间的时间不仅是年和月,还包括天:
如果您希望它精确地基于月份和年份进行计算,请使用
trunc()
功能: