I’ve created a query and i would like to results to look like as follows:
Reporting Date Fund AssetClass %
31/10/2012 1 Equity 10
31/10/2012 1 Bond 40
31/10/2012 1 Cash 40
31/10/2012 1 Balanced 10
31/10/2012 1 Other 0
The problem i’m having above is where the % is 0 and it obviously not showing as there is no data. However i would like it to show.
So i thought the best solution would be to create a temp table with all the Asset Classes in it and then RIGHT OUTER JOIN to it from my worktable so that it would populate a line with no data. The code is as follows:
SELECT
ReportingDate
, PortfolioID
, AC.AssetClass
, ROW_NUMBER() OVER (PARTITION BY PortfolioID ORDER BY SUM(Percentage) DESC) AS [Rank]
, CAST(SUM(Percentage) AS DECIMAL(22,1)) AS [Weight]
FROM @Worktable as WT
RIGHT OUTER JOIN @AssetClass AS AC
ON WT.AssetClass = AC.AssetClass
GROUP BY WT.ReportingDate, WT.PortfolioID, AC.AssetClass
ORDER BY [Weight] DESC
My problem is that when this returns it looks like this:
Reporting Date Fund AssetClass %
31/10/2012 1 Equity 10
31/10/2012 1 Bond 40
31/10/2012 1 Cash 40
31/10/2012 1 Balanced 10
NULL NULL Other NULL
How can i populate the NULL’s with data in this script? Is there potentially another better way of doing this?
1 Answer