title: Case statement in Having clause in mysql
body: <p>I have a stored procedure where I have to check compare some result on bases of operator. operator could be either '<code>&gt;</code>' or '<code>&lt;</code>'
  My Query is:</p>
  
  <pre><code>  SELECT COUNT(LastWeekId) , COUNT(PreviousToLastWeekId)
          FROM 
          (
          SELECT `transaction_id`  AS LastWeekId, NULL AS PreviousToLastWeekId
          FROM `kwt_transaction`
          WHERE `statusid` =1000            
          AND (createdon BETWEEN CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 7 DAY)," 00:00:00") AND CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 1 DAY)," 23:59:59"))
      UNION
  
          SELECT  `transaction_id` AS LastWeekId, NULL AS PreviousToLastWeekId
          FROM `kwt_transactions_p2p`
          WHERE `statusid` =1000            
          AND (createdon BETWEEN CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 7 DAY)," 00:00:00") AND CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 1 DAY)," 23:59:59"))
          UNION                   
          SELECT NULL AS LastWeekId, `transaction_id` AS PreviousToLastWeekId
          FROM `kwt_transaction`
          WHERE `statusid` =1000            
          AND (createdon BETWEEN CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 14 DAY)," 00:00:00") AND CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 8 DAY)," 23:59:59"))
      UNION   
          SELECT NULL AS LastWeekId, `transaction_id` AS PreviousToLastWeekId
          FROM `kwt_transactions_p2p`
          WHERE `statusid` =1000            
          AND (createdon BETWEEN CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 14 DAY)," 00:00:00") AND CONCAT(DATE_SUB(DATE(NOW()), INTERVAL 8 DAY)," 23:59:59"))
      ) ttable
      HAVING COUNT(LastWeekId) &gt; COUNT(PreviousToLastWeekId)
  </code></pre>
  
  <p>So I wants to check:</p>
  
  <pre><code>HAVING Case _operaotr
         when '&gt;'    
         COUNT(LastWeekId) &gt; COUNT(PreviousToLastWeekId);
  when '&lt;'
      COUNT(LastWeekId) &lt; COUNT(PreviousToLastWeekId);
  End Case;
  </code></pre>
  
  <p>Is this possible in mysql</p>