fix category events

for some reason the call to union() caused the result to contain to many
(all?) events. To fix this I converted parts of the SQL query to python.
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.
Merge pull request #927 from xi/feature-fix-category-events

fix category events