title: SQL Table Rows to Columns - Possible PIVOT?
body: <p>I have a table of information from Active Direcory, but unfortunately it's displayed like this;</p>
  
  <pre><code>+------+------------+----------------+-------------------+
  | dnId | propNameId |  propertyName  |   propertyValue   |
  +------+------------+----------------+-------------------+
  |    1 |         10 | objectsid      | S-1-5-32-548      |
  |    1 |         19 | _objectclass   | group             |
  |    1 |         80 | cn             | Account Operators |
  |    1 |         82 | samaccountname | Account Operators |
  |    1 |         85 | name           | Account Operators |
  |    2 |         10 | objectsid      | S-1-5-32-544      |
  |    2 |         19 | _objectclass   | group             |
  |    2 |         80 | cn             | Administrators    |
  |    2 |         82 | samaccountname | Administrators    |
  |    2 |         85 | name           | Administrators    |
  |    3 |         10 | objectsid      | S-1-5-32-551      |
  |    3 |         19 | _objectclass   | group             |
  |    3 |         80 | cn             | Backup Operators  |
  |    3 |         82 | samaccountname | Backup Operators  |
  |    3 |         85 | name           | Backup Operators  |
  +------+------------+----------------+-------------------+
  </code></pre>
  
  <p>Using a bunch of temp tables and joins, I can get it to display how I want, like this;</p>
  
  <pre><code>+------+--------------+--------------+-------------------+-------------------+-------------------+
  | dnId |  objectsid   | _objectclass |        cn         |  samaccountname   |       name        |
  +------+--------------+--------------+-------------------+-------------------+-------------------+
  |    1 | S-1-5-32-548 | group        | Account Operators | Account Operators | Account Operators |
  |    2 | S-1-5-32-544 | group        | Administrators    | Administrators    | Administrators    |
  |    3 | S-1-5-32-551 | group        | Backup Operators  | Backup Operators  | Backup Operators  |
  +------+--------------+--------------+-------------------+-------------------+-------------------+
  </code></pre>
  
  <p>But this feels really messy and long-winded.. I'm sure there must be a more elegant way of doing this, perhaps using <strong>PIVOT</strong>, which I've tried (and failed) to understand.</p>
  
  <p>Any thoughts/suggestions?</p>
  
  <p><strong>EDIT:</strong> Apologies, removed mysql tag (it was auto-suggested). I'm using MS SQL Server 2008 R2.</p>