从累积和中计算减法
Posted
技术标签:
【中文标题】从累积和中计算减法【英文标题】:Calculate subtraction from cumulative sum 【发布时间】:2020-06-16 18:59:43 【问题描述】:我的数据如下所示:
PAIR TIMESTAMP SIDE PRICE AMOUNT PROCEEDS DEPTH_RANK CUM_SUM_AMOUNT
BTC-USD 1592328691 ask 9478.383 0.2 1895.6766 1 0.2
BTC-USD 1592328691 ask 9478.384 0.20517 1944.680045 2 0.40517
BTC-USD 1592328691 ask 9478.479 0.26 2464.40454 3 0.66517
BTC-USD 1592328691 ask 9479.784 0.4 3791.9136 4 1.06517
BTC-USD 1592328691 ask 9480.126 0.1335 1265.596821 5 1.19867
BTC-USD 1592328691 ask 9485.722 0.081 768.343482 6 1.27967
BTC-USD 1592328691 ask 9485.723 0.2 1897.1446 7 1.47967
BTC-USD 1592328691 ask 9485.833 0.28082357 2663.845487 8 1.76049357
BTC-USD 1592328691 ask 9485.84 0.00136758 12.97264507 9 1.76186115
BTC-USD 1592328691 ask 9486.874 4.80287064 45564.2286 10 6.56473179
BTC-USD 1592328691 ask 9486.875 5.58780566 53010.81382 11 12.15253745
BTC-USD 1592328691 ask 9488.702 0.3665 3477.609283 12 12.51903745
BTC-USD 1592328691 ask 9496.899 1.1406 10832.163 13 13.65963745
BTC-USD 1592328691 ask 9496.9 0.14785281 1404.143351 14 13.80749026
BTC-USD 1592328691 ask 9496.902 0.00535416 50.84793281 15 13.81284442
BTC-USD 1592328691 ask 9500.749 3.7257 35396.94055 16 17.53854442
BTC-USD 1592328691 ask 9500.75 1.65528473 15726.4464 17 19.19382915
BTC-USD 1592328691 ask 9508.518 0.039 370.832202 18 19.23282915
BTC-USD 1592328691 ask 9512.745 0.15830434 1505.908819 19 19.39113349
BTC-USD 1592328691 ask 9512.746 1.74885185 16636.38344 20 21.13998534
假设我有一些任意的INT
-type ORDER_SIZE
。我需要一个额外的列FILLED
,它显示ORDER_SIZE
和AMOUNT
之间的差异,使得FILLED
的累积总和等于ORDER_SIZE
。
需要在PAIR
、TIMESTAMP
和SIDE
上设置PARTITION BY
。
例如,如果我的ORDER_SIZE
是5
,我会得到以下信息:
PAIR TIMESTAMP SIDE PRICE AMOUNT PROCEEDS DEPTH_RANK CUM_SUM_AMOUNT FILLED
BTC-USD 1592328691 ask 9478.383 0.2 1895.6766 1 0.2 0.2
BTC-USD 1592328691 ask 9478.384 0.20517 1944.680045 2 0.40517 0.20517
BTC-USD 1592328691 ask 9478.479 0.26 2464.40454 3 0.66517 0.26
BTC-USD 1592328691 ask 9479.784 0.4 3791.9136 4 1.06517 0.4
BTC-USD 1592328691 ask 9480.126 0.1335 1265.596821 5 1.19867 0.1335
BTC-USD 1592328691 ask 9485.722 0.081 768.343482 6 1.27967 0.081
BTC-USD 1592328691 ask 9485.723 0.2 1897.1446 7 1.47967 0.2
BTC-USD 1592328691 ask 9485.833 0.28082357 2663.845487 8 1.76049357 0.28082357
BTC-USD 1592328691 ask 9485.84 0.00136758 12.97264507 9 1.76186115 0.00136758
BTC-USD 1592328691 ask 9486.874 4.80287064 45564.2286 10 6.56473179 3.23813885
BTC-USD 1592328691 ask 9486.875 5.58780566 53010.81382 11 12.15253745 0
BTC-USD 1592328691 ask 9488.702 0.3665 3477.609283 12 12.51903745 0
BTC-USD 1592328691 ask 9496.899 1.1406 10832.163 13 13.65963745 0
BTC-USD 1592328691 ask 9496.9 0.14785281 1404.143351 14 13.80749026 0
BTC-USD 1592328691 ask 9496.902 0.00535416 50.84793281 15 13.81284442 0
因此,FILLED
的累积总和,直到第 10 个最深的顺序将是我们原来的 ORDER_SIZE
(5)。
如何在单个 SELECT
中完成此操作?我需要在表函数中返回它。我实际上是在 Snowflake 中执行此操作,但假设我可以访问所有常见的 SQL 窗口函数(例如 LEAD
、LAG
、RANK
等)
谢谢!!
【问题讨论】:
【参考方案1】:这看起来很简单的比较逻辑:
select . . .,
(case when cum_sum_amount < 5 then amount
when cum_sum_amount > 5 and cum_sum_amount - amount < 5 then 5 - cum_sum_amount
else 0
end)
from t;
你也可以把它表达得更迟钝:
select . . . ,
greatest(0,
least(amount,
5 - cum_sum_amount
)
)
from t;
【讨论】:
以上是关于从累积和中计算减法的主要内容,如果未能解决你的问题,请参考以下文章