简体   繁体   English

每个组中最大的n组

[英]greatest-n-per-group in hibernate

Is there any way in hibernate to get greatest record from a group. 休眠状态下是否有任何方法可以从小组中获得最佳记录。 In my table I have three columns(error_id, transaction_id, phone) I want to apply order by clause on transaction_id and then make group on basis of phone and then want to get top record from each group. 在我的表中,我有三列(error_id,transaction_id,phone),我想在transaction_id上应用order by子句,然后在phone的基础上进行分组,然后从每个组中获取最高记录。

Any suggestions for this type or query.... Thanx in Advance. 有关此类型或查询的任何建议。

I got the answer from my search. 我从搜索中得到了答案。

String msisdnQueryString = "SELECT DISTINCT table.phone FROM TABLE_NAME table";
Query msisdnQueryObject = session.createQuery(msisdnQueryString);
List msisdnList = msisdnQueryObject.list();

List<Long> transactionList = new ArrayList<Long>();
for(Object object : msisdnList){
    Long msisdn = Long.parseLong(object.toString());
    String transactionQueryString = "SELECT table.transactionId FROM TABLE_NAME table WHERE table.phone = "+msisdn+" ORDER BY table.transactionId DESC";
    Query transactionQueryObject = session.createQuery(transactionQueryString);
    transactionQueryObject.setFirstResult(0);
    transactionQueryObject.setMaxResults(1);
    List transactions =transactionQueryObject.list();
    if(transactions.size() > 0){
    transactionList.add((Long)transactions.get(0));
    }
}

It is a long process but it is working. 这是一个漫长的过程,但是正在奏效。 If any one know better way to get such records then please post your answers. 如果有人知道获取此类记录的更好方法,请发表您的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM