开发者

OpenJPA: how to construct a GROUP BY query with a group count

In JPQL I want to construct the equivalent query to this:

select *, count(*) as finger_count from page_delta_summary 
where delta_history_id = ? and change_type = ? group by fingerprint;

where fingerprint is a varchar field in table page_delta_summary. What I have is this:

select d, count(d) as finger_count from PageDeltaSummary d 
where d.deltaHistoryId = :deltaHistoryId and d.type = :pageDeltaType 
GROUP BY d.fingerprint"

where PageDeltaSummary is my entity. But I'm getting the following exception:

org.apache.openjpa.persistence.ArgumentExcepti开发者_运维技巧on: Your query on type "class com.su3analytics.sitedelta.model.PageDeltaSummary" with filter "select d, count(d) from PageDeltaSummary d where d.deltaHistoryId = :deltaHistoryId and d.type = :pageDeltaType GROUP BY d.fingerprint" is invalid. Your select and having clauses must only include aggregates or values that also appear in your grouping clause.

The query works fine if I remove either count(d) as finger_count or the GROUP BY.

Any suggestions?

Thanks


Your original SQL query doesn't make sense, therefore you can't convert in into JPQL.

I guess you want to get count of page_delta_summary rows satisfying where conditions for each fingerprint. If so, the SQL query looks like this:

select fingerprint, count(*) as finger_count from page_delta_summary  
where delta_history_id = ? and change_type = ? group by fingerprint;

and JPQL - like this:

select d.fingerprint, count(d) from PageDeltaSummary d    
where d.deltaHistoryId = :deltaHistoryId and d.type = :pageDeltaType    
GROUP BY d.fingerprint

These queries return pairs <fingerprint, finger_count> instead of full page_delta_summary rows (or entities).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜