You can join the result of the cube onto the DimGroup
table.
There are two problems. It's fairly simple to deal with joining on nulls. It's slightly harder to deal with the multiple meanings of null. Fortunately there's a Grouping
function that can distinguish between an underlying null value and a null added by the cube.
This answer assumes that when you have a null in the DimGroup that you want the cube meaning - i.e. the aggregate over any underlying value.
Select
d.GroupId,
d.Dim1,
d.Dim2,
d.Dim3,
total
From (
Select
sum(Amount) total,
Dim1,
Dim2,
Dim3
From
FactTable
Group By
Cube(Dim1,Dim2,Dim3)
Having -- exclude underlying values with unknown (null) dims
(Dim1 Is Not Null or Grouping(Dim1) != 0) And
(Dim2 Is Not Null or Grouping(Dim2) != 0) And
(Dim3 Is Not Null or Grouping(Dim3) != 0)
) c
inner join
DimGroups d
on (c.Dim1 = d.Dim1 Or (c.Dim1 Is Null and d.Dim1 Is Null)) And
(c.Dim2 = d.Dim2 Or (c.Dim2 Is Null and d.Dim2 Is Null)) And
(c.Dim3 = d.Dim3 Or (c.Dim3 Is Null and d.Dim3 Is Null))