The only solution i could work out for this is to create the script below which basically summed up the detail to give a total. Then after my CTE i subtracted this from the top 1 and the total:
IF @ReportType = 'SectorEQ'
BEGIN
INSERT INTO @RoundingDifference
SELECT
PortfolioID
, CAST(SUM([Weight])-100 AS DECIMAL(22,1)) AS [Weight]
FROM @FinalOutputEQ_CS
WHERE Sector <> 'Total'
GROUP BY
ReportingDate
, PortfolioID
, PortfolioNme
END
IF @ReportType = 'SectorEQ'
BEGIN
;;WITH CTE AS
(
/* 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)
)
SELECT
ReportingDate
, FundCode
, FundName
, Sector
, [Rank]
, CASE
WHEN [Rank]IN (1,102) THEN Percentage - RD.[Weight]
ELSE Percentage
END AS Percentage
FROM CTE AS CTE
INNER JOIN @RoundingDifference AS RD
ON RD.PortfolioID = CTE.FundCode
ORDER BY ReportingDate
, FundCode
, [Rank]
END