Ticket/master/pdb 581 event subqueries

This replaces #900 with tests and a bit of a cleanup.