计算累计和的减法

dl5txlt9  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(426)

以下是我的数据:

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 -类型 ORDER_SIZE . 我需要一个额外的专栏 FILLED 这说明 ORDER_SIZE 以及 AMOUNT 使得 FILLED 等于 ORDER_SIZE .
它将需要 PARTITION BY -埃德 PAIR , TIMESTAMP ,和 SIDE .
例如,如果我的 ORDER_SIZE5 ,我会得到以下结果:

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 下到第十个最深的订单是我们的原始订单 ORDER_SIZE (5).
我怎样才能在一个小时内完成这一点 SELECT ? 我需要它在一个表函数中返回。我实际上是在snowflake中实现的,但是假设我可以访问所有常见的sql窗口函数(例如。 LEAD , LAG , RANK 等)
谢谢!!

f1tvaqid

f1tvaqid1#

这似乎是一个非常简单的比较逻辑:

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;

相关问题