在Impala / Hive中重构使用子查询在表中创建新列的查询

问题描述 投票:0回答:2

我正在将SQL查询转换为Impala。 SQL查询在select中使用子查询来创建新列,如下所示-

select *, (select min(day)
           from date_series 
           where day > t.work_day) as next_work_day
from table1 t

但是,Impala在select中不支持子查询来创建新列,此查询失败。我能否获得帮助以Impala可以执行的方式重写此查询。

查询目的:为work_day列找到下一个工作日。

Table1是外部表,包含

table1 contains 4 columns including the work day column

date_series contains all working dates stating from 2019-06-18 to current_day + 5喜欢

   2019-06-20
   2019-06-21
   2019-06-24
   .
   .

sql subquery impala
2个回答
0
投票

我认为您可以这样做:

select t.*, ds.next_day
from table1 t left join
     (select ds.*, lead(day) over (order by day) as next_day
      from date_series ds
     ) ds
     on t.current_work_day >= ds.day and
        (t.current_work_day < ds.next_day or ds.next_day is null);

0
投票

您可以按照以下方式重新编写查询

select 
    t.*,
    work_day 
from table1 t 
join (
    select 
        min(day) as work_day 
    from date_series
) ds
on t.current_work_day = ds.work_day

where ds.work_day > t.current_work_day
© www.soinside.com 2019 - 2024. All rights reserved.