【问题标题】:Hive query to return last date of each month for 3 yearsHive 查询以返回 3 年内每个月的最后一个日期
【发布时间】:2018-07-07 19:40:50
【问题描述】:

请提供 hive 查询以返回 'yyyy-mm-dd' 格式的 3 年内每个月的最后日期。

【问题讨论】:

标签: date hive calendar hiveql


【解决方案1】:

用您的替换本示例中的开始日期和结束日期。它是如何工作的:space函数生成长度=由datediff()函数返回的天数的空格字符串,用空格分割创建一个数组,posexplode分解一个数组,返回数组中元素的位置,它对应于天数。然后 date_add('${hivevar:start_date}',s.i) 返回每一天的日期,lest_day() 函数(自 1.1 版本以来存在于 Hive 中)将每个日期转换为最后一天(此处需要 distinct)。运行这个例子:

set hivevar:start_date=2015-07-01;
set hivevar:end_date=current_date;

select distinct last_day(date_add ('${hivevar:start_date}',s.i)) as last_date 
  from ( select posexplode(split(space(datediff(${hivevar:end_date},'${hivevar:start_date}')),' ')) as (i,x)
        ) s
order by last_date
;

输出:

OK
2015-07-31
2015-08-31
2015-09-30
2015-10-31
2015-11-30
2015-12-31
2016-01-31
2016-02-29
2016-03-31
2016-04-30
2016-05-31
2016-06-30
2016-07-31
2016-08-31
2016-09-30
2016-10-31
2016-11-30
2016-12-31
2017-01-31
2017-02-28
2017-03-31
2017-04-30
2017-05-31
2017-06-30
2017-07-31
2017-08-31
2017-09-30
2017-10-31
2017-11-30
2017-12-31
2018-01-31
2018-02-28
2018-03-31
2018-04-30
2018-05-31
2018-06-30
2018-07-31
Time taken: 71.581 seconds, Fetched: 37 row(s)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多