title: GROUP BY statement with a PIVOT command in T-SQL
body: <p>I have a SQL query that involves a PIVOT command that creates the correct table but now I need to GROUP BY one of the columns.</p>
  
  <p>When I try to add the GROUP BY statement, it is returning the following error: "Column 'PivotTable.1' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."</p>
  
  <pre><code>SELECT [Load ID],[1],[2],[3],[4]
  FROM TMS_Load_Stops
  PIVOT (
      MIN([Stop Zip])
      for [Sequence] IN ([1],[2],[3],[4])
      ) PivotTable
  ;
  </code></pre>
  
  <p>The original code yields the below results:
  <a href="https://i.stack.imgur.com/6YItR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6YItR.png" alt="enter image description here"></a></p>
  
  <p>And I would like the results to be as follows (values are random numbers for explanation purposes):
  <a href="https://i.stack.imgur.com/H13ma.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/H13ma.png" alt="enter image description here"></a></p>