Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6749847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:44:19+00:00 2026-05-26T12:44:19+00:00

I have a database that I’ve inherited and need to create a query that

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:44:19+00:00Added an answer on May 26, 2026 at 12:44 pm

    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.

    WITH Combined(productId, options, combination, level) as (
                  SELECT productId, optionName, skuModifier, 1
                  FROM #Modifiers
                  UNION ALL
                  SELECT a.productId, a.options || b.optionName,
                         a.combination || b.skuModifier, a.level + 1
                  FROM Combined as a
                  JOIN #Modifiers as b
                  ON b.productId = a.productId
                  AND a.options not like ('%' || b.optionName || '%')),
         Option_Count(productId, count) as (SELECT productId, COUNT(DISTINCT optionName)
                                            FROM #Modifiers
                                            GROUP BY productId)
    SELECT a.sku || COALESCE(b.combination, '')
    FROM #Base as a
    LEFT JOIN (Combined as b
               JOIN Option_Count as c
               ON c.productId = b.productId
               AND c.count = b.level)
    ON b.productId = a.productId)
    

    Which yields:

    MARIN17R        
    MARIN15R        
    MARIN19R        
    MARIN20R        
    MARIN17G        
    MARIN15G        
    MARIN19G        
    MARIN20G        
    MARIN17B        
    MARIN15B        
    MARIN19B        
    MARIN20B        
    MARINR17        
    MARING17        
    MARINB17        
    MARINR15        
    MARING15        
    MARINB15        
    MARINR19        
    MARING19   
    MARINB19   
    MARINR20   
    MARING20   
    MARINB20
    

    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_Count is being used to restrict results to ‘full-length’ combinations – permutations where all the options are used, rather than just some of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database that is similar to the following: create table Store(storeId) create
I have a database that my program will query. it has 3 tables all
I have a database that contains a table that looks a bit like this:
i have a database that already has a users table COLUMNS: userID - int
I have a database that I need to connect to and select from. I
I Have Database that contains 4 tables TABLE TBLCARTITEM (CART_ID, ITEM_ID, PROMOTION_ID, many more
I have a database that contains two tables: entries tags The entries table contains
I have one database that has a bigger size than expected, how can I
I have a database that deconstructs an x,y coordinate pair table into specific dataitems.
I have a database that I use for all my development and testing and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.