用cap计算SUM并按比例乘以SQL中的系数

问题描述

我的桌子或多或少看起来像这样:

+--------+---------------------+--------+------+
| CustId |      TransDate      | Amount | Coef |
+--------+---------------------+--------+------+
|      1 | 2020-09-04 11:02:00 |    400 |  0.1 |
|      1 | 2020-09-04 12:05:00 |    500 |  0.2 |
|      1 | 2020-09-04 13:02:00 |    400 |  0.1 |
|      1 | 2020-09-04 13:11:00 |    600 |  0.4 |
|      2 | 2020-09-04 10:01:00 |    300 |  0.3 |
|      2 | 2020-09-04 11:02:00 |    700 |  0.2 |
+--------+---------------------+--------+------+

我要实现的目标是给每个客户“奖金”,以“金额* Coef”计算,但总交易额的上限为1,500。对于总交易额小于1,500(按交易顺序)的客户交易授予“奖励”。

例如,客户“ 1”将在前3笔交易中获得全额奖励,并在第4笔交易中获得1,500的差额奖励。基本上,最终结果应如下所示:

+--------+---------------------+--------+------+---------------------------------------------------+
| CustId |      TransDate      | Amount | Coef |                       Bonus                       |
+--------+---------------------+--------+------+---------------------------------------------------+
|      1 | 2020-09-04 11:02:00 |    400 |  0.1 | 40                                                |
|      1 | 2020-09-04 12:05:00 |    500 |  0.2 | 100                                               |
|      1 | 2020-09-04 13:02:00 |    400 |  0.1 | 40                                                |
|      1 | 2020-09-04 13:11:00 |    600 |  0.4 | 80 /*(this is given only for the remaining 200)*/ |
|      2 | 2020-09-04 10:01:00 |    300 |  0.3 | 90                                                |
|      2 | 2020-09-04 11:02:00 |    700 |  0.2 | 140                                               |
+--------+---------------------+--------+------+---------------------------------------------------+

谢谢!

干杯。

解决方法

如果我理解正确,则需要一个累加的总和和一些逻辑:

(\S+)\s+exp+
,

[EDIT]:现在使用MAX函数限制奖金金额。

数据

drop table if exists #tTEST;
go
select * INTO #tTEST from (values 
(1,'2020-09-04 11:02:00',400,0.1),(1,'2020-09-04 12:05:00',500,0.2),'2020-09-04 13:02:00','2020-09-04 13:11:00',600,0.4),'2020-09-05 13:11:00',(2,'2020-09-04 10:01:00',300,0.3),(3,700,0.2)) V(CustId,TransDate,Amount,Coef);

查询

declare @bonus_limit            int=1500;

with data_cte as (
    select *,sum(amount) over (partition by custid order by transdate) cum_amount,@bonus_limit-sum(amount) over (partition by custid order by transdate) cum_amount_diff
    from #tTEST t) 
select *,(SELECT Max(v)  FROM (VALUES (0),(case when cum_amount_diff<0 then (Amount+cum_amount_diff)*Coef
                    else Amount*Coef end)) AS value(v)) Bonus
from data_cte;

结果

+--------+---------------------+--------+------+------------+-----------------+-------+
| CustId |      TransDate      | Amount | Coef | cum_amount | cum_amount_diff | Bonus |
+--------+---------------------+--------+------+------------+-----------------+-------+
|      1 | 2020-09-04 11:02:00 |    400 |  0.1 |        400 |            1100 |  40.0 |
|      1 | 2020-09-04 12:05:00 |    500 |  0.2 |        900 |             600 | 100.0 |
|      1 | 2020-09-04 13:02:00 |    400 |  0.1 |       1300 |             200 |  40.0 |
|      1 | 2020-09-04 13:11:00 |    600 |  0.4 |       1900 |            -400 |  80.0 |
|      1 | 2020-09-05 13:11:00 |    600 |  0.4 |       2500 |           -1000 |   0.0 |
|      2 | 2020-09-04 10:01:00 |    300 |  0.3 |        300 |            1200 |  90.0 |
|      3 | 2020-09-04 11:02:00 |    700 |  0.2 |        700 |             800 | 140.0 |
+--------+---------------------+--------+------+------------+-----------------+-------+