Mixing HAVING with CASE OR Analytic functions in MySQL (PartitionQualify(? <p>I have a SELECT query that returns some fields like this:</p> <pre><code>Date | Campaign_Name | Type | Count_People Oct | Cats | 1 | 500 Oct | Cats | 2 | 50 Oct | Dogs | 1 | 80 Oct | Dogs | 2 | 50 </code></pre> <p>The query uses aggregation and I only want to include results where when Type = 1 then ensure that the corresponding Count_People is greater than 99.</p> <p>Using the example table, I'd like to have two rows returned: Cats. Where Dogs is type 1 it's excluded because it's below 100, in this case where Dogs = 2 should be excluded also.</p> <p>Put another way, if type = 1 is less than 100 then remove all records of the corresponding campaign name.</p> <p>I started out trying this:</p> <pre><code>HAVING CASE WHEN type = 1 THEN COUNT(DISTINCT Count_People) &gt; 99 END </code></pre> <p>I used Teradata earlier int he year and remember working on a query that used an analytic function "Qualify PartitionBy". I suspect something along those lines is what I need? I need to base the exclusion on aggregation before the query is run?</p> <p>How would I do this in MySQL? Am I making sense?</p>