How to use a window function to determine when to perform different tasks in Hive or Postgres?

百般思念 提交于 2019-12-08 08:38:31

The solution in Postgres is fairly easy, because it supports generate_series(). First, explode the data for one row per day for each row in your table:

select d.*, gs.dy
from data d, lateral
     generate_series(start_day, end_day) gs(dy);

Then, aggregate to get the task for each day:

select d.person, d.dy, max(d.task_key) as task_key
from (select d.*, gs.dy
      from data d, lateral
           generate_series(start_day, end_day) gs(dy)
     ) d
group by d.person, d.dy;

You can then re-aggregate, but this is tricky because you might have "split" the original rows (see my comment). This answers your question about which task to perform on which day.

You can do all of this without a lateral join or generate_series() by using a number/tally table.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!