简体   繁体   中英

JOOQ select count distinct from group

I am trying to count the number of records return by a group by select.

This stackoverflow question summarises the different approaches well:

Count number of records returned by group by

I would like to use the solution:

SELECT DISTINCT COUNT(*) OVER () AS TotalRecords
FROM table
GROUP BY column

How do I turn this into a JOOQ query?

This is how I would do a simple count query in JOOQ:

Record record = jooq.select( TABLE.COLUMN.count() ).from( TABLE).fetchOne();

return record.into( Long.class );

Is it possible to express the "DISTINCT COUNT(*) OVER () AS TotalRecords" in JOOQ syntax?

James

Write this:

// Assuming this static import
import static org.jooq.impl.DSL.*;

int totalRecords =
jooq.selectDistinct(count().over().as("TotalRecords"))
    .from(TABLE)
    .groupBy(TABLE.COLUMN)
    .fetchOneInto(int.class);

The methods you were missing were:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM