如何找到在10秒内发生的所有交易

时间:2020-06-26 21:24:10

标签: sql sql-server

我想找到所有在10秒内发生的交易,价格相差超过10%,并且结果集应包括这两个交易之间的价差百分比。

表格创建:

create table Trades 
(TRADE_ID varchar(max), TIMESTAMP time(0), SECURITY varchar(max), QUANTITY int, PRICE int
);

Insert into Trades values
('TRADE1','10:01:05','BP',100,20),
('TRADE2','10:01:06','BP',20,15),
('TRADE3','10:10:00','BP',-100,19),
('TRADE4','10:10:01','BP',-300,19),
('TRADE5','10:01:08','BP',150,30),
('TRADE6','10:01:09','BP',300,32);

预期输出:

First_Trade Second_Trade    PRICE_DIFF
TRADE1      TRADE2          25
TRADE1      TRADE5          50
TRADE1      TRADE6          60
TRADE2      TRADE5          100
TRADE2      TRADE6          113

1 个答案:

答案 0 :(得分:0)

如果我理解正确,则可以使用exists

select t1.price, t2.price, (t1.price - t2.price) * 100.00 / t1.price
from trades t1 join
     trades t2
     on t2.timestamp >= t1.timestamp and
        t2.timestamp < dateadd(second, 10, t1.timestamp) and
        t2.tradeid <> t1.tradeid and
        t2.price not between 0.9 * t1.price and 1.1 * t1.price
相关问题