我尝试使用Jpa Specification来构建Group by查询。它看起来像这样:
Specification<AggregatedData> spec = (Specification<AggregatedData>) (root, criteriaQuery, cb) -> {
Predicate conditions = cb.and(
cb.between(root.get("sendDate"), beginDate, endDate),
cb.equal(root.get("taskNumber"), taskNum)
);
criteriaQuery.multiselect(
root.get("file"),
root.get("bok"),
root.get("taskNumber"),
root.get("documentType"),
cb.sum(root.get("to8Pages")),
cb.sum(root.get("from9To20Pages")),
.where(conditions)
.groupBy(
root.get("file"),
root.get("bok"),
root.get("taskNumber"),
root.get("documentType")
).orderBy(cb.desc(root.get("taskNumber")), cb.desc(root.get("bokNumber")));
return null;
};
字符串
当我使用它时,我得到了错误:
原因:org.postgresql.util.PSQLException:ERROR:列“aggregated0_.id”必须出现在GROUP BY子句中或在聚合函数中使用
这是否意味着我需要添加'id'到我的GROUP BY?如果我不想要它怎么办?我如何修复我的查询?
1条答案
按热度按时间mpbci0fu1#
可能是这样的
.orderBy(cb.desc(root.get("taskNumber")), cb.desc(root.get("bokNumber")))