重组 Impala/Hive 中使用子查询在 table 中创建新列的查询
Restructure a query in Impala/Hive that is using subquery to create new column in table
我正在将 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列的下一个工作日。
表 1 是外 table 并且包含
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
.
.
我认为你可以这样做:
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);
您可以按如下方式重新编写查询
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
我正在将 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列的下一个工作日。
表 1 是外 table 并且包含
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
.
.
我认为你可以这样做:
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);
您可以按如下方式重新编写查询
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