I can’t test this right now, but maybe you could look into it:
If this works make sure to add it conditionally as HAVING doesn’t reduce the selection result until after the full query before it is run, I believe.
Hope this either works or points you in a new direction to try!