我有两个结果集,如下所示:
@四舍五入
PortfolioID Duration
AAA -0.1
@FinalOutput
ReportingDate FundCode Sector Rank Duration Weight
31/07/2013 AAA Sector1 1 0 33.5
31/07/2013 AAA Sector2 2 0.9 29.6
31/07/2013 AAA Sector3 3 0.6 17.3
31/07/2013 AAA Sector4 4 0.8 11.8
31/07/2013 AAA Sector5 5 0.1 3.1
31/07/2013 AAA Sector6 6 0.1 1.3
31/07/2013 AAA Sector7 7 0 0.4
31/07/2013 AAA Sector8 8 -0.9 0
31/07/2013 AAA Sector9 11 0 -1.3
31/07/2013 AAA Sector10 100 0 2.8
31/07/2013 AAA Sector11 101 0 1.5
31/07/2013 AAA Total 102 1.6 100
我需要做的是从等级 1 和等级 102 的持续时间中减去 @Rounding 中的持续时间,但是如果等级 1 是“Sector1”,那么我需要从等级 2 中减去它。在这种情况下这可能吗陈述?我已经创建了以下内容,但我想不出如何满足“Sector1”的情况,如果它是排名 1,则永远不会被减去?
SELECT
ReportingDate
, FundCode
, Sector
, [Rank]
, CASE
WHEN [Rank] IN (1,102) THEN [Duration Contribution] - RD1.Duration
ELSE [Duration Contribution]
END AS [Duration Contribution]
, CASE
WHEN [Rank] IN (1,102) THEN Percentage - RD.[Weight]
ELSE Percentage
END AS Percentage
FROM CTE AS CTE
INNER JOIN @RoundingDifference AS RD -- Portfolio Weight Rounding
ON RD.PortfolioID = CTE.FundCode
INNER JOIN @RoundingDifferenceDur AS RD1 -- Duration Contribution Rounding
ON RD1.PortfolioID = CTE.FundCode
WHERE (Percentage <> 0.0
OR [Duration Contribution] <> 0.0)
ORDER BY ReportingDate
, FundCode
, [Rank]
所以我正在寻找的结果是 Sector 1 Duration 仍然 = 0 但 Sector 2 Duration 和 Total 分别为 1.0 和 1.7。