3秒内剔除同类型交易
Excluding same type of transactions within 3 seconds
例如table如下
类型
时间戳
结果
1
2021-06-2514:21:00
一个
1
2021-06-2514:21:03
B
1
2021-06-2514:21:06
C
1
2021-06-2514:23:00
D
2
2021-06-2514:21:02
C
2
2021-06-2514:21:06
C
2
2021-06-2514:21:09
D
3
2021-06-2514:21:06
E
我想要的结果如下。
如果每隔 3 秒或更短时间有一系列相同类型的交易,我将只打印最后一个。
类型
时间戳
结果
1
2021-06-2514:21:06
C
1
2021-06-2514:23:00
D
2
2021-06-2514:21:02
C
2
2021-06-2514:21:09
D
3
2021-06-2514:21:06
E
基本上,您希望相同类型的下一个行提前三秒以上:
select t.*
from (select t.*,
lead(timestamp) over (partition by type order by timestamp) as next_timestamp
from t
) t
where next_timestamp is null or
next_timestamp > timestamp + interval '3' second;
使用默认的 LEAD 和 Teradata 的专有 QUALIFY 关键字简化 Gordon 的查询:
select *
from vt
qualify
lead(timestamp, 1, date '9999-12-31')
over (partition by type
order by timestamp) > timestamp_ + interval '3' second;
例如table如下
类型 | 时间戳 | 结果 |
---|---|---|
1 | 2021-06-2514:21:00 | 一个 |
1 | 2021-06-2514:21:03 | B |
1 | 2021-06-2514:21:06 | C |
1 | 2021-06-2514:23:00 | D |
2 | 2021-06-2514:21:02 | C |
2 | 2021-06-2514:21:06 | C |
2 | 2021-06-2514:21:09 | D |
3 | 2021-06-2514:21:06 | E |
我想要的结果如下。 如果每隔 3 秒或更短时间有一系列相同类型的交易,我将只打印最后一个。
类型 | 时间戳 | 结果 |
---|---|---|
1 | 2021-06-2514:21:06 | C |
1 | 2021-06-2514:23:00 | D |
2 | 2021-06-2514:21:02 | C |
2 | 2021-06-2514:21:09 | D |
3 | 2021-06-2514:21:06 | E |
基本上,您希望相同类型的下一个行提前三秒以上:
select t.*
from (select t.*,
lead(timestamp) over (partition by type order by timestamp) as next_timestamp
from t
) t
where next_timestamp is null or
next_timestamp > timestamp + interval '3' second;
使用默认的 LEAD 和 Teradata 的专有 QUALIFY 关键字简化 Gordon 的查询:
select *
from vt
qualify
lead(timestamp, 1, date '9999-12-31')
over (partition by type
order by timestamp) > timestamp_ + interval '3' second;