I have a database that I’ve inherited and need to create a query that generates all possible variations of a SKU. One table has the “base” SKU and the other table has all the SKU modifiers.
Example
Base SKU: MARIN could be modified into
MARINR15 MARINB15 MARING15
MARINR17 MARINB17 MARING17
MARINR19 MARINB19 MARING19
MARINR20 MARINB20 MARING20
Base SKU
ProductID SKU
----------- ---------------
532 MARIN
SKU Modifiers
ProductID OptionName OptionValue SkuModifier
----------- -------------------- ------------------------ -----------
532 Color Red R
532 Color Green G
532 Color Blue B
532 Size 17" 17
532 Size 15" 15
532 Size 19" 19
532 Size 20" 20
You can use a recursive solution (and indeed, this is probably the only viable answer). You’d probably save processing if you had a predifined ordering (because at the moment the only way I can think to do this is text concatenation).
Here is a general solution that should get you your needed results.
Note that this was written and run on DB2 (iSeries) – you may need to adjust it for SQL Server.
Which yields:
Personally, though, I think I’d try to get some sort of ordering established – this would at least allow you to knock out dealing with
optionName(although in that case you may want to further normalize the tables).Please note that the CTE
Option_Countis being used to restrict results to ‘full-length’ combinations – permutations where all the options are used, rather than just some of them.