title: How do I create SQL PIVOT column names by aggregating row values?
body: <p>I am guessing that this needs to be done with dynamic sql, but I would prefer a simple query if it is possible.  Searches so far have shown me some examples, but I am totally new to pivot tables and I'm probably not seeing the forest for the trees.  Thanks for any help.</p>
  
  <p>I have a table that looks like this where the 'begin' and 'end' columns can contain basically arbitrary numbers (it's graph data and those are the node numbers).</p>
  
  <pre><code>graph_id    begin   end distance
  597857  145 147 2.59
  601978  107 147 2.86
  596794  107 147 2.92
  601978  145 147 2.98
  601635  145 147 3.06
  601635  107 257 3.06
  599472  147 151 3.21
  601635  107 147 3.29
  601978  107 257 3.31
  596794  107 257 3.32
  </code></pre>
  
  <p>I want to create a table that looks like this, basically where each row is a graph and each column is an edge:</p>
  
  <pre><code>graph_id    145:147 107:147 107:157 147:151 107:257
  597857  2.59    NULL    NULL    NULL    NULL
  596794  NULL    2.92    NULL    NULL    3.32
  601978  2.98    2.86    NULL    NULL    3.31
  601635  3.06    3.29    NULL    NULL    3.06
  599472  NULL    NULL    NULL    3.21    NULL
  </code></pre>
  
  <p>Any help is appreciated; I am not sure how to construct the proper aggregations and my brain refuses to wrap itself around pivot tables.</p>
  
  <p>thanks!</p>