I have the following table with the following sample data
ID Language Question SubQuestion SubSubQuestion TotalCount TotalPercent
3 E 9 0 1 88527 73%
3 E 9 0 2 19684 16%
3 E 9 0 3 12960 11%
3 E 9 0 9 933 1%
I want all in one row like this
ID Language TotalCount901 TotalPercent901 TotalCount902 TotalPercent902 TotalCount903 TotalPercent903
3 E 88527 73% 19684 16% 12960 11%
I’ve tired using the pivot command, but it dosnt to work for me.
I made a few assumptions based on your column names, but it looks like you want to use something similar to this. This applies both an
UNPIVOTand then aPIVOTto get the values in the columns you requested:See SQL Fiddle with Demo
Note: when performing the
UNPIVOTthe columns need to be of the same datatype. If they are not, then you will need to convert/cast to get the datatypes the same.If you have an unknown number of values to transform, you can use dynamic sql:
See SQL Fiddle with Demo