title: SQL query to rebuild a table using its dynamic row data for column names
body: <p>My existing table is like this:</p>
  
  <pre>
  <b>ID   Grant   Subtotal       Month</b>
  1   Hatch   144.56         Nov-2012
  2   NIH     240.9          Nov-2012
  3   NSF     100.7          Nov-2012
  4   Other   276.67         Nov-2012
  5   NSF     234.53         Oct-2012
  6   DOE     214.35         Oct-2012
  7   Hatch   321.54         Oct-2012
  8   Other   312.35         Oct-2012
  9   NSF     156.89         Sep-2012
  10  Hatch   147.99         Sep-2012
  11  Other   245.67         Sep-2012
  12  State   148.66         Sep-2012
  
  </pre>
  
  <p>The table lists monthly expenses on each grant. As time goes by, the row number is increasing. Within the grant column, the names remain the same most of the time. When a new grant is available, it will appear here. This happens not very often.</p>
  
  <p>Now I want to plot the data for a chart so people can see how much is spent each month for each grant. For this purpose, I would like to reshape the table like this:</p>
  
  <pre>
  <b>ID   Month          DOE   Hatch     NIH     NSF     Other      State</b>
  1   Nov-2012             144.56   240.9    100.7    276.67  
  2   Oct-2012     214.35  321.54           234.53    312.35  
  3   Sep-2012             147.99           156.89    245.67     148.66
  
  </pre>
  
  <p>The desired new table keeps column Month (distinct) but will use all the data values of the Grant column (distinct) in the original table as column names (dynamic). These new columns take the corresponding data values of column Subtotal as their data.</p>
  
  <p>Could someone help build the query? Thanks a lot.</p>
  
  <p>BTW: I am using MySQL/PHP. I am a newbie. Any sample code is highly appreciate.</p>