假设我们有以下 SQL Server 表
date, qty, debit
我想根据此处显示的公式计算
NewDebit
值。我已经尝试了一切 - 分组、分区、cte - 但就是没有发生......
我尝试的任何东西要么是递归CTE,要么是分区依据,似乎我无法获得之前计算结果的总和。
参见示例
with trng as( -- ordering for recursive sequentially process table
select *,row_number()over(order by dt)rn from test
)
,r as( -- anchor - first row
select 0 as lvl,t.dt, qty,debit,rn
,cast(0.0 as float) tsum
,debit as newdebit
,cast('0' as varchar(100)) expr
,formula,testvalue
from trng t
where rn=1
union all
select r.lvl+1 lvl,t.dt, t.qty,r.debit,t.rn
,(r.newdebit+r.tsum) tsum -- cumulative sum
,(r.newdebit+r.tsum)/t.qty newdebit -- partial sum
-- for debug and clarity
,cast(concat(r.expr,'+',r.newdebit) as varchar(100)) expr
,t.formula,t.testvalue
from r inner join trng t on t.rn=(r.rn+1)
)
select dt,qty,debit,newdebit,lvl,rn,tsum
,concat('(',expr,')/',qty) exprn
,formula,testvalue
from r
输出是
dt | 数量 | 借方 | 新借记 | lvl | rn | tsum | exprn | 公式 | 测试值 |
---|---|---|---|---|---|---|---|---|---|
2024-02-01 | 5 | 100 | 100 | 0 | 1 | 0 | (0)/5 | 100 | 100 |
2024-02-02 | 8 | 100 | 12.5 | 1 | 2 | 100 | (0+100)/8 | 100/8 | 12.5 |
2024-02-03 | 3 | 100 | 37.5 | 2 | 3 | 112.5 | (0+100+12.5)/3 | (100+12.5)/3 | 37.5 |
2024-02-04 | 6 | 100 | 25 | 3 | 4 | 150 | (0+100+12.5+37.5)/6 | (100+12.5+37.5)/6 | 25 |
2024-02-05 | 4 | 100 | 43.75 | 4 | 5 | 175 | (0+100+12.5+37.5+25)/4 | (100+12.5+37.5+25)/4 | 43.75 |
有测试数据
create table test (dt date, qty int, debit float,formula varchar(100),testValue float);
insert into test values
('2024-02-01',5,100,'100','100')
,('2024-02-02',8,150,'100/8',12.5)
,('2024-02-03',3, 50,'(100+12.5)/3',37.5)
,('2024-02-04',6,120,'(100+12.5+37.5)/6',25)
,('2024-02-05',4, 80,'(100+12.5+37.5+25)/4',43.75)
;