考虑时间衰减因素,为特定用户添加得分列的值

Add the values of score column for a specific user considering time decay factor

我在下面link问了一个问题,其中一位成员帮助我解决了大部分问题(计算列t和列pre_score)。但是我需要再计算一列。我在下面解释了细节 link.

综上所述,如何使用t列和pre_score列计算智力资本列? intellectual-capital 专栏考虑了之前所有比赛的预分,然后将每个预分乘以 e^(从 competition/500 开始的天数)。在这个例子中,对于每个用户,我们最多有 2 场以前的比赛,但在我的数据集中,它可能甚至超过 200 场比赛,因此我需要查询考虑比赛的所有分数和每场比赛已经过去的时间。 --> e 的值大约为 2.71828

competitionId UserId    t pre_score intelectual-capital
1                100   
2             100        -4  3000 3000* POWER (e, -4/500)
3                100        -5  4000 3000*POWER(e,-9/500) + 4000*POWER(e, -5/500)
1                200   
4             200        -19  3000 3000*POWER(e,-19/500)
1                300   
3             300        -9  3000 3000*POWER(e,-9/500)
4             300       -10  1200 3000*POWER(e,-19/500)+ 1200*POWER(e,-10/500) 
1             400   
2                400        -4 3000  3000* POWER(e, -4/500)
3                400        -5 4000 3000* POWER(e, -9/500) + 4000*POWER(e,-5/500)

这个结果:

| prev_score | intellectual_capital | competitionsId | UserId |                 date | score | day_diff |      t | prev_score |
|------------|----------------------|----------------|--------|----------------------|-------|----------|--------|------------|
|     (null) |               (null) |              1 |    100 | 2015-01-01T00:00:00Z |  3000 |       -4 | (null) |     (null) |
|       3000 |              2976.09 |              2 |    100 | 2015-01-05T00:00:00Z |  4000 |       -5 |     -4 |       3000 |
|       4000 |              6936.29 |              3 |    100 | 2015-01-10T00:00:00Z |  1200 |   (null) |     -5 |       4000 |
|     (null) |               (null) |              1 |    200 | 2015-01-01T00:00:00Z |  3000 |      -19 | (null) |     (null) |
|       3000 |              2888.13 |              4 |    200 | 2015-01-20T00:00:00Z |  1000 |   (null) |    -19 |       3000 |
|     (null) |               (null) |              1 |    300 | 2015-01-01T00:00:00Z |  3000 |       -9 | (null) |     (null) |
|       3000 |              2946.48 |              3 |    300 | 2015-01-10T00:00:00Z |  1200 |      -10 |     -9 |       3000 |
|       1200 |              4122.72 |              4 |    300 | 2015-01-20T00:00:00Z |  1000 |   (null) |    -10 |       1200 |
|     (null) |               (null) |              1 |    400 | 2015-01-01T00:00:00Z |  3000 |       -4 | (null) |     (null) |
|       3000 |              2976.09 |              2 |    400 | 2015-01-05T00:00:00Z |  4000 |       -5 |     -4 |       3000 |
|       4000 |              6936.29 |              3 |    400 | 2015-01-10T00:00:00Z |  1200 |   (null) |     -5 |       4000 |

由此查询生成,现在包含 e

with Primo as (
      select
              *
            , datediff(day,lead([date],1) over(partition by userid order by [date]),[date]) day_diff
      from Table1
      )
, Secondo as (
      select
              *
           , lag(day_diff,1) over(partition by userid order by [date]) t
           , lag(score,1) over(partition by userid order by [date]) prev_score
      from primo
      )
 select
        prev_score
      , sum(prev_score*power(2.71828,t/500.0)) over(partition by userid order by [date]) intellectual_capital
      , competitionsId,UserId,date,score,day_diff,t,prev_score
from secondo

Demo