title: SQL Server Pivot Table and Group
body: <p>I'm attempting to pivot some of my rows, currently my query is this:</p>
  
  <pre><code>  SELECT count(distinct users.userName) as TotalUsers, products.productNameCommon, employees.Division, products.productType
  FROM FlexLM_users users
  INNER JOIN Org.Employees employees ON employees.Username=users.userName
  INNER JOIN FlexLM_history history ON users.userID=history.userID
  INNER JOIN FlexLM_products products ON products.productID=history.productID
  where products.productType = 'Base'
  GROUP BY  products.productNameCommon, employees.Division, products.productType
  ORDER BY users DESC
  </code></pre>
  
  <p>And outputs this:</p>
  
  <pre><code>TotalUsers|   productNameCommon  |   Division            |  productType
  ------------------------------------------------------------------------
  16        |  Standard            |  Disease Control      |  base
  12        |  Basic               |  Epidemiology         |  base
  10        |  Standard            |  Prevention           |  base
  8         |  Advanced            |  Epidemiology         |  base
  6         |  Basic               |  Disease Control      |  base
  2         |  Advanced            |  Prevention           |  base
  </code></pre>
  
  <p>What I am looking to do is this:</p>
  
  <pre><code>Division       | Basic | Standard | Advanced | TotalUsers
  ----------------------------------------------------------
  Disease Control|   6   |    16    |    0     |  22
  Epidemiology   |   12  |    0     |    8     |  20
  Prevention     |   0   |   10     |    2     |  12   
  </code></pre>