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

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的个数,但是由于三次加入同一个大表流,效率很低。如何高效简化?

最佳答案

好吧,您可以使用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;

09-07 19:18