Fixup: do category union in database again

The issue solved in the previous commit wasn't caused by the SQL union
itself, but by filtering after performing the union step.

In this commit, we filter each subquery separately and *then* perform
union, which also solves the problem while keeping everything in one SQL
request.