Skip to content
Advertisement

How to create all possible combinations with splitted items in SQL?

I have data as in below delimited format

expected output – is the combination with each element and the element itself

I am trying by splitting the data into different items as below

any idea how we can create combination with splitted items?

Thanks!

Advertisement

Answer

You have a couple of mistakes in your SQL. @data had no closing “‘”, and you have the wrong separator in your split function (‘|’ instead of ‘,’).

Once you have the split values, you can simply get the combinations by a self cross join, where the split data doesn’t match. Finally get your orderid based on length and value. Something like this should get the results you need, without the need for a cursor.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement