Hi @brenda grossnickle ,
Welcome to Microsoft Q&A!
Please also refer below:
create table trx
(
txn_dt datetime,
switch_id int,
txn_code varchar(2)
)
insert into trx values
('2021-02-18 13:00:00.770', 111, 'A'),
('2021-02-18 13:04:00.770', 111, 'Z'),
('2021-02-18 14:00:00.770', 111, 'A'),
('2021-02-18 14:15:00.770', 111, 'Z'),
('2021-02-18 15:00:00.770', 111, 'B'),
('2021-02-18 16:04:00.770', 111, 'Z'),
('2021-02-18 13:00:00.770', 222, 'A'),
('2021-02-18 13:01:00.770', 222, 'A'),
('2021-02-18 13:04:00.770', 222, 'Z')
select 'switch '+ cast(switch_id as char(4))+' - '+cast(count(txn_code) as char(1)) result
from (SELECT *, next_dt = lead(txn_dt) OVER (PARTITION BY switch_id ORDER BY txn_dt),
next_code = lead(txn_code) OVER (PARTITION BY switch_id ORDER BY txn_dt)
FROM trx)a
where txn_code='A' and next_code='Z'
and next_dt<=DATEADD(MINUTE,10,txn_dt)
group by switch_id
Output:
result
switch 111 - 1
switch 222 - 1
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.