我有一个域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 /条件查询?真的受支持吗?

最佳答案

我创建了一些列date_created以处理最新的事务

SQL:

select 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")

07-24 18:22