title: SQL server join tables and pivot
body: <p>I have two tables with data</p>
  
  <p><strong>TABLE 1</strong></p>
  
  <pre><code>    ---------------------------------------------------
      | SALEID | SOLDBY | SALEPRICE | MARGIN |   DATE   |
      |  1     |  'aa'  |  10,000   |   10   | 2013-1-1 |
      |  2     |  'bb'  |  25,000   |    5   | 2013-5-1 |
  </code></pre>
  
  <p><strong>TABLE 2</strong></p>
  
  <pre><code>    ---------------------------------------------------
      | SALEITEMID | SALEID | SALEPRICE | CATEGORY |
      |  1         |  1     |   6,000   | BOOKS    |
      |  2         |  1     |   4,000   | PRINTING |
      |  3         |  2     |   5,000   | BOOKS    |
      |  4         |  2     |   12,000  | PRINTING |
      |  5         |  2     |   8,000   | DVD      |
  </code></pre>
  
  <p>I need a query which will produce</p>
  
  <p><strong>TAB3</strong></p>
  
  <pre><code>    --------------------------------------------------------------------------------
      | SALEID | SOLDBY | SALEPRICE | MARGIN |   DATE   |  BOOKS  | PRINTING | DVD
      |  1     |  'aa'  |  10,000   |   10   | 2013-1-1 |  6,000  |  4,000   | 0
      |  2     |  'bb'  |  25,000   |    5   | 2013-5-1 |  5,000  | 12,000   | 8,000
  </code></pre>
  
  <p>I am pretty new to pivoting and not sure if pivot is way to go for this or not.</p>