2017-08-09 80 views
0

我不是在Teradata數據或SQL的專家,所以需要計算天數一定的幫助一個人出席了客戶計算連續日期的Teradata

如果一個銷售人員出席了顧客從1 - 3天連續這將計爲1,如果該人出席4天那麼它將被算作2 我將添加數據的例子,結果,我想

DATA:

Sales Person Date 
John 1/03/2016 
John 2/03/2016 
John 3/03/2016 
John 4/03/2016 
John 5/03/2016 
David 6/03/2016 
David 7/03/2016 
David 8/03/2016 
David 9/03/2016 
David 10/03/2016 
David 11/03/2016 
John 12/03/2016 
John 13/03/2016 
John 14/03/2016 
John 15/03/2016 
John 16/03/2016 
John 17/03/2016 
John 18/03/2016 
John 19/03/2016 
David 20/03/2016 
Sue 21/03/2016 
Sue 22/03/2016 
Sue 23/03/2016 
Lily 24/03/2016 
Lily 25/03/2016 
Lily 26/03/2016 
Sue 27/03/2016 
David 28/03/2016 
John 29/03/2016 
David 30/03/2016 
John 31/03/2016 

結果通緝:

Sales Person Groups 
John 6 
David 4 
Sue 2 
Lily 1 

Excel Format Picture

+1

我刪除了MySQL的標籤,因爲它不屬於那裏。 –

+0

我不明白你的數學。你能否清楚地解釋你作爲約翰的價值如何到達? –

+0

由於添 以下是約翰 – Harry

回答

0

有趣的問題。

這是一個使用有序分析函數和嵌套派生表的解決方案。 每個人的最終分數是在person_points。我使用分析函數sum()而不是分組,因爲我想顯示中間步驟。不應該統計與前一組重疊的3天期限的規則,這有點棘手。

create table t (person varchar(30), dt date); 
insert into t values('John','2016-03-01'); 
insert into t values('John','2016-03-02'); 
insert into t values('John','2016-03-03'); 
insert into t values('John','2016-03-04'); 
insert into t values('John','2016-03-05'); 
insert into t values('David','2016-03-06'); 
insert into t values('David','2016-03-07'); 
insert into t values('David','2016-03-08'); 
insert into t values('David','2016-03-09'); 
insert into t values('David','2016-03-10'); 
insert into t values('David','2016-03-11'); 
insert into t values('John','2016-03-12'); 
insert into t values('John','2016-03-13'); 
insert into t values('John','2016-03-14'); 
insert into t values('John','2016-03-15'); 
insert into t values('John','2016-03-16'); 
insert into t values('John','2016-03-17'); 
insert into t values('John','2016-03-18'); 
insert into t values('John','2016-03-19'); 
insert into t values('David','2016-03-20'); 
insert into t values('Sue','2016-03-21'); 
insert into t values('Sue','2016-03-22'); 
insert into t values('Sue','2016-03-23'); 
insert into t values('Lily','2016-03-24'); 
insert into t values('Lily','2016-03-25'); 
insert into t values('Lily','2016-03-26'); 
insert into t values('Sue','2016-03-27'); 
insert into t values('David','2016-03-28'); 
insert into t values('John','2016-03-29'); 
insert into t values('David','2016-03-30'); 
insert into t values('John','2016-03-31'); 

select t_points.* 
    ,sum(points) over(partition by person) person_points 
from 
(
    select person, consecutive_group, min(dt) first_dt, max(dt) last_dt 
     , last_dt - first_dt + 1 n_days 
     ,floor((n_days + 2)/3)*3 + first_dt - 1 end_of_3day_period 
     ,max(end_of_3day_period) over(partition by person order by consecutive_group rows between 1 preceding and 1 preceding) prev_end_3day_dt 
     ,case when prev_end_3day_dt >= first_dt then prev_end_3day_dt - first_dt + 1 else 0 end overlapped_days 
     ,n_days - overlapped_days n_days_no_overlap 
     , floor((n_days_no_overlap + 2)/3) points 
    from 
    (
     select person,dt 
      ,sum(begin_new_consecutive) over(partition by person order by dt rows unbounded preceding) consecutive_group 
     from 
     (
      select person, dt 
       ,max(dt) over(partition by person order by dt rows between 1 preceding and 1 preceding) prev_dt 
       ,case when dt = prev_dt+1 then 0 else 1 end begin_new_consecutive 
      from t 
     ) t_consecutive 
    ) t_consecutive_group 
    group by 1,2 
) t_points 
order by 1,2 ; 

Results