title: Conditionally COUNT the Value of a Column in SQL and Account for NULL Value
body: <p>I have a SQL query where I try to perform some conditions inside a COUNT(). I am using PostgreSQL.  Here is my original query:</p>
  <pre><code>SELECT s.schedule_ID as &quot;schedule_ID&quot;, schedule_start, schedule_end, spots, 
  COUNT(CASE a.schedule_ID WHEN s.schedule_ID THEN TRUE ELSE NULL END) as &quot;takenSpots&quot; 
  FROM schedules s, appointments a 
  GROUP BY s.schedule_ID
  ORDER BY s.schedule_ID;
  </code></pre>
  <p>Basically, this counts the <code>schedule_ID</code> from appointments only when it matches the <code>schedule_ID</code> in schedules table. The issue is that this query does not account for the case when <code>a.schedule_ID</code> is null from appointments table. When it is null, it returns no records.I want to account for null values and if it is null, then return 0.</p>
  <p>After some research I found that something like this must work:</p>
  <pre><code>SELECT s.schedule_ID as &quot;schedule_ID&quot;, schedule_start, schedule_end, spots, 
  COUNT(CASE a.schedule_ID WHEN s.schedule_ID THEN TRUE WHEN NULL THEN '0' END) as &quot;takenSpots&quot; 
  FROM schedules s, appointments a 
  GROUP BY s.schedule_ID
  ORDER BY s.schedule_ID;
  </code></pre>
  <p>But no luck and still no record is returned here even when I check for NULL value. Can someone please help me out?</p>
  <p>I appreciate your help!</p>