Consider you have table T, with fields A and B.
With regular SQL, I could do this:
SELECT SUM(A * (100.0 - B) / 100.0) AS D FROM T;
And I would get exactly what I expect.
However, I'm not sure how to do it with CriteriaQuery.
I know how to do sum over 1 field, but not how to do sum over some math expression over multiple fields in a row.
The
CriteriaBuilder
interface provides the following arithmetic functions:sum(a, b)
diff(a, b)
prod(a, b)
quot(a, b)
where
a
b
parameters can be an expression and/or literal.As for the query, here is an exampe written in a human readable form:
You can also build the query as an one-liner:
I hope it clarifies your doubts.