gpt4 book ai didi

hibernate - HQL:基于属性值的最大/极限结果

转载 作者:行者123 更新时间:2023-12-02 14:09:29 25 4
gpt4 key购买 nike

我有一个域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")

关于hibernate - HQL:基于属性值的最大/极限结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5077989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com