I am looking to use pivot function to convert row values of a column into separate columns. There are 100+ distinct values in that column and hard-coding each and every single value in the 'for' clause of the pivot function would be very time consuming and not good from maintainability purposes. I was wondering if there is any easier way to tackle this problem?
Thanks
You can use Dynamic SQL in a
PIVOT
for this type of query. Dynamic SQL will get the list of the items that you want to transform on execution which prevents the need to hard-code each item:See SQL Fiddle with Demo
If you post a sample of data that you need to transform, then I can adjust my query to demonstrate.