开发者

HQL : max/limit result based on property value

I have a domain, AccountTransaction

class AccountTransaction {
  Account account
  BigDecimal amount
  ...
}

I would like to get the maximum last 3 transactions of each account. Output expected:

id  | account | amoun开发者_如何学编程t
______________________
1   | a1      | 200
21  | a1      | 300
33  | a1      | 100
11  | a2      | 100
22  | a2      | 30
31  | a2      | 10
55  | a3      | 20

How should I write HQL/Criteria query for the same? Is it really supported?


I created some column date_created to work with the lastest transactions

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;

HQL:

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


I don't think you have to use HQL/Criteria to get this:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜