2

I'd like to create a table with all the days of a year using HQL. I already tried this approach :

generate days from date range

but the hql is a bit different from sql What's the best approach?
Using the PL/HQL or using bash script and importing ?

Expected result:

start_date = 2017-02-14;

| date     |
|2017-02-14|
|2017-02-13|
|2017-02-12|
|2017-02-11|
|2017-02-10|
|2017-02-09|
|2017-02-08|
|2017-02-07|
....

Thanks

Community
  • 1
  • 1
Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80

2 Answers2

2
set start_date=2017-02-14;

select  date_sub('${hiveconf:start_date}',i)
from    (select 1 as n) dummy lateral view posexplode(split(space(364),' ')) p as i,x
;

2017-02-14
2017-02-13
2017-02-12
2017-02-11
2017-02-10
.
.
.
2016-02-20
2016-02-19
2016-02-18
2016-02-17
2016-02-16
David דודו Markovitz
  • 42,900
  • 6
  • 64
  • 88
1

Using bash and an older start date for testing purposes:

start_date="2014-02-14"
days=$((($(date -u +%s) - $(date -ud $start_date +%s))/60/60/24))
(( day_end = 366 + days ))

while (( days < day_end  ));do
  date "+%Y-%m-%d" -d "$days days ago"
  (( days++ ))
done

Result

2014-02-14
2014-02-13
2014-02-12
2014-02-11
2014-02-10
2014-02-09
2014-02-08
2014-02-07
2014-02-06
2014-02-05
2014-02-04
...
...
...
2013-02-21
2013-02-20
2013-02-19
2013-02-18
2013-02-17
2013-02-16
2013-02-15
2013-02-14
Juan Diego Godoy Robles
  • 14,447
  • 2
  • 38
  • 52