Skip to content
Advertisement

Transpose Rows Into Columns by Delimiter Column Name and Column Title

I’m slightly stuck on this issue and need some SQL assistance please. Iv got a hierarchy table from someone who needs help transposing it and putting them into actual columns of another table. Here is a sample line of the table post querying it. I don’t have access to adjust anything besides working from this table result:

Output:

The above table can go to around 10 levels deep. Anyways I thankfully know the maximum depth of the final table and I have access to where the results should be stored its just above converting the above results to read the path and decide the appropriate column for it and then inserting the value into the end table.

Visual example of this end table (Desired Result):

Advertisement

Answer

This gets you most of the way there. As, however, the definition of your data changes from row to row, then there’s no way to get the data in the order you want if you want to do it dynamically. (for Example should state be in position 2 or 3, as it appears in both? Without knowlegde of your data, or a look up table, that is impossible. This is a dynamic solution, however, it shows you the code it generates to get the results you’re after, if you want to manually code all of your positions.

This also makes use of DelimitedSplit8k_LEAD, as STRING_SPLIT does not supply the ordinal position of a item in a delimited list; making it useless here.

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