0
我有兩個結果集,如下圖所示:tsql - 我可以在CASE語句中執行此操作嗎?
@Rounding
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
什麼,我需要能夠做的是從減去持續時間在@Rounding等級1和等級102的持續時間,但是如果等級1是「扇區1」,那麼我需要將它從等級2中減去。這是否可能在個案聲明中?我已經創建了下面的內容,但我想不出如何爲「Sector1」提供案例,以便在第一級時不會被扣除?
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]
所以我期待的結果是扇區1持續時間仍然= 0,但扇區2持續時間和總數分別爲1.0和1.7。
那就完成了這項工作。謝謝你的幫助 –