title: SQL Pivot not working
body: <p>I am doing a pivot in SQL, but it's not working, can someone help what is wrong ? , below is my result set, desired output, and my query.</p>
  
  <p>-->> The #Final temp table is created using below query.</p>
  
  <pre><code>CREATE TABLE #Final
      (
         Name VARCHAR(100),
         Code VARCHAR(20),
         Amount MONEY
      )
  
  --&gt;&gt; Insert data, here i insert all data that i needed
  INSERT INTO [#Final]
  (
      [Name],
      [Code],
      [Amount]
  )
  SELECT something...
  -----------------------------------------------------------------
  </code></pre>
  
  <p>Here's the result set</p>
  
  <pre><code>Name        Code    **Amount**
  Batman      11111   4472.23
  Batman      11111   -1788.89
  Batman      22222   5166.98
  Batman      22222   -5166.98
  Batman      22222   5166.98
  Batman      33333   6880.43
  Batman      33333   -2293.47
  Robin       11111   8278.54
  Robin       11111   41392.66
  Robin       22222   15940.95
  Robin       22222   79704.71
  Robin       33333   16557.23
  Robin       33333   106136.1
  Robin       33333   4669.99
  Superman    11111   8944.39
  Wolverine   11111   32782.99
  Wolverine   11111   32782.99
  Wolverine   11111   49671.2
  Wolverine   11111   16557.06
  Wolverine   22222   127527.54
  Wolverine   33333   105074.74
  Wolverine   33333   -420.3
  -----------------------------------------------------------------
  
  --&gt;&gt; My pivot query
  SELECT [pvt].[Name], [1],[2],[3]
  FROM 
  (
      SELECT Name, Code, Amount FROM #Final
  ) src
  PIVOT
  (
      SUM(Amount) FOR Code IN ([1],[2],[3])
  ) pvt
  </code></pre>
  
  <p>The result of amount is all NULL</p>
  
  <pre><code>            11111   22222   33333
  Batman      NULL    NULL    NULL
  Robin       NULL    NULL    NULL
  Superman    NULL    NULL    NULL
  Wolverine   NULL    NULL    NULL
  -----------------------------------------------------------------
  </code></pre>
  
  <p>Expected result should be: (The sum of amount per Code per Name)</p>
  
  <pre><code>            11111       22222       33333
  Batman      2683.34     5166.98     4586.96
  Robin       49671.2     95645.66    127363.32
  Superman    8944.39            0            0
  Wolverine   131794.24   127527.54   104654.44
  </code></pre>
  
  <p>Any help would be much appreciated. Thanks in advance</p>