如何用hive简化计算效率?

o4tp2gmn  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(282)

代码正在配置单元上运行:

select day,count(mdn)*5 as number from
(select distinct a.mdn,a.day from 
flow a
left outer join
flow b
on a.day=date_add(b.day,-1) and a.mdn=b.mdn
left outer join
flow c
on a.day=date_add(c.day,-2) and a.mdn=c.mdn
left outer join
flow d
on a.day=date_add(d.day,-3) and a.mdn=d.mdn
where b.mdn is null  and c.mdn is null  and d.mdn is null)t 
group by day

代码的逻辑是选择一个今天的mdn,这个mdn在未来三天内没有出现,然后计算mdn的个数,但是这个代码的效率很低,因为使用同一个大表流进行了三次连接。如何高效简化?

o0lyfsai

o0lyfsai1#

好吧,你可以用 lead() 比较日期和时间:

select f.*
from (select f.*,
             lead(f.day) over (partition by f.mdn order by f.day) as next_day
      from flow f
     ) f
where next_day > date_add(day, 3) or next_date is null;

相关问题