Reputation: 398
Please suggest JOOQ DSL to query for result of boolean expression
In SQL I would write:
SELECT sum(apples.quantity) > sum(bananas.quantity)
FROM ...
Upvotes: 3
Views: 3948
Reputation: 220762
In order to get a jOOQ Condition
for your sum comparison, simply write:
Condition condition = sum(apples.quantity).gt(sum(bananas.quantity));
Now, Condition
types currently cannot be put into the SELECT
clause in jOOQ, but you can wrap them using DSL.field(Condition)
:
Field<Boolean> field = field(sum(apples.quantity).gt(sum(bananas.quantity)));
jOOQ will take care of deciding whether your SQL dialect allows for using predicates as column expressions (e.g. MySQL, PostgreSQL, SQLite), or whether this needs to be emulated using an equivalent CASE
expression (e.g. DB2, HANA, Oracle, SQL Server).
Which leads to:
Record1<Boolean> result =
DSL.using(configuration)
.select(field(sum(apples.quantity).gt(sum(bananas.quantity))))
.from(...)
.fetchOne();
Upvotes: 5