[英]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:这个 stackoverflow 问题很好地总结了不同的方法:
Count number of records returned by group 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?如何将其转换为 JOOQ 查询?
This is how I would do a simple count query in JOOQ:这就是我在 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?是否可以用 JOOQ 语法表达“DISTINCT COUNT(*) OVER () AS TotalRecords”?
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:您缺少的方法是:
DSLContext.selectDistinct()
DSL.count()
WindowOverStep.over()
( WindowOverStep
being a supertype of the type returned by DSL.count()
) WindowOverStep.over()
( WindowOverStep
是DSL.count()
返回类型的超类型)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.