title: SQL Pivot String Data
body: <p>I have a tables in SQL Server: Product</p>
  
  <p><strong>Product Table</strong>:</p>
  
  <pre><code>ImageID  ProductID  
  -------  ---------- 
  1           P1
  1           P2             
  1           P3             
  2           S1
  2           S2
  2           S3
  3           M1
  </code></pre>
  
  <p>This is the output that I need:</p>
  
  <pre><code>ImageID  Product1ID     Product2ID      Product3ID
  ----------- ---------- ----------    ----------
  1           P1             P2           P3
  2           S1             S2           S3
  3           M1             null         null
  </code></pre>
  
  <p>An ImageID can have maximum 3 ProductID
  It is not necessary that all ImageID will have 3 products [eg. ImageID=3]</p>
  
  <pre><code>SELECT ImageID, [Product1ID], [Product2ID], [Product3ID]
  FROM    
  (  
          SELECT ImageID,  ProductID
          FROM ProductTable
  ) AS P
  PIVOT 
  (  
      max( ImageID) 
      FOR ProductID IN ([Product1ID], [Product2ID], [Product3ID])
  ) AS  PVT
  </code></pre>