A pivot (or crosstab) converts row data into columnar data, and vice versa for an unpivot. Not all databases support explicit PIVOT syntax but the functionality can be implemented, often using decision logic (CASE statements, etc), aggregate functions, or an extension.
Databases that support PIVOT
/UNPIVOT
syntax:
- Oracle 11g+
- SQL Server 2005+
PostgreSQL includes crosstab()
in the tablefunc
optional module.
While MySQL does not have a PIVOT
function, this can be emulated with plain SQL using aggregate functions and CASE
statements, which works in all other RDBMS as well.