Working with the UNPIVOT operator
The UNPIVOT
operator performs the complete opposite operation of the PIVOT
operator. It rotates columns into rows. For example, in the previous section, the supplier’s name [A Datum Corporation]
would be rotated to record the row value and all purchase order values would be added to a new column, [PurchaseOrder]
.
In the following example, we have provided a SQL script to create a new table, pvtSupplierPurchaseOrder
, with one record storing values, similar to the output from the dynamic PIVOT
example discussed in the previous section:
------------------------------------------------------------- -- Create sample table for UNPIVOT operator testing -------------------------------------------------------------DROP TABLE IF EXISTS [dbo].[pvtSupplierPurchaseOrder]; GO -- Create the table and insert values as portrayed in the previous example.CREATE TABLE pvtSupplierPurchaseOrder ( TotalPurchaseOrders VARCHAR(25) , [A Datum Corporation] INT ...