title: PIVOT Table Order of Operations
body: <p>Can anyone tell me the order of evaluation / operation / execution for a PIVOT statement in MS SQL 2014 ?  In other words what are the general steps taken by SQL to arrive at the pivoted output given the code below? This would include the SELECT, INTO, FROM and PIVOT clauses.</p>
  
  <pre><code>WITH NumberShipsCTE2 AS
  
  (
  SELECT
      ShipperID -- grouping / column 
      , CustID -- spreading / rows
      , OrderID
      , 1 AS AggCol -- aggregation column
  FROM Sales.Orders
  ) 
  
  SELECT CustId, [1], [2], [3]
  INTO Sales.NumberShipsTBL
  FROM NumberShipsCTE2
  PIVOT(COUNT(OrderID) FOR ShipperId IN ([1],[2],[3]) ) AS X;
  </code></pre>
  
  <p>Based on the TSQL2012 database.</p>
  
  <p>Note I have a pretty good understanding how CTEs work and I am not looking for an explanation on this function.</p>