title: In PostgreSQL, conditionally count rows
body: <p><strong>Background</strong></p>
  <p>I'm a novice Postgres user running a local server on a Windows 10 machine. I've got a dataset <code>g</code> that looks like this:</p>
  <pre><code>+--+---------+----------------+
  |id|treatment|outcome_category|
  +--+---------+----------------+
  |a |1        |cardiovascular  |
  |a |0        |cardiovascular  |
  |b |0        |metabolic       |
  |b |0        |sensory         |
  |c |1        |NULL            |
  |c |0        |cardiovascular  |
  |c |1        |sensory         |
  |d |1        |NULL             |
  |d |0        |cns            |
  +--+---------+----------------+
  </code></pre>
  <p><strong>The Problem</strong></p>
  <p>I'd like to get a count of <code>outcome_category</code> by <code>outcome_category</code> for those <code>id</code> who are &quot;ever treated&quot; -- defined as &quot;<code>id</code>'s who have any row where <code>treatment</code>=1&quot;.</p>
  <p>Here's the desired result:</p>
  <pre><code>+----------------+---------+
  |outcome_category|  count  |
  +----------------+---------+
  |cardiovascular  |    3    |
  |sensory         |    1    |
  |cns             |    1    |
  +----------------+---------+
  </code></pre>
  <p>It would be fine if the result had to contain <code>metabolic</code>, like so:</p>
  <pre><code>+----------------+---------+
  |outcome_category|treatment|
  +----------------+---------+
  |cardiovascular  |    3    |
  |metabolic       |    0    |
  |sensory         |    1    |
  |cns             |    1    |
  +----------------+---------+
  </code></pre>
  <p>Obviously I don't need the rows to be in any particular order, though descending would be nice.</p>
  <p><strong>What I've tried</strong></p>
  <p>Here's a query I've written:</p>
  <pre><code>select treatment, outcome_category, sum(outcome_ct)
  from (select max(treatment) as treatment,
      outcome_category,
      count(outcome_category) as outcome_ct
      from g
      group by outcome_category) as sub
  group by outcome_category, sub.treatment;
  </code></pre>
  <p>But it's a mishmash result:</p>
  <pre><code>+---------+----------------+---+
  |treatment|outcome_category|sum|
  +---------+----------------+---+
  |1        |cardiovascular  |3  |
  |1        |sensory         |2  |
  |0        |metabolic       |1  |
  |1        |NULL            |0  |
  |0        |cns             |1  |
  +---------+----------------+---+
  </code></pre>
  <p>I'm trying to identify the &quot;ever exposed&quot; <code>id</code>'s using that first line in the subquery: <code>select max(treatment) as treatment</code>. But I'm not quite getting at the rest of it.</p>
  <p><strong>EDIT</strong></p>
  <p>I realized that the toy dataset <code>g</code> I originally gave you above doesn't correspond to the idiosyncrasies of my real dataset. I've updated <code>g</code> to reflect that many <code>id</code>'s who are &quot;ever treated&quot; won't have a non-null <code>outcome_category</code> next to a row with <code>treatment</code>=1.</p>