title: Pivoting rows into columns dynamically in Oracle
body: <p>I have the following Oracle 10g table called _kv:</p>
  
  <pre><code>select * from _kv
  
  ID       K       V
  ----     -----   -----
    1      name    Bob
    1      age     30
    1      gender  male
    2      name    Susan
    2      status  married
  </code></pre>
  
  <p>I'd like to turn my keys into columns using plain SQL (not PL/SQL) so that the resulting table would look something like this:</p>
  
  <pre><code>ID       NAME    AGE    GENDER  STATUS
  ----     -----   -----  ------  --------
    1      Bob      30     male 
    2      Susan                   married
  </code></pre>
  
  <ul>
  <li>The query should have as many columns as unique <code>K</code>s exist in the table (there aren't that many)</li>
  <li>There's no way to know what columns may exist before running the query.</li>
  <li>I'm trying to avoid running an initial query to programatically build the final query.</li>
  <li>The blank cells may be nulls or empty strings, doesn't really matter.</li>
  <li>I'm using Oracle 10g, but an 11g solution would also be ok.</li>
  </ul>
  
  <p>There are a plenty of examples out there for when you know what your pivoted columns may be called, but I just can't find a generic pivoting solution for Oracle.</p>
  
  <p>Thanks!</p>