Here you are building a rather complex query. A comment showing an example with the generated SQL and the general purpose of this construction would be welcome.