Lesson 42. Pivoting Data

As functions go, PIVOT is actually pretty wack. I am only bringing it up here because I talk about it again in the solutions section.

This function is so wack, I’m not going to put ANY work into this lesson. I’m going to straight phone it in and just use the exact same code example from the MS documentation.

You have my full permission to make a Thug Life gif out of my professional picture.

Examples

A Completely Phoned In Example Of Pivoting Data

In [ ]:

USE AdventureWorks2016

-- Pivot table with one row and five columns  
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,   
[0], [1], [2], [3], [4]  
FROM  
(
SELECT DaysToManufacture, StandardCost   
FROM Production.Product
) AS SourceTable  
PIVOT  
(  
AVG(StandardCost)  
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])  
) AS PivotTable;

Last updated