Skip to content
Advertisement

Pivoting a dynamic column based on split value

I Have a couple of tables as per below

Position table

Id PositionName
1 Developer
2 Analyst
3 Tester

Employee table

Id Name Positions
1 John 1,2
2 Lisa 3
3 Smith 1
4 Willow NULL
5 Burly 2,3

From the above tables, what is the query to produce a pivoted report as per below?

Id Name Developer Analyst Tester
1 John Y Y N
2 Lisa N N Y
3 Smith Y N N
4 Willow N N N
5 Burly N Y Y

I am stuck with the fact I have to do some split string and use the CASE WHEN to apply Y or N to the pivot.

here’s my playground in SQL fiddle http://sqlfiddle.com/#!18/2ad8d/31

Advertisement

Answer

I’m having trouble getting Fiddle to work right now, but the code isn’t too bad to just reproduce in full. Note that you should watch your @Cols variable to make sure all of your position names work as column names in real life, that’s often not the case! Also note that your original example had duplicate employee IDs, I gave them unique values.

EDIT: Fixed the missing close parenthesis in the dynamic query.

EDIT: Note: This does not use a split function, it takes advantage of the facts that the ID must be an integer and the positions listed are delimited in a predictable way. We don’t need a list of position IDs, we only need to know if the position ID in question is in the list. We prepend and append commas so we can search on “,1,” and not just “1” because “1” would also match “21” but “,1,” only matches the single ID.

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