You have the instruction in your code, but commented.
The query setLimit() function requires two parameters:
$limit: the number of records to return, and
$offset: the first record you want to return, zero-based.
This is the signature of the function:
public function setLimit($limit = 0, $offset = 0)
Please take notice that the parameter order is opposite ...
If you implement the code from my earlier code, then you only need to write a dynamic $having array to generate the correct HAVING clause. I must note that your current coding attempt is not stable/secure. You are receiving user-supplied data from $_POST and feeding it directly into your raw sql. This means that there are no validating or sanitising steps ...
I think the simplest way forward is to use a JOIN to relate your first query to your second query. An INNER JOIN (same as JOIN) will only generate rows in the result set where both "derived" queries have data.
I am assuming that %%P1=0%% is your Plotalot placeholder.
This was a late-night endeavour, so I cannot guarantee that it is fully refined, ...