FROM - Using PIVOT & UNPIVOT
implicit GROUP BY on remaining columns, aggregate of value_column, spread by pivot_column
What changes
- Input
- input rowset
- Output or change
- rowset with one column per IN-list value
Syntax at a glance
SELECT [ <non-pivoted column> [ AS <column name> ] , ] ... [ <first pivoted column> [ AS <column name> ] , [ <second pivoted column> [ AS <column name> ] , ] ... [ <last pivoted column> [ AS <column name> ] ] ] FROM ( <SELECT query that produces the data> ) AS <alias for the source query> PIVOT ( <aggregation function> ( <column being aggregated> ) FOR <column that contains the values that become column headers> ...
Work example
A controlled task needs the behavior documented for FROM - Using PIVOT & UNPIVOT before it is added to a batch.
Read the linked Microsoft syntax and test FROM - Using PIVOT & UNPIVOT with synthetic input in an isolated database. Record its scope, permissions, and error path.
Check your work FROM - Using PIVOT & UNPIVOT. The observed FROM - Using PIVOT & UNPIVOT behavior matches its written purpose and the test has no effect on permanent data or credentials.
Open Microsoft Learn for full syntax, permissions, and details for your version.