2017-07-17 40 views
0

我有這個表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

我想你只需要包含在子查詢正確的列。這裏是我的意思:

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); 
+0

現在它的工作!謝謝。那麼,我是否正確理解這個查詢會查找所有時間段的空數據?這些時間可能是1天,2天,3天等。對吧? – Dinosaurius

+0

@Dinosaurius。 。 。除了開始和結束的任何事情。不過,我應該強調,這基本上是你的初衷,只是修正了語法。 –