问题
I have this raw query for PSQL that I would like to transform to JOOQ query:
SELECT DISTINCT date_trunc('day', ref_date) AS refdate
FROM income
WHERE probos_id = :probosId
The best version I was able to create is:
Result<Record1<Timestamp>> result = createQueryBuilder()
.selectDistinct(incomeTable.REF_DATE.cast(Date.class).cast(Timestamp.class).as("refdate"))
.from(incomeTable)
.where(incomeTable.PROBOS_ID.eq(probosId))
.fetch();
This is the generated query:
select distinct
cast(cast("public"."income"."ref_date" as date) as timestamp) as "refdate"
from "public"."income"
where "public"."income"."probos_id" = ?
I'd like to find a better way for set the precision (like date_trunc in PSQL) to get the necessary value without doulbe casting.
If it's possible I'd like to find a native solution without extending the DSL.
Thanks!
回答1:
Use DSL.trunc(Field, DatePart), i.e.
Result<Record1<Timestamp>> result = createQueryBuilder()
.selectDistinct(trunc(incomeTable.REF_DATE, DatePart.DAY).as("refdate"))
.from(incomeTable)
.where(incomeTable.PROBOS_ID.eq(probosId))
.fetch();
来源:https://stackoverflow.com/questions/51681406/jooq-timestamp-precision-on-query