This question was recently asked on the jOOQ user group:
jOOQ's (historic) understanding of UNIONs is documented here:
- http://www.jooq.org/doc/3.2/manual/sql-building/sql-statements/select-statement/union-clause/#N10EC1
This diverges from the SQL standard, where ORDER BY is applied to a whole set of subqueries. A fix for this is on the roadmap: https://github.com/jOOQ/jOOQ/issues/1658
In the mean time, you can work around this limitation by using a derived table:
ctx.select()
.from(
select(A)
.from(T1)
.union(
select(B)
.from(T2))
)
.orderBy(...)
Note that you may have to specify an alias for your derived table, in order to access its columns.