我有以下 JPA 实体(省略了 getter、setter 和非相关字段):
@Entity
@Table(name = "transaction")
public class Transaction {
@Id
@GeneratedValue
@Column(name = "id")
private Long id;
@Column(name = "start_date", nullable = false)
private Date startDate;
}
我的目标是使用 JPQL 或标准 API 实现查询,它将返回每天的平均交易量和每天的最大交易量。
提供所需结果的本机 SQL 查询(MySQL 数据库)如下所示:
select max(cnt) from (
select date(start_date) start_date, count(t.id) cnt
from transaction t
group by date(t.start_date)
) t;
select avg(cnt) from (
select date(start_date) start_date, count(t.id) cnt
from transaction t
group by date(t.start_date)
) t;
不幸的是,不鼓励使用本机 SQL 查询,并且 JPQL 不允许在 where 子句中使用子查询。
先感谢您。
添加:
我从以下 Spring Data 查询开始:
@Query("select max(cnt) from ("
+ "select date(t.startDate) startDate, count(t.id) cnt "
+ "from Transaction t "
+ "group by date(t.startDate))")
但它显然没有用:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 22 [select max(cnt) from (select date(t.startDate) startDate, count(t.id) cnt from Transaction t group by date(startDate))]
我可以想象,使用排序和限制输出可以管理搜索最大值,但这对平均值没有帮助。