WeightedPerformance = SUM ( (Allocation/SUM(Allocation Per FundType) * Performance)
信用示例:绩效 | |||
---|---|---|---|
我想根据以下公式来得出每种基金类型的加权性能: |
Sum of Allocation = 1.50 (0.10+0.20+0.30+0.40+0.50)
WeightedPerformance = Sum(((0.1/1.5)*0.15) + ((0.2/1.5)*0.25) + ((0.3/1.5)*0.35) + ((0.4/1.5)*0.45)+ ((0.5/1.5)*0.55) )
WeightedPerformance of Credit = 0.416666666666667
如何使用每个funcete的T-SQL对此进行计算? 到目前为止,Query:
SELECT
FundType,
Sum((Allocation/Sum(Allocation))*Performance) as WeightedPerformance
FROM
FundAllocation
GROUP BY
FundType
但是得到: Cannot在包含聚合或子查询的表达式上执行聚合函数。
注:这个问题不是
SQLServer的重复,“无法在包含聚合或子查询的表达式上执行汇总函数”,但是Sybase可以使用,因为建议的问题是使用子句分组的,以进行进一步汇总。在这种情况下,它由Fundtype分组。
您可以使用窗口函数为此
WITH summed AS (
SELECT
fa.*,
SUM(fa.Allocation) OVER (PARTITION BY fa.FundType) AS AllocationPerFundType
FROM FundAllocation fa
)
SELECT
fa.FundType,
SUM(fa.Allocation / fa.AllocationPerFundType * fa.Performance) as WeightedPerformance,
SUM(Allocation) AS SumAllocation
FROM summed fa
GROUP BY
fa.FundType;