Hadoop 使用hive计算从出生日期算起的年龄

Hadoop 使用hive计算从出生日期算起的年龄,hadoop,hive,Hadoop,Hive,我想从给定的用户出生日期计算年龄。 我怎么做?配置单元中是否有任何特定命令可用? 没找到多少。我尝试使用DATEDIFF,但没有得到正确的输出,只给出null SELECT FLOOR(DATEDIFF(DAY, user_dob, unix_timestamp()) / 365.25) 试试这个: select floor(datediff(to_date(from_unixtime(unix_timestamp())), to_date(cust_brth_day)) /

我想从给定的用户出生日期计算年龄。 我怎么做?配置单元中是否有任何特定命令可用? 没找到多少。我尝试使用DATEDIFF,但没有得到正确的输出,只给出null

SELECT FLOOR(DATEDIFF(DAY, user_dob, unix_timestamp()) / 365.25)
试试这个:

select 
   floor(datediff(to_date(from_unixtime(unix_timestamp())), 
   to_date(cust_brth_day)) / 365.25) 
from some_table;
试试这个:

select 
   floor(datediff(to_date(from_unixtime(unix_timestamp())), 
   to_date(cust_brth_day)) / 365.25) 
from some_table;

当前年份减去出生年份
如果他们的出生月份大于当前月份,则加上-1
如果他们的出生月份等于当前月份,则加上 并且月的出生日期大于月的当前日期-1

select birth_date
, current_year
, (year(current_date) - year(birth_date) 
   +case when month(birth_date) > month(current_date) then -1
         when month(birth_date) = month(current_date) and day(birth_date) > day(current_date) then -1
         else 0 
     end) as age
from person

当前年份减去出生年份
如果他们的出生月份大于当前月份,则加上-1
如果他们的出生月份等于当前月份,则加上 并且月的出生日期大于月的当前日期-1

select birth_date
, current_year
, (year(current_date) - year(birth_date) 
   +case when month(birth_date) > month(current_date) then -1
         when month(birth_date) = month(current_date) and day(birth_date) > day(current_date) then -1
         else 0 
     end) as age
from person

你得到了什么结果?你看起来大致上走对了方向。另外,用户_dob的格式是什么?也许它没有正确地转换为日期1993年2月14日12:00:00这就是我的出生日期的样子你得到了什么结果?你看起来大致上走对了方向。另外,用户_dob的格式是什么?也许它没有正确地转换为日期1993年2月14日12:00:00这就是我的出生日期的样子