本文基于hive1.1.0-cdh5.12.1
要求:通过sql生成一段连续的日期,入2021-01-01至2021-01-31
参考结果:
dt |
2021-01-01 |
2021-01-02 |
2021-01-03 |
2021-01-04 |
参考实现:
--由于hive不能递归,所有使用posexplode生成连续序号
with t as
(
select date_add( to_date('2021-01-01'),i) as dt -- 基础日期
from (select 30 as days ) t --需要加多少天
LATERAL VIEW
posexplode(split(repeat(',',days),',')) pe as i, x
)
select * from t;
+-------------+--+
| t.dt |
+-------------+--+
| 2021-01-01 |
| 2021-01-02 |
| 2021-01-03 |
| 2021-01-04 |
| 2021-01-05 |
| 2021-01-06 |
| 2021-01-07 |
| 2021-01-08 |
| 2021-01-09 |
| 2021-01-10 |
| 2021-01-11 |
| 2021-01-12 |
| 2021-01-13 |
| 2021-01-14 |
| 2021-01-15 |
| 2021-01-16 |
| 2021-01-17 |
| 2021-01-18 |
| 2021-01-19 |
| 2021-01-20 |
| 2021-01-21 |
| 2021-01-22 |
| 2021-01-23 |
| 2021-01-24 |
| 2021-01-25 |
| 2021-01-26 |
| 2021-01-27 |
| 2021-01-28 |
| 2021-01-29 |
| 2021-01-30 |
| 2021-01-31 |
+-------------+--+