Select 上一个动作之后的第一个动作实例

Select the first instance of an action after the previous action

所以,这个主题超级混乱,因为我不知道如何用语言来解释我想要的东西。我正在尝试通过操作从单个 table 中提取数据。下面是我的数据的一小部分示例。

我需要每个名称的前一个操作之后每个 OptIn 和 OptOut 的第一个实例。我也在下面发布了我想要的结果。

我在 SQL 2014 年尝试这样做。我对此一无所知。我想也许我可以做一些滞后的东西,但我不完全理解如何使用它们,所以我自己很困惑。

示例数据:

Inst,Acct,First,MI,Last,Activty,ActivtyDate

001,00000000001111111,EDWARD,,BICKLE,OptIn,2019-05-25 12:02:00.000
001,00000000001111111,EDWARD,,BICKLE,OptIn,2019-05-25 12:37:00.000
001,00000000001111111,KIM,,BICKLE,OptIn,2019-06-17 08:43:00.000
001,00000000001111111,EDWARD,,BICKLE,OptOut,2019-06-20 11:57:00.000
001,00000000001111111,EDWARD,,BICKLE,OptOut,2019-06-20 23:10:00.000
001,00000000001111111,EDWARD,,BICKLE,OptOut,2019-06-21 08:46:00.000
001,00000000001111111,EDWARD,,BICKLE,OptIn,2019-06-27 10:34:00.000
001,00000000001111111,EDWARD,,BICKLE,OptOut,2019-06-27 10:35:00.000
001,00000000001111111,EDWARD,,BICKLE,OptOut,2019-06-27 23:04:00.000
001,00000000001111111,KIM,,BICKLE,OptIn,2019-07-02 11:35:42.000
001,00000000001111111,EDWARD,,BICKLE,OptOut,2019-07-05 15:04:00.000
001,00000000001111111,EDWARD,,BICKLE,OptIn,2019-07-06 07:57:00.000
001,00000000001111111,EDWARD,,BICKLE,OptIn,2019-07-06 08:16:00.000
001,00000000001111111,EDWARD,,BICKLE,OptIn,2019-07-09 08:17:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-03-03 20:51:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-03-03 20:51:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-03-08 19:10:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-03-16 06:34:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-03-18 16:21:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-03-21 16:38:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-03-22 05:26:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-04-01 15:39:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-04-02 04:06:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-04-06 11:53:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-04-09 12:51:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-04-20 15:21:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-04-20 15:21:00.000

期望的结果:

Inst,Acct,First,MI,Last,Activty,ActivtyDate

001,00000000001111111,EDWARD,,BICKLE,OptIn,2019-05-25 12:02:00.000
001,00000000001111111,EDWARD,,BICKLE,OptOut,2019-06-20 11:57:00.000
001,00000000001111111,EDWARD,,BICKLE,OptIn,2019-06-27 10:34:00.000
001,00000000001111111,EDWARD,,BICKLE,OptOut,2019-06-27 10:35:00.000
001,00000000001111111,KIM,,BICKLE,OptIn,2019-07-02 11:35:42.000
001,00000000001111111,EDWARD,,BICKLE,OptOut,2019-07-05 15:04:00.000
001,00000000001111111,EDWARD,,BICKLE,OptIn,2019-07-06 07:57:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-03-03 20:51:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-03-08 19:10:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-03-18 16:21:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-03-21 16:38:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-03-22 05:26:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-04-01 15:39:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-04-02 04:06:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-04-06 11:53:00.000
000,00000000002222222,DAWN,M,ADAMS,OptOut,2019-04-09 12:51:00.000
000,00000000002222222,DAWN,M,ADAMS,OptIn,2019-04-20 15:21:00.000

您可以使用 lag() 来识别 activity 不同于 "previous" 记录的记录:

select t.*
from (
    select 
        t.*,
        lag(activity) over(partition by inst, acct order by activityDate) lag_activity
    from mytable t
) t
where lag_activity is null or activity <> lag_activity
order by inst, acct, activityDate   

Demo on DB Fiddle:

Inst |    Acct | First  | MI   | Last   | Activty | ActivityDate            | lag_activity
---: | ------: | :----- | :--- | :----- | :------ | :---------------------- | :-----------
   1 | 1111111 | EDWARD | null | BICKLE | OptIn   | 2019-05-25 12:02:00.000 | null
   1 | 1111111 | EDWARD | null | BICKLE | OptOut  | 2019-06-20 11:57:00.000 | OptIn
   1 | 1111111 | EDWARD | null | BICKLE | OptIn   | 2019-06-27 10:34:00.000 | OptOut
   1 | 1111111 | EDWARD | null | BICKLE | OptOut  | 2019-06-27 10:35:00.000 | OptIn
   1 | 1111111 | KIM    | null | BICKLE | OptIn   | 2019-07-02 11:35:42.000 | OptOut
   1 | 1111111 | EDWARD | null | BICKLE | OptOut  | 2019-07-05 15:04:00.000 | OptIn
   1 | 1111111 | EDWARD | null | BICKLE | OptIn   | 2019-07-06 07:57:00.000 | OptOut
   0 | 2222222 | DAWN   | M    | ADAMS  | OptOut  | 2019-03-03 20:51:00.000 | null
   0 | 2222222 | DAWN   | M    | ADAMS  | OptIn   | 2019-03-08 19:10:00.000 | OptOut
   0 | 2222222 | DAWN   | M    | ADAMS  | OptOut  | 2019-03-18 16:21:00.000 | OptIn
0 | 2222222 | DAWN | M | ADAMS | OptIn | 2019-03-21 16:38:00.000 | OptOut
0 | 2222222 | DAWN | M | ADAMS | OptOut | 2019-03-22 05:26:00.000 | OptIn 0 | 2222222 | DAWN | M | ADAMS | OptIn | 2019-04-01 15:39:00.000 | OptOut 0 | 2222222 | DAWN | M | ADAMS | OptOut | 2019-04-02 04:06:00.000 | OptIn
0 | 2222222 | DAWN | M | ADAMS | OptIn | 2019-04-06 11:53:00.000 | OptOut 0 | 2222222 | DAWN | M | ADAMS | OptOut | 2019-04-09 12:51:00.000 | OptIn
0 | 2222222 | DAWN | M | ADAMS | OptIn | 2019-04-20 15:21:00.000 | OptOut