2015-09-22 82 views
1

我有attendancedata有日期,EMPNO timein /超時列沒有周末日期的表格(星期五和星期六是週末)在Vertica的計數不包括Vertica的週末

請注意: 1,表是DateTimeAttendance 2,對於缺席的員工,Time-In爲NULL。

所以我的條件在這裏是

TimeIn = NULL

我嘗試了數據Vertica的以下分析功能,並得到了outpout如下

select name, date, CONDITIONAL_TRUE_EVENT(date - lag(date)>1) over (partition by name order by date) as ConsecutiveDatesCounter 
from DateTimeAttendance 
where timein is null group by name,date ; 

樣本輸出中:

name     date ConsecutiveDatesCounter 

Aaron Gadsen  19/3/2014 0 

Aaron Gadsen  23/3/2014 1 

Aaron Gadsen  24/3/2014 1 

Aaron Gadsen  25/3/2014 1 

Aaron Gadsen  26/3/2014 1 

Aaron Gadsen  27/3/2014 1 

Aaron Gadsen  30/3/2014 2 

Aaron Gadsen  31/3/2014 2 

這裏28/3/2014和29/3/2014是週末,所以我想ConsecutiveDatesCounter 1不應該改變爲2,它應該繼續作爲1

我想要得到的輸出如下

name     date  ConsecutiveDatesCounter 

Aaron Gadsen  19/3/2014 0 

Aaron Gadsen  23/3/2014 1 

Aaron Gadsen  24/3/2014 1 

Aaron Gadsen  25/3/2014 1 

Aaron Gadsen  26/3/2014 1 

Aaron Gadsen  27/3/2014 1 

Aaron Gadsen  30/3/2014 1 

Aaron Gadsen  31/3/2014 1 

上述結果接着查詢將如下

select name, count(1) num_days, min(date) startdate, max(date) enddate 
from (select name, date, CONDITIONAL_TRUE_EVENT(date - lag(date)>1) over (partition by name order by date) as ConsecutiveDatesCounter 
     from DateTimeAttendance where timein is null group by name,date) as consecutive 
group by name, consecutiveDatesCounter order by startdate; 

最終輸出應是像這樣:

name     num_days startdate  enddate  

Aaron Gadsen  1    19/3/2014 19/3/2014 

Aaron Gadsen  7    23/3/2014 31/3/2014 

請幫我解決週末的這個問題,在這種情況下..

由於提前

回答

0

它看起來像你真的只需要考慮到週五星期一。我假設根據你的數據集,你根本沒有周末行(如果你這樣做,你會想過濾它們以使其工作)。在週一/週五的情況下將活動假設爲假(同時確保只有3天的差距)。這不太好,也許別人有更好的辦法。

CONDITIONAL_TRUE_EVENT((date - lag(date) > 1) AND NOT (date - lag(date) = 3 AND DAYOFWEEK(date) = 2 AND DAYOFWEEK(lag(date)) = 6) 
相關問題