Question

I have searched a lot to use variable names as column names, however I could not find anything. I have a table which has nameMaster column and xVariable,yVariable,zVariable underneath. I know I can use:

case nameMaster when 'xVariable' then total else 0 end AS xVariable
case nameMaster when 'yVariable' then total else 0 end AS yVariable
case nameMaster when 'zVariable' then total else 0 end AS zVariable

However I like to do this in a loop, not manually.

My table is like this:

nameMaster | total
---------------------
xVariable  | 10.000
yVariable  | 20.000
zVariable  | 30.000

What I want to display is like this:

xVariable | yVariable | zVariable
---------------------------------
10.000      20.000       30.000

Thanks in advance..

Was it helpful?

Solution

Well the thing that you want to achieve is called PIVOT table and it will transform your columns in to rows based on your columns selections.

You can refer this article to see how the pivot happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top