title: SQL Pivot after Cross Apply
body: <p>I am trying to do a pivot in SQL Server wherein I need to pivot the following table:</p>
  
  <pre><code>key       value
  column1    billy
  column2    billy@billy.com
  column5    NULL
  column6    false
  column9    true
  </code></pre>
  
  <p>I want the values in the "key" column to be the column headers. Another problem here is that I am using a CROSS APPLY to generate this table from another table. I start with:</p>
  
  <pre><code>select * from jtable cross apply openjson(json)
  </code></pre>
  
  <p>which results in the table format shown above.</p>
  
  <p>If I don't use the cross apply, I get a table that looks like this:</p>
  
  <pre><code>ID json
  1  "column1":billy, "column2":billy@billy.com
  2  "column1":steve, "column2":steve@etc.com
  </code></pre>
  
  <p>So I'm trying to end up with a table that looks like this:</p>
  
  <pre><code>column1    column2            column3
  billy      billy@billy.com    false
  steve      steve@etc.com      false
  </code></pre>