Quick question that’s bugging me.
When using PIVOT in T-SQL, can the ‘FOR..IN’ portion reference a table rather than a defined list of values?
Example, instead of something like:-
PIVOT (Count(widgets) For Team IN ([Dave],[Peter],[John]....etc))
Could you do something like:-
PIVOT (Count(widgets) for Team IN (SELECT TeamLeader FROM Teams))
I can find no reference to confirm whether or not this is possible.
No, this is not possible.
The
PIVOToperator syntax dictates that these values is entered as a list:But you can do this dynamically, something like: