如何根据开始日期和结束日期创建循环

时间:2020-07-30 13:02:17

标签: date hive hiveql

我有下表:

enter image description here

,我要基于read_date每隔10天执行一次循环,从min(reading_date)开始直到max(reading_date)。 最后,我需要将相应的数据更新为新日期:

如下图所示: enter image description here

有人在蜂巢sql中如何做到这一点?

在C ++中是如此简单,但是由于某些原因,我无法使其在蜂巢中工作。 我真的需要我能得到的所有帮助。谢谢!

t1.reading_date t1.use_date t1.name t1.reading_pct
12/17/2019  12/11/2019  file1   75.00915527
12/22/2019  12/11/2019  file1   75.5859375
12/27/2019  12/11/2019  file1   76.90429688
1/1/2020    12/11/2019  file1   74.29199219
1/2/2020    12/11/2019  file1   64.93835449
1/7/2020    12/11/2019  file1   65.10620117
1/12/2020   12/11/2019  file1   66.90063477
1/17/2020   12/11/2019  file1   66.47033691
1/22/2020   12/11/2019  file1   66.35131836
1/27/2020   12/11/2019  file1   59.61303711
            
new(t1.reading_date)    t1.use_date t1.name t1.reading_pct
12/17/2019  12/11/2019  file1   75.00915527
12/27/2019  12/11/2019  file1   76.90429688
1/7/2020    12/11/2019  file1   65.10620117
1/17/2020   12/11/2019  file1   66.47033691
1/27/2020   12/11/2019  file1   59.61303711

1 个答案:

答案 0 :(得分:0)

类似这样的内容,请参见代码中的注释。无法测试,因为您已将数据发布为图片。自己调试:

With minmaxdt as (
select min(reading_date) as mindt, max(reading_date) as maxdt from your_table
),

date_range as 
(--this query generates date range
select date_add (mindt,s.i+10) as dt 
  from minmaxdt
       lateral view posexplode(split(space(int(datediff(maxdt,mindt) div 10)),' ')) s as i,x 
) 

--Join with date range 
select reading_date, use_date, name, reading_pct
  from your_table t1
       inner join date_range dr on t1.reading_date=dr.dt