title: SQL Unpivot multiple columns Data
body: <p>I am using SQL server 2008 and I am trying to unpivot the data. Here is the SQL code that I am using,</p>
  
  <pre><code>CREATE TABLE #pvt1 (VendorID int, Sa int, Emp1 int,Sa1 int,Emp2 int)
  GO
  INSERT INTO #pvt1  VALUES (1,2,4,3,9);
  
  GO
  
  --Unpivot the table.
  SELECT distinct VendorID,Orders,Orders1
  FROM 
     (SELECT VendorID, Emp1, Sa,Emp2,Sa1
     FROM #pvt1 ) p
  UNPIVOT
     (Orders FOR Emp IN 
        (Emp1,Emp2)
  )AS unpvt
  UNPIVOT
     (Orders1 FOR Emp1 IN 
        (Sa,Sa1)
  )AS unpvt1;
  GO
  </code></pre>
  
  <p>And Here is the result of the above code. </p>
  
  <pre><code>VendorID    Orders  Orders1
  1            4      2
  1            4      3
  1            9      2
  1            9      3
  </code></pre>
  
  <p>But I want my Output to be the way indicated below</p>
  
  <pre><code>VendorID    Orders  Orders1
  1           4       2
  1           9       3
  </code></pre>
  
  <p>The relationship from the above code is 2 is related to 4, and 3 is related to 9.</p>
  
  <p>How can I achieve this?</p>