Answer the question
In order to leave comments, you need to log in
How to compose a query using JPQL?
There is a SQL query:
SELECT user_id, sum_price FROM (
SELECT user_id, sum_price, RANK() OVER (order by sum_price DESC) AS r
FROM (SELECT user_id, SUM(price) sum_price FROM `order` GROUP BY user_id) t
) AS t2
WHERE r = 1;
CriteriaQuery<UserPrice> cq = cb.createQuery(UserPrice.class);
Root<Order> from = cq.from(Order.class);
Subquery<Order> sQuery = cq.subquery(Order.class);
Root<Order> sFrom = sQuery.from(Order.class);
Path<User> sUser = sFrom.get(ParameterName.USER);
Path<BigDecimal> sPrice = sFrom.get(ParameterName.PRICE);
Expression<BigDecimal> sSum = cb.sum(sPrice);
Predicate restriction = cb.equal(sQuery, NumberUtils.INTEGER_ONE);
cq.multiselect(sUser, sSum)
.orderBy(cb.desc(sSum))
.groupBy(sUser)
.where(restriction);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question