Does t-sql include a pivot function?
- User
Does t-sql include a pivot function?
- Assistant
Yes, T-SQL (Transact-SQL) includes a PIVOT function that allows you to transform rows into columns. The PIVOT function is used to reshape data from a table or a query result into a matrix of columns, which can be useful for creating summary data or aggregating values.
Here's an example of the syntax for the PIVOT function:
SELECT * FROM (SELECT col1, col2, col3, ..., colN FROM table1) AS pivotedTable PIVOT ( MAX(col1) FOR col2 IN ([col2], [col3], ..., [colN]) ) AS pivotedResult;In this example,
table1is the table or query result that you want to pivot, andcol1is one of the columns that you want to use as the basis for the pivot. ThePIVOTclause specifies the columns that you want to use as the pivot columns, and the function returns the maximum value for each combination of columns.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.