[英]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 列表不在 GROUP BY 子句中,並且包含在功能上不依賴於 GROUP BY 子句中的列的非聚合列“price0_.id”
還有另一種我沒想到的方法嗎?
您看到的錯誤源於您不清楚每個分銷商組需要哪個實體。 這是使用 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
實體是否是每個分銷商的最新實體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.