How to select periods of time with empty data?

旧巷老猫 提交于 2019-12-11 07:37:10

问题


I want to find out all periods with empty data, given the following table my_table:

id         day       
29         2017-06-05
26         2017-06-05
30         2017-06-06
30         2017-06-06
21         2017-06-06
21         2017-07-01
29         2017-07-01
30         2017-07-20

The answer would be:

Empty_start    Empty_end
2017-06-07     2017-06-30
2017-07-02     2017-07-19

It's important that the number of months is considered. For example, in the first row the answer 2017-06-31 would be incorrect.

How can I write this query in Hive?


回答1:


You can use lag() or lead():

select date_add(day, 1) as empty_start, date_add(next_day, -1) as empty_end
from (select day,
             lead(day) over (order by day) as next_day
      from t
      group by day
     ) t
where next_day <> date_add(day, 1);


来源:https://stackoverflow.com/questions/44978201/how-to-select-periods-of-time-with-empty-data

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