title: Display rows like a columns
body: <p>How to display rows like a column? I need turn this table for each meta_key value.</p>
  
  <p>I have following table:</p>
  
  <pre><code>CREATE TABLE wp_postmeta (
    meta_id int  NOT NULL,
    post_id int NOT NULL DEFAULT '0',
    meta_key varchar(255) DEFAULT NULL,
    meta_value varchar(255) 
  )
  INSERT INTO wp_postmeta ([meta_id], [post_id], [meta_key], [meta_value]) VALUES
  (14454, 1614, 'price_bin', '2'),
  (14453, 1614, 'price_current', '1'),
  (14452, 1614, 'post_tags', ''),
  (14451, 1614, 'price_reserve', '3'),
  (14450, 1614, 'price_shipping', '4'),
  (14449, 1614, 'condition', '1'),
  (14448, 1614, 'auction_type', '1'),
  (14447, 1614, 'listing_expiry_days', '1'),
  (14446, 1614, 'city', '2'),
  (14445, 1614, 'listing_price_due', '25'),
  (14444, 1614, 'showgooglemap', 'no'),
  (14443, 1614, 'topcategory', 'yes'),
  (14442, 1614, 'visitorcounter', 'no'),
  (14441, 1614, 'html', 'yes'),
  (14440, 1614, 'featured', 'no'),
  (14439, 1614, 'listing_price', '25'),
  (14724, 1658, '_edit_last', '1'),
  (14438, 1614, 'listing_expiry_date', ''),
  (14437, 1614, 'hits', '0')
  </code></pre>
  
  <p>I'm trying to use the following script:</p>
  
  <pre><code> DECLARE @idList varchar(500),
          @sqlToRun varchar(1000)
  
  SET @idList = STUFF((SELECT (meta_key)
              FROM wp_postmeta
              FOR XML PATH(''), TYPE
              ).value('.', 'NVARCHAR(MAX)') 
          ,1,1,'')
  
  SET @sqlToRun ='
  SELECT post_id,  ' + @idList + ',
  FROM (
      SELECT 
          post_id, meta_key, meta_value
      FROM wp_postmeta
          WHERE post_id = 1614
  ) as s
  PIVOT
  (
      Max(meta_value)
      FOR [meta_key] IN ('+ @idList +')
  )AS pvt'
  
  EXEC (@sqlToRun)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Yk6IZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yk6IZ.png" alt="enter image description here"></a></p>
  
  <p>But I getting NULL results. How to fix it?</p>
  
  <p>Output should like this:</p>
  
  <pre><code>post_id, price_bin, price_current, price_shipping
  1614        2          1            4
  </code></pre>