Code Vamping

MSSQL Pivot

Published on 2018-09-25

Pivots (turning a column’s values into actual columns) is a very common activity. Spreadsheet programs have robust support for it. But Standard SQL? Not so much. The Problem Create Table orders ( orderNumber int, sku char(3), quantity int, salesPerson varchar(10) ); insert into orders values ( 1, 'ZR34', 2, 'Mary'), ( 1, 'AS99', 1, 'Mary'), ( 2, 'ZR34', 1, 'Jim'), ( 2, 'MB01', 1, 'Jim'); The ubiquitous order table with the SKU, quantity and sales person....