查找包含空数据的句点

时间:2017-07-17 18:56:03

标签: sql hive

我在Hive中有这个表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

我想得到一段空数据:

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

我尝试使用this solution

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

我使用to_date因为day是字符串。

最后,我收到以下错误消息:

SemanticException [Error 10004]: Line 1:269 Invalid table alias or column reference 'day': (possible column names are: _c0, next_day)

1 个答案:

答案 0 :(得分:1)

我认为您只需要在子查询中包含正确的列。这就是我的意思:

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