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.