累计SQL Server总数

时间:2018-05-23 23:46:45

标签: sql sql-server sum calculated-columns sql-server-2017

我正在尝试使用LAG函数和SUM获得累计运行总计。我想要求和的列是将行1 + 2加在一起,但它不会继续添加第1,2,3,4行等。一旦达到“重置”数量,运行总计需要返回重置金额乘以同一行的可乐币金额。

最终,我想知道历史上任何一个特定点,老虎机的进展对于水平1& 2在累积奖金支付之前和之后。 (此查询仅查看级别1)

Select Distinct  A.AID
                ,A.BID
                ,B.Level
                ,A.Date
                ,B.Reset
                ,B.Cap
                ,Description
                ,B.RateofProg
                ,A.Coinin
                ,LAG(B.RateofProg/100.00 * A.Coinin/100.00) OVER (order by AID, BID, Level) + SUM(B.RateofProg/100 * A.Coinin/100) as RunningTotal
                ,CASE When C.Eventcode = 10004500 THEN ProgressivePdAmt/100.00 Else 0 end as ProgressivePdAmt
From Payout A
Join Slot_Progression B
on A.Mnum = B.Mnum
Join Events C
on A.Date = C.Date
Where A.Mnum = '102026'
and level = '1'
and A.Coinin > '0'
Group by A.AID, A.BID, B.Level, A.Date, B.Reset, B.Cap, Description, C.ProgressivePdAmt, B.RateofProg, A.Coinin, C.Eventcode 
Order by AID, BID, Level

1 个答案:

答案 0 :(得分:0)

累计金额使用sum()而不是lag()计算。大概你想要这样的东西:

sum(B.RateofProg/100.00 * A.Coinin/100.00) OVER (order by AID, BID, Level) as RunningTotal