test0112是 1 中的第一张表
create table test0112 (
dateA string,
fromA string,
toA string,
rateA double
) row format delimited fields terminated by '\t';
test0112_all是 1 中的第二张表
create table test0112_all(
dateA string,
fromA string,
toA string,
rateA double
) row format delimited fields terminated by '\t';
注:建表导初始数的时候主义是低版本还是高版本
低版本是不能指定列名, 默认是全部列,直接 values(),指定列名会报错。
高版本可以指定列名,不会报错。
创建结果表并插入数据
create table test0112_result(
dateA string,
fromA string,
toA string,
rateA double
) row format delimited fields terminated by '\t';
insert into test0112_result
select
test0112_all.dateA as dateA,
test0112.fromA as fromA,
test0112.toA as toA,
test0112.rateA as rateA
from test0112_all
left join
test0112
on test0112_all.dateA=test0112.dateA
order by dateA
select
row_number()over() as rn,
case when rateA =='' then 0 else 1 end as flag,
dateA,
fromA,
toA,
rateA
from test0112_result
select
sum(flag)over(order by rn) as sumValue,
dateA,
fromA,
toA,
rateA
from
(
select
row_number()over() as rn,
case when toA is NULL then 0 else 1 end as flag,
dateA,
fromA,
toA,
rateA
from test0112_result
) t
select
row_number()over() as sumFlag,
dateA,
fromA,
toA,
rateA
from test0112
select
t1.dateA,
t2.fromA,
t2.toA,
t2.rateA
from
(
select
sum(flag)over(order by rn) as sumValue,
dateA,
fromA,
toA,
rateA
from
(
select
row_number()over() as rn,
case when toA is NULL then 0 else 1 end as flag,
dateA,
fromA,
toA,
rateA
from test0112_result
)t
)t1
inner join
(
select
row_number()over() as sumFlag,
dateA,
fromA,
toA,
rateA
from test0112_result
where rateA is not null
) as t2
on t1.sumValue=t2.sumFlag
order by t1.dateA