Hibernate HQL:基于属性值的最大/限制结果

Hibernate HQL:基于属性值的最大/限制结果,hibernate,grails,hql,Hibernate,Grails,Hql,我有一个域名,AccountTransaction class AccountTransaction { Account account BigDecimal amount ... } 我想获得每个帐户的最多最后3笔交易。 预期产出: id | account | amount ______________________ 1 | a1 | 200 21 | a1 | 300 33 | a1 | 100 11 | a2 | 100

我有一个域名,AccountTransaction

class AccountTransaction {
  Account account
  BigDecimal amount
  ...
}
我想获得每个帐户的最多最后3笔交易。 预期产出:

id  | account | amount
______________________
1   | a1      | 200
21  | a1      | 300
33  | a1      | 100
11  | a2      | 100
22  | a2      | 30
31  | a2      | 10
55  | a3      | 20

我应该如何为相同的对象编写HQL/条件查询?它真的受支持吗?

我认为您不必使用HQL/标准来实现这一点:

AccountTransaction.list(max: 3, sort: 'amount', order: 'desc')

我创建了一些列date_,用于处理最近的事务

SQL:

HQL:


这将给出三个金额最高的条目。但他会列出一个名单,每个账户有三个最高金额的条目。万分感谢!它只是起作用了。只是使用了id而不是dateCreated,即at1.idselect id, account, amount from account_transaction as at1 where ( select count(*) from account_transaction as at2 where at1.account_id = at2.account_id and at1.date_created < at2.date_created ) <= 2;
AccountTransaction.executeQuery("select id, account, amount from AccountTransaction as at1
where (select count(*) from AccountTransaction as at2 
where at1.account = at2.account and at1.date_created > at2.date_created) <= 2")