[英]Hive query to return last date of each month for 3 years
請提供蜂巢查詢,以'yyyy-mm-dd'
格式返回3年中每個月的最后日期。
在此示例中,將開始日期和結束日期替換為您的日期。 工作原理: space
函數生成長度為= datediff()
函數返回的天數的空格字符串,按空格分割創建一個數組,posexplode展開一個數組,返回該元素在數組中的位置,這對應於天。 然后date_add('$ {hivevar:start_date}',si)返回每天的日期, 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.