rewrite query to filter user leaves