我有一个名为T的表,它有三行如下....
Id allocation multiplier multipliedallocation multipliedallocationsum
1 20.000 1.0008 20.016 100.052
2 50.000 1.0006 50.030 100.052
3 30.000 1.0002 30.006 100.052
我想使用这个表来产生一些投影结果,这些结果会在几个月内改变分配,如下所示......
Id allocation multiplier multipliedallocation multipliedallocationsum mnth
1 20.000 1.0008 20.016 100.052 1
2 50.000 1.0006 50.030 100.052 1
3 30.000 1.0002 30.006 100.052 1
1 20.005 1.0008 20.021 100.050 2
2 50.003 1.0006 50.033 100.050 2
3 29.990 1.0002 29.996 100.050 2
1 20.011 1.0008 20.027 100.052 3
2 50.008 1.0006 50.038 100.052 3
3 29.981 1.0002 29.987 100.052 3
1 20.017 1.0008 20.033 100.054 4
2 50.012 1.0006 50.042 100.054 4
3 29.971 1.0002 29.979 100.054 4
etc
任何行上的multipliedallocation =分配x乘数。
任何行上的multipliedallocationsum =该月份的乘法分配总和
月份n的分配是第n-1个月的乘法分配除以第n-1个月的乘数分数,乘以100
这是我到目前为止提出的递归cte SQL语句,但它并没有达到我的预期......
WITH Alloc (Id, allocation, multiplier, multipliedallocation, mnth)
AS
(
SELECT Id, allocation, multiplier, allocation * multiplier AS multipliedallocation, 1 AS mnth
FROM T
),
AllocProjected (Id, allocation, multiplier, multipliedallocation, multipliedallocationsum, mnth)
AS
(
SELECT a.Id, a.allocation, a.multiplier, a.multipliedallocation,
SUM(a.multpliedallocation) OVER (PARTITION BY a.Id, a.mnth), 1
FROM Alloc a
UNION ALL
SELECT a.Id,
CONVERT(decimal(6,3), (a.multipliedallocation / a.multipliedallocationsum) * 100.0) AS allocation,
a.multiplier AS multiplier,
a.multiplier,
a.multiplier * (a.multipliedallocation / a.multipliedallocationsum) * 100.0 AS multipliedallocation,
SUM(a.multipliedallocation) OVER (PARTITION BY a.mnth) AS multipliedallocationsum,
a.mnth + 1
FROM AllocProjected a
WHERE a.mnth < 24
)
SELECT * FROM AllocProjected
ORDER BY mnth
multipliedallocationsum似乎只显示单行的乘法分配,而不是整月的乘法分配总和(即跨3行)