如何标记Teradata中多个条件的出现次数

时间:2016-09-30 14:48:38

标签: case conditional-statements teradata flags last-occurrence

我正在努力实现我在下面描述的内容,我希望有人可以提供帮助。

我在Teradata中有一个如下所示的数据集。

enter image description here

我需要设置一个标志来识别过去呼叫,挂断,然后在三天内再次呼叫的呼叫(忽略匿名),如下所示。

这是逻辑:

  • CALLBACK_FLAG为过去调用的所有呼叫显示1,挂断 (CALLER_HUNG_UP),然后在3天内再次致电。
  • 同一天,同一天再次通话的所有来电显示1。
  • POST_1对于在24到48之间再次呼叫的所有呼叫显示1 小时。
  • 对于在48到72之间再次呼叫的所有呼叫,POST_2显示1 小时。
  • 对于超过72小时内再次呼叫的所有呼叫,POST_3显示1。

enter image description here

以下是相同的数据:

SESS_KEY    PHONE_NUMBER    CALL_DATETIME   PRIOR_OUTCOME   SELF_SERV
CEC8FC08-FB74-4C22-49A2-38515697C540    3109911531  7/13/2016 15:01:31  TRANSFER    N
863125E8-B1CD-42DA-C3A0-6123D5553FF1    3123716274  7/8/2016 17:48:9    CALLER_HUNG_UP  N
ED090710-1A7A-4903-E7B8-2C8095F9BE9F    3123716274  7/8/2016 17:50:26   TRANSFER    N
00917937-D5C2-4170-CD92-C888F4EB9138    3126221129  7/14/2016 11:37:39  TRANSFER    N
BB052CFB-4221-49D3-38AD-6D9DA977AE4B    3133733000  7/20/2016 9:51:36   CALLER_HUNG_UP  N
A70F0C35-10A1-415C-1FBE-656AF451F674    3133733000  7/20/2016 9:57:32   TRANSFER    ?
DE0D195B-664B-4EF6-63A6-98C2D45B96D0    3133733000  7/26/2016 9:01:26   TRANSFER    N
07979610-0F60-4581-DD9E-38174E03E6AD    5152481040  7/19/2016 18:13:51  CALLER_HUNG_UP  N
8837EDAF-FBFB-4115-80B3-B4A035863CDB    5152481040  7/22/2016 13:29:24  TRANSFER    N
4FFBC4DE-317C-4472-0186-1E348CD837D7    5152706814  7/6/2016 12:47:2    TRANSFER    Y
EB2572A7-BE96-48F1-6B86-B14FBFBAD10D    7702467089  7/5/2016 15:07:4    CALLER_HUNG_UP  N
A7B3E35F-0A2A-453B-7A9A-716450D801AA    7702467089  7/5/2016 15:41:52   TRANSFER    ?
E0E129F5-EAD2-40CA-0B97-6D9DBB1320D8    7702467089  7/6/2016 21:38:34   CALLER_HUNG_UP  N
4635F54F-A5F3-4930-90A6-9DDC8ABA9AD4    7702467089  7/6/2016 23:54:6    CALLER_HUNG_UP  ?
3C72BF2A-6C2D-4721-F9A2-EDB4F51E39F0    7702467089  7/10/2016 20:53:39  CALLER_HUNG_UP  N
EAD8F235-A9CC-4958-9CB0-49D953140C08    7702467089  7/13/2016 17:52:3   CALLER_HUNG_UP  N
C868D5AF-217D-424B-8DB5-9F3F3A0B2CD3    7702467089  7/14/2016 17:55:39  CALLER_HUNG_UP  N
433CCDDF-0355-4AE4-5889-0FFC0680404F    7702467089  7/16/2016 19:55:8   TRANSFER    ?
7FD6E57A-A5BA-445E-DF99-17A9716B7BE2    7702467089  7/17/2016 19:52:2   CALLER_HUNG_UP  N
89FBB98F-DF55-4FFD-E0A8-6680C103A520    7702467089  7/20/2016 21:09:42  CALLER_HUNG_UP  ?
3508DDD9-A7A7-4473-ECA8-88850AAF4934    anonymous   7/13/2016 9:05:19   CALLER_HUNG_UP  N
990A15C4-B2F8-4335-E392-0B2FA1640F58    anonymous   7/13/2016 9:54:36   CALLER_HUNG_UP  N
571428CB-32B2-4358-4EA2-F1B144B57890    anonymous   7/15/2016 3:19:56   TRANSFER    Y
FF3EC14E-F7F2-4DE3-3697-0912E27F2AC0    anonymous   7/15/2016 8:28:39   CALLER_HUNG_UP  N
7856FA05-F1EE-4173-C48F-9996AB5317C7    anonymous   7/17/2016 10:13:15  CALLER_HUNG_UP  Y
829E7469-AD84-45AC-9E9C-9CA6C8BD336F    anonymous   7/18/2016 10:45:53  TRANSFER    N

感谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用前面的行,沿着这些行:

select
phone_number,
call_datetime,
max(call_datetime) 
  over (partition by phone_number order by call_datetime rows between 1 preceding and 1 preceding) as prev_call

然后,您可以将之前的呼叫时间戳与当前呼叫时间戳进行比较。对于你的同一天:

case when cast(call_timestamp as date) = cast(prev_call as date) then 1 end as SAME_DAY