【问题标题】:calculating last 5 years from current date in hive从 hive 中的当前日期计算过去 5 年
【发布时间】:2018-11-04 09:39:09
【问题描述】:

我需要根据给定的时间范围计算一些计数 我需要考虑当前日期和过去 5 年之间的日期

select count(*) from table where (year(current_date) -year('2015-12-01')) < 5 ;

上面的查询将给出过去 5 年的计数,但它只考虑年份部分,但我需要考虑天数的确切计数,所以如果我写

 select count(*) from table where datediff(current_date,final_dt) <= 1825 ;

如果过去 5 年有闰年,则不考虑闰年

因此,hive 中是否有任何函数可以计算两个日期之间的精确差异,考虑闰年等情况?

【问题讨论】:

  • 所以基本上,您希望行介于当前日期和距当前日期 5 年后的日期之间,对吧?

标签: date hive


【解决方案1】:

使用add_months函数(假设日期应该回到2013-05-25,当前日期是2018-05-25)。

select count(*) 
from table
where final_dt >= add_months(current_date,-60) and final_dt <= current_date

【讨论】:

    【解决方案2】:

    我认为您正在尝试计算 count(*) 之间的所有记录 current_date 和从 current_date 过去 5 年的日期,在这种情况下,您可以执行以下操作:

    SELECT count(*) FROM table_1 WHERE date_column BETWEEN current_date AND to_date(CONCAT(YEAR(current_date) - 5, '-', MONTH(current_date), '-', DAY(current_date)));
    

    还有SELECT datediff( current_date() ,to_date(CONCAT(YEAR(current_date) - 5, '-', MONTH(current_date), '-', DAY(current_date))));

    给你1826(考虑到 2016 年是闰年)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 2021-06-26
      • 1970-01-01
      相关资源
      最近更新 更多