从DOB计算年龄

时间:2020-04-28 02:50:34

标签: sql hadoop hive hiveql

我正在使用HiveQL,我只需要使用“出生日期”列来计算年龄,但是问题是GetDate不起作用,而Current_Date()却起作用。我正在尝试的示例是

datediff(yy,Dateofbirthcol,current_date()) As Age

DOB列看起来像1988-12-14

3 个答案:

答案 0 :(得分:2)

尝试以下选项之一。

  1. floor(datediff(to_date(from_unixtime(unix_timestamp())), Dateofbirthcol) / 365.25)

  2. datediff(now(), Dateofbirthcol) / 365.25

答案 1 :(得分:0)

请勿使用unix_timestamp(),因为它是non-deterministic。使用current_date:


$modified_product = shopify_call($token, $shop, "/admin/products/" . $product_id . ".json", $modify_data, 'PUT');

// Storage response
$status = $modified_product['headers']['status'];

//echo $status;      (This prints = HTTP/1.1 200 OK)

//print_r($modified_product['headers']);

if ($status == "HTTP/1.1 200 OK" ) {  // Somehow this compare does not work. I always get problem
    echo "Done";
}else
{
   echo "Problem";  
}

答案 2 :(得分:0)

您可以通过多种方式实现这一目标,但是蜂巢中没有现成的解决方案。

1)使用datediff():

此函数返回两天之间的天数,因此必须将函数输出除以365.25以获得输出。 (除以365.25来考虑leap年)

select cast(datediff(current_date, DOB) / 365.25 as int) as age;

2)使用months_between():

此函数返回两天之间的月份差,因此必须将输出除以12以得到预期的输出。

select cast(months_between(current_date, DOB) / 12 as int) as age;

3)使用UDF:

如果您要查找年,月和日格式的年龄,则使用蜂巢函数是一种复杂的方法。编写自定义udf函数将很有帮助,该函数将dob作为输入并以YYMM个月dd天的格式返回年龄。

要引用日期函数,请参见hive documentation

相关问题