JPQL select 查询,无法分组
JPQL select query, unable to Group By
下班后我似乎找不到实现此目的的方法,我有 2 个实体:
public class Price{
@Id
int id;
@Temporal(TemporalType.DATE)
private Date dtComu;
private String descCarb;
private Double price;
//bi-directional many-to-one association to Distributor
@ManyToOne
@JoinColumn(name="idImpiant")
private Distributor distributor;
和
public class Distributor{
@Id
private int idImpiant;
private String province;
//bi-directional many-to-one association to Price
@OneToMany(mappedBy="distributor")
private List<Price> prices;
我要做的事情看起来很简单,为每个分销商获取最新的价格。我想的是获得一份价格清单(每个价格都有一个分销商),按日期排序并按分销商分组。我的查询是:
SELECT e FROM Price e JOIN e.distributor d WHERE e.descCarb like '%Diesel%' group by e.distributor order by e.dtComu desc
我得到的错误是:
SELECT list is not in GROUP BY clause and contains nonaggregated
column 'price0_.id' which is not functionally dependent on columns in
GROUP BY clause
有没有其他我没想过的方法来实现这个目标?
您看到的错误是因为您不清楚每个经销商组需要哪个实体。这是使用 HQL 执行此操作的一种方法:
select e FROM Price e JOIN e.distributor d
where e.descCarb like '%Diesel%' and
e.dtComu = (select max(ee.dtComu) from Price ee where ee.distributor = e.distributor)
这使用相关子查询来检查匹配的 Price
实体是否是每个分销商的最新实体。
下班后我似乎找不到实现此目的的方法,我有 2 个实体:
public class Price{
@Id
int id;
@Temporal(TemporalType.DATE)
private Date dtComu;
private String descCarb;
private Double price;
//bi-directional many-to-one association to Distributor
@ManyToOne
@JoinColumn(name="idImpiant")
private Distributor distributor;
和
public class Distributor{
@Id
private int idImpiant;
private String province;
//bi-directional many-to-one association to Price
@OneToMany(mappedBy="distributor")
private List<Price> prices;
我要做的事情看起来很简单,为每个分销商获取最新的价格。我想的是获得一份价格清单(每个价格都有一个分销商),按日期排序并按分销商分组。我的查询是:
SELECT e FROM Price e JOIN e.distributor d WHERE e.descCarb like '%Diesel%' group by e.distributor order by e.dtComu desc
我得到的错误是:
SELECT list is not in GROUP BY clause and contains nonaggregated column 'price0_.id' which is not functionally dependent on columns in GROUP BY clause
有没有其他我没想过的方法来实现这个目标?
您看到的错误是因为您不清楚每个经销商组需要哪个实体。这是使用 HQL 执行此操作的一种方法:
select e FROM Price e JOIN e.distributor d
where e.descCarb like '%Diesel%' and
e.dtComu = (select max(ee.dtComu) from Price ee where ee.distributor = e.distributor)
这使用相关子查询来检查匹配的 Price
实体是否是每个分销商的最新实体。