计算连续日期teradata

时间:2017-08-09 04:23:29

标签: teradata

我不是Teradata或SQL的专家,所以在计算一个人上客户的天数时需要一些帮助

如果销售人员连续1至3天就读客户,则将其计为1,如果此人已参加4天,则将其计为2 我将添加我想要的数据和结果示例

数据:

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 个答案:

答案 0 :(得分: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

相关问题