Following the example in https://www.mssqltips.com/sqlservertip/1019/crosstab-queries-using-pivot-in-sql-server/, I have a PIVOT
that works but is not very flexible. It is basically
SELECT * FROM
(SELECT
...) AS raw
PIVOT (
AGGREGATE(aColumn)
FOR x IN ( [c1], [c2], ...) AS pvt;
But the columns I want in my pivot table are listed in another table. What I'd really like to do is something like:
SELECT * FROM
(SELECT
...) AS raw
PIVOT (
AGGREGATE(aColumn)
FOR x IN ( (SELECT cName FROM otherTable) ) AS pvt;
But SELECT
there ends up being a syntax error. Is there any work around for this? I really don't want to have to edit this query every time I add another field that should be a column in the output.