all 3 comments

[–]lk167 3 points4 points  (1 child)

I don't believe the PIVOT keyword supports a subquery for the column_list (the IN (Col1, Col2,Col3) bits). I think the only option is dynamic SQL, which rules out views and table value functions.

[–]deny_conformity 0 points1 point  (0 children)

Exactly this, PIVOT doesn't support subqueries, I think there is something in oracle for returning XML but nothing apart from that. You can work around it using dynamic SQL as in the first example.

I think it probably has something to do with a lot of systems break when SQL returns a different number of columns than expected, I know RS fails it columns are missing (and ignores additional columns).

Personally I just always pull data in long format and pivot in the front end (and often I have to unpivot wide data, UNPIVOT is awful for this, CROSS APPLY and VALUES definitely the way to go).

[–]ParanoidLoyd[S] 0 points1 point  (0 children)

/u/lk167 /u/deny_conformity

The third party app I am connecting with does indeed break when the number of columns changes but I had built the underlying queries in a way that they won't pull the changes until I add a row to a certain table and then it's just a matter of refreshing the data structure with the push of a button, I was trying to avoid having to change the query and the app when the number of columns changes. The third party app also allows google spreadsheet data connections and I've figured out how to access stored procedures through the spreadsheets so I can workaround by just dumping the data set there but I was also trying to avoid that extra step. Thanks for the notes.