配置单元查询在生成缺失日期时面临的问题

iswrvxsc  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(332)

我有一个要求,即我需要返回到前1000行之前的列值,并为下一步获取前1000个日期,但表中该列的所有前1000个日期都不存在。但我需要从查询的输出中获取缺少的日期。
当我尝试运行下面的查询时,它不会显示当前日期的1000个以前的日期值。
示例:假设只有2个日期可用于日期列

date      
2019-01-16 
2019-01-19

我已经提出了一个查询,以获得回1000个日期,但它只给出了最近的日期,因为所有以前的回日期都丢失了

SELECT date FROM  table1 t
WHERE 
date >= date_sub(current_date,1000) and  dt<current_date ORDER BY date LIMIT 1

如果我在上面运行查询,它将显示 2019-01-16 ,因为前1000天的回溯日期不存在,所以它给出了最近的日期,即 2019-01-16 但我需要错过从 2016-04-23 (自当前日期起第1000个日期)至当前日期之前( 2019-01-18 )作为查询的输出。

8ljdwjyq

8ljdwjyq1#

您可以在子查询中为所需范围生成日期(请参见 date_range 子查询)和 left join 把它和你的table放在一起。如果在某些日期的表中没有记录,则该值将为空,并且日期将从 date_range 没有间隙的子查询。套 start_date 以及 end_date 所需日期范围的参数:

set hivevar:start_date=2016-04-23; --replace with your start_date
set hivevar:end_date=current_date; --replace with your end_date

set hive.exec.parallel=true;
set hive.auto.convert.join=true; --this enables map-join
set hive.mapjoin.smalltable.filesize=25000000; --size of table to fit in memory

with date_range as 
(--this query generates date range, check it's output
select date_add ('${hivevar:start_date}',s.i) as dt 
  from ( select posexplode(split(space(datediff('${hivevar:end_date}','${hivevar:start_date}')),' ')) as (i,x) ) s
) 

select d.dt as date,
       t.your_col --some value from your table on date
  from date_range d 
       left join table1 t on d.dt=t.date 
order by d.dt --order by dates if necessary

相关问题