我正在为一些管理信息的报告构建前 10 名,该报告以以下脚本结尾:
IF @ReportType = 'SectorEQ'
BEGIN
/* Final Select with roll up for total per portfolio */
SELECT
CONVERT(VARCHAR, ReportingDate, 103) AS ReportingDate
, PortfolioID AS FundCode
, PortfolioNme AS FundName
, CASE
WHEN GROUPING_ID(ReportingDate, PortfolioID, PortfolioNme, Sector, [Rank], [Weight]) = 7 THEN 'Total'
ELSE Sector
END AS Sector
, CASE
WHEN GROUPING_ID(ReportingDate, PortfolioID, PortfolioNme, Sector, [Rank], [Weight]) = 7 THEN 102
ELSE [Rank]
END AS [Rank]
, CAST(SUM([Weight]) AS DECIMAL(22,1)) AS Percentage
FROM @FinalOutputEQ_CS
GROUP BY ReportingDate
, PortfolioID
, PortfolioNme
, Sector
, [Rank]
, [Weight] WITH ROLLUP
HAVING GROUPING_ID(ReportingDate, PortfolioID, PortfolioNme, Sector, [Rank], [Weight]) IN (1,7)
ORDER BY ReportingDate
, PortfolioID
, [Rank]
如您所见,百分比四舍五入到小数点后 1 位,这可能会导致舍入问题,因为到小数点后 1 位的百分比之和可能小于或大于 100,即 100.1 或 99.9。我需要做的是确保四舍五入到小数点后 1 位的百分比都等于 100.0。
我想做的是检查四舍五入百分比的总和是否等于 100.0,如果不是,则取前 1 名并减去其百分比的差值。
我对如何做到这一点有一些想法,但我想知道在不减慢进程的情况下确保快速有效地运行的最佳方法是什么?
编辑:示例表
ReportingDate FundCode FundName Sector Rank Percentage
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Consumer Discretionary 1 16.1
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Health Care 2 13.8
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Information Technology 3 11.8
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Telecommunication Services 4 10.3
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Consumer Staples 5 10.1
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Industrials 6 8.7
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Financials 7 6.8
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Materials 8 6.5
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Energy 9 5.2
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Utilities 10 0.7
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Cash and Bonds 101 10.1
28/02/2013 XXXXXXXX XXXXXXXXXXXXXXX Total 102 100.1