title: SQL query - multiple COUNT on same column with different values from nested SELECT query
body: <p>I'm struggling with the correct SQL syntax to return counts of a particular value in one column.</p>
  
  <p>This query works (probably incorrect syntax, but SQL Server 2008 seems happy)</p>
  
  <pre><code>SELECT StudentID, count(UnApproved)as Late, count(Unapproved) as Absent from results
  WHERE unapproved=1 and StudentID in 
    (
      SELECT studentid  FROM [Results] 
      WHERE StudentYearLevel='10' and Date &gt; 20130101) group by StudentID
    )
  </code></pre>
  
  <p>Of course, both <code>Late</code> and <code>Absent</code> columns return the same values because of where the 'where' is.</p>
  
  <p>So what this is doing is (from the right) determining the IDs of students who are members of "Year 10".</p>
  
  <p>Then, for each student ID returned, I need it to return the count of unapproved absences recorded where the type of unapproved absence is 1 and in the next column, also return the count of unapproved absences where the type is 2.</p>
  
  <p>If I try to submit the query like so:-</p>
  
  <pre><code>SELECT StudentID, count(UnApproved)as Late where unapproved=2, count(Unapproved) as Absent from results 
  where unapproved=1 and StudentID in 
    (
      SELECT studentid  FROM [Results] where StudentYearLevel='10' and Date &gt; 20130101
    )
  group by StudentID
  </code></pre>
  
  <p>SQL Server cracks it, and underlines almost the entire query in red.</p>
  
  <p>I need to end up these three columns:-</p>
  
  <p><code>StudentID | Late | Absent</code></p>
  
  <p>And the three columns having student IDs with the appropriate counts.</p>
  
  <p>I can do most basic select queries, but when it comes to nested queries, unions, joins, inners I'm out of my depth. Any help would be most appreciated. By no means am I sure my (working) query is in any way correctly structured, 'coz I'm a hack at this.</p>