title: Case When vs. Count in MySQL
body: <p>I have problem with this query on MySQL.</p>
  
  <p>If tried this version :</p>
  
  <pre><code>mysql&gt;  SELECT
      COUNT(*),
      myCode,
      myState
  FROM
      `tbl_2016`
  WHERE
      `myCode` LIKE '%XI10-2-020012%'
  GROUP BY
      `myState`;
  </code></pre>
  
  <p>The output is :</p>
  
  <pre><code>+----------+---------------+--------------------------------+
  | COUNT(*) | myCode        | myState                        |
  +----------+---------------+--------------------------------+
  |        5 | XI10-2-020012 | NULL                           |
  |        4 | XI10-2-020012 | ACTIVE                         |
  |        1 | XI10-2-020012 | NOT ACTIVE                     |
  +----------+---------------+--------------------------------+
  2 rows in set
  </code></pre>
  
  <p>But if tried this other versione with <strong>CASE WHEN</strong> syntax :</p>
  
  <pre><code>mysql&gt; SELECT
      myCode,
      COUNT(
          CASE
          WHEN myState IN (
              'Active'
          ) THEN
              1
          ELSE
              0
          END
      ) AS `Active`,
      COUNT(
          CASE
          WHEN myState IN (
              'Not Active'
          ) THEN
              1
          ELSE
              0
          END
      ) AS `Not Active`
  FROM
      `tbl_2016`
  WHERE
          `myCode` LIKE '%XI10-2-020012%'
  GROUP BY
      `myState`;
  </code></pre>
  
  <p>The output is different than the first version :</p>
  
  <pre><code>+---------------+--------+------------+
  | myCode        | Active | Not Active |
  +---------------+--------+------------+
  | XI10-2-020012 |      4 |          4 |
  +---------------+--------+------------+
  1 row in set
  </code></pre>
  
  <p>How to do resolve this ?</p>
  
  <p>Can you help me?</p>
  
  <p>Thank you in advance, my code below.</p>