【问题标题】:calculating age from date of birth using hive使用 hive 从出生日期计算年龄
【发布时间】:2018-07-21 03:07:57
【问题描述】:

我想从给定的用户出生日期计算年龄。 我该怎么做 ? hive 中是否有任何特定的命令可用? 没有找到太多关于它的东西。我尝试使用 DATEDIFF 但没有得到正确的输出,它只给出 null

SELECT FLOOR(DATEDIFF(DAY, user_dob, unix_timestamp()) / 365.25)

【问题讨论】:

  • 你得到了什么结果?你看起来好像在大致正确的轨道上。另外user_dob的格式是什么?也许它没有正确转换为日期
  • 2/14/1993 12:00:00 AM 这是我出生日期的样子

标签: hadoop hive


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    当年减去出生年份
    加上如果他们的出生月份大于当前月份 -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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多