Hi Experts, I'm hoping someone knows the alternative to a CASE statement in a join. I have a sproc that builds a table that's a cross join of a set of dimension tables. Its purpose is to create a row for all possible permutations of the dimension values.
The trick is that the last dimension changes depending on one of the parameters. This code doesn't work, but it represents what I need.
declare @Dim1 table (Dim1 varchar(50)) insert into @Dim1 select 'A' union all select 'B' union all select 'C' declare @Dim2 table (Dim2 varchar(50)) insert into @Dim2 select 'D' union all select 'E' union all select 'F' declare @Dim3 table (Dim3 varchar(50)) insert into @Dim3 select 'G' union all select 'H' union all select 'I' declare @PossibleDimA table (Dim varchar(50)) insert into @PossibleDimA select '1' declare @PossibleDimB table (Dim varchar(50)) insert into @PossibleDimA select '2' union all select '3' declare @AdditionalDimension varchar(50) --create table MyTable as (Dim1 varchar(50), Dim2 varchar(50) Dim3 varchar(50), Dim_that_changes varchar(50)) --insert into MyTable select Dim1, Dim2, Dim3 from @Dim1 cross join @Dim2 cross join @Dim3 cross join case when @AdditionalDimension = 'A' then @PossibleDimA else @PossibleDimB end
If the sproc receives @AdditionalDimension = 'A', the table should include the single value of 1 from @PossibleDimensionA, creating a 27-row table. If @AdditionalDimension = 'B' then the table should contain 54 rows.
In real life, there are 9 possible tables that could be used as additional dimensions, but the problem is the same whether it's two or nine. I've considered an IF...ELSEIF structure, but with 9 possible additional dimension tables, that's a lot of code.
Does anyone here know of an alternative? Thx in advance!