title: SQL select invalid because it is not contained in aggregate function
body: <p>Here's the problem, I want to display the month, count and avg of one column in a table, but I keep getting an error when I try and group it by the month.</p>
  
  <p>This is the code:</p>
  
  <pre><code>SELECT MONTH(ContractDate) AS Q, 
         DATENAME(month, ContractDate) AS M, 
         COUNT(ContractDate) AS C, SUM(ContractPrice) AS S 
         FROM dashboard
         WHERE YEAR(ContractDate) = $year
         AND ContractDate IS NOT NULL
         AND ContractPrice IS NOT NULL
     GROUP BY MONTH(ContractDate)
  </code></pre>
  
  <p>But this results in the error:</p>
  
  <pre><code>[Microsoft][SQL Server Native Client 10.0][SQL Server]
  Column 'dashboard.ContractDate' is invalid in the select 
  list because it is not contained in either an aggregate 
  function or the GROUP BY clause.
  </code></pre>
  
  <p>But if I removed the <code>MONTH()</code> from the group by... it works fine.. But I need to have them grouped by month otherwise I get multiple of the same month not counted as one.</p>
  
  <p>Sorry again, I did search and there is HEAPS of answers, but like I said I'm noob and they didn't really help me because I don't understand why this happens.</p>