【问题标题】:Calculating consecutive dates teradata计算连续日期 teradata
【发布时间】:2017-10-02 00:33:43
【问题描述】:

我不是 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

【问题讨论】:

  • 我删除了 MySQL 标签,因为它不属于那里。
  • 我不懂你的数学。您能清楚地解释一下您是如何将 6 作为 John 的值的吗?
  • 感谢 Tim 这里是 John 的群组
  • 第 1 组约翰 1/03/2016 约翰 2/03/2016 约翰 3/03/2016 第 2 组约翰 4/03/2016 约翰 5/03/2016 第 3 组约翰 12/03/2016约翰 13/03/2016 约翰 14/03/2016 第 4 组 约翰 15/03/2016 约翰 16/03/2016 约翰 17/03/2016 第 5 组 约翰 18/03/2016 约翰 19/03/2016 第 6 组 约翰 29 /03/2016 约翰 31/03/2016
  • 蒂姆你将能够在我附上的图片中看到我是如何计算日期组的

标签: teradata


【解决方案1】:

有趣的问题。

这是一个使用有序分析函数和嵌套派生表的解决方案。 每人的最终分数以 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    ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2019-05-11
    • 2014-04-26
    • 2017-05-09
    • 1970-01-01
    相关资源
    最近更新 更多