I have two sums to calculate in JPA, from two different entities:
SELECT SUM(da.debtorBalance) FROM DebtorAccount da WHERE <conditions for DebtorAccounts>
and
SELECT SUM(mda.debtorBalance) FROM MasterDebtorAccount mda WHERE <conditions for MasterDebtorAccounts>
What I need is sum of this sums. Is it possible by JPAQL? Or I need to run two separate queries and add this in the application?
You can’t in strict JPQL.
In SQL, it would be something like
SELECT SUM(a) FROM (SELECT da.debtorBalance a from DebtorAccount UNION SELECT mda.debtorBalance a FROM MasterDebtorAccount)
JPA does not support UNION, although several implementations do.
And no, you can’t do this without union because relational algebra means that you begin with DebtorAccount * MasterDebtorBalance and after that you refine it through conditions / aplying functions.