Skip to content
Advertisement

Excluding same type of transactions within 3 seconds

For example, a table is like below

Type Time Stamp Result
1 2021-06-25 14:21:00 A
1 2021-06-25 14:21:03 B
1 2021-06-25 14:21:06 C
1 2021-06-25 14:23:00 D
2 2021-06-25 14:21:02 C
2 2021-06-25 14:21:06 C
2 2021-06-25 14:21:09 D
3 2021-06-25 14:21:06 E

And the result I want is as below. If there are a series of transactions with the same type every 3 seconds or less, I will print the last one only.

Type Time Stamp Result
1 2021-06-25 14:21:06 C
1 2021-06-25 14:23:00 D
2 2021-06-25 14:21:02 C
2 2021-06-25 14:21:09 D
3 2021-06-25 14:21:06 E

Advertisement

Answer

Simplifying Gordon’s query using a default for LEAD and Teradata’s proprietary QUALIFY keyword:

select *
from vt
qualify 
   lead(timestamp, 1, date '9999-12-31')
   over (partition by type
         order by timestamp) > timestamp_ + interval '3' second;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement