QueryDSL计数汇总(分组)行

时间:2020-03-20 09:35:21

标签: java querydsl

我有一个querydsl以group by和sum来获取数据,类似这样

var query = queryFactory
                .select(Projections.constructor(DistributionData.class, entity.date, entity.type,
                        entity.whiteCase.sum(), entity.blackCase().sum()))
                .from(entity).where(where).groupBy(entity.date, entity.type);
var listDataSum = query.limit(size).offset(page).orderBy(entity.date.asc(), entity.type.asc()).fetch();

这将产生如下查询:

  select date, type, sum(white_case), sum(black_case)
    from table
group by date, type

但是现在我需要处理分页,以获取querydsl返回的总聚合行。在本机SQL中,意味着创建一些查询,例如

select count(*) from 
(
   // generated query above
)

如何创建此计数查询? 使用query.fetchCount()无效,因为它将生成以下查询:

select count(distinct distributionData.date, distributionData.type) ....

因此,fetchCount()不会包装查询,而是使用count来包装字段。

谢谢

0 个答案:

没有答案