Search code examples
conditional-statementscaseteradata

How to flag occurrences on multiple conditions in Teradata


I am struggling to achieve what I describe below, I hope someone could help.

I have a data set looks like below in Teradata.

enter image description here

And I need to set a flag to identify for calls (ignore anonymous) that called in the past, hung up, then called again within three days as shown below.

Here is the logic:

  • CALLBACK_FLAG shows 1 for all calls which called in the past, hung up (CALLER_HUNG_UP), and then call again within 3 days.
  • SAME DAY shows 1 for all calls that called again in same day.
  • POST_1 shows 1 for all calls that called again between 24 to 48 hours.
  • POST_2 shows 1 for all calls that called again between 48 to 72 hours.
  • POST_3 shows 1 for all calls that called again in more than 72 hours.

enter image description here

And here is the same data:

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

Thank you for your help everyone!


Solution

  • You can use rows preceding, something along these lines:

    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
    

    Then you can compare the previous call timestamp to the current one. For your same_day:

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