Count existence of associated transactions within 10 minutes

brenda grossnickle 206 Reputation points
2021-02-18T18:44:41.937+00:00

I have some typical transaction data: transaction datetime, switch_id, transaction code. looking to count the number of Z transaction codes that have a single A within the previous 10 minutes and then group by switch_id. below the 1st and 4th group would qualify. so count for switch_id 111 = 1 and switch_id 222 = 1. I realize that there could be some crazy nested data (A, A, Z, Z) examples but this is a high level data review and not worried about the 1% of data exceptions. Thanks for any help.

txn_dt, swith_id, txn_code

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

expected results

switch 111 - 1
switch 222 - 1

Developer technologies | Transact-SQL
{count} votes

Accepted answer
  1. Erland Sommarskog 121.8K Reputation points MVP Volunteer Moderator
    2021-02-18T22:15:40.673+00:00

    Try this:

    ; WITH CTE AS (
       SELECT txn_dt, prev_dt = LAG(txt_dt) OVER (PARTITION BY switch_id ORDER BY txn_dt),
              switch_id, txn_code, prev_code = LAG(txt_code) OVER (PARTITION BY switch_id ORDER BY txn_dt)
       FROM   tbl
    )
    SELECT switch_id
    FROM   CTE
    WHERE  txn_code = 'Z' 
      AND  prev_code = 'A'
      AND  datediff(second, prev_dt, txn_dt) <= 600
    

2 additional answers

Sort by: Most helpful
  1. MelissaMa-MSFT 24,221 Reputation points
    2021-02-19T02:32:20.2+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

  2. brenda grossnickle 206 Reputation points
    2021-02-18T22:06:29.893+00:00

    2021-02-18 13:00:00.770, 222, A
    2021-02-18 13:01:00.770, 222, A --- this is within 10 minutes of 13:04
    2021-02-18 13:04:00.770, 222, Z


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.