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

时间:2017-11-21 04:34:21

标签: select group-by sum sql-server-2016 row-number

我在下面的链接中提出了一个问题,其中一个成员帮助我解决了大部分问题(计算列t和列pre_score)。但我需要再计算一列。我在以下链接中解释了详细信息。

Previous question

总之,我如何使用列t和列pre_score计算知识资本列?智力资本专栏考虑所有先前比赛的预分数,然后将每个预分数乘以e ^(从该比赛中获得的天数/ 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)

1 个答案:

答案 0 :(得分:1)

结果:

| 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