Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select greatest element per group in querydsl

I have a sql table that looks roughly like this:

+-----+------+-------+
|  id | type | value |
+-----+------+-------+
|  1  |   X  |   20  |
|  2  |   X  |   30  |
|  3  |   Y  |  200  |
|  4  |   Y  |  500  |
|  5  |   Y  |  300  |
|  6  |   Z  |    5  |
+-----+------+-------+

For each type I want to retrieve the row with the maximum value. This is my expected result:

+-----+------+
|  id | type |
+-----+------+
|  2  |   X  | <-- had value = 30
|  4  |   Y  | <-- had value = 500
|  6  |   Z  | <-- had value = 5
+-----+------+

In SQL, this can be expressed as follows (given that for each type there aren't two entries with the same value, which I can rule out):

select t1.id, t1.type from T t1
inner join (
  select t2.type, max(t2.value) as max_value from T t2
  group by t2.type
) on t1.type = t2.type
  and t1.value = max_value

However I cannot find a way to express the same using QueryDSL (version 4). I tried this:

final JPQLQuery<Tuple> subquery = JPAExpressions
    .from(q2)
    .select(q2.type, q2.value.max())
    .groupBy(q2.type);
final JPQLQuery<Tuple> query = JPAExpressions
    .from(q1)
    .select(q1.id, q1.type)
    .from(q1)
    .innerJoin(subquery) // <-- not allowed
    .on(q1.type.eq(q2.type), q1.value.eq(q2.value.max()));

But innerJoin() (and other join methods) only take an expression as parameter, not another query. The same goes for from().

like image 896
Felk Avatar asked Sep 18 '25 23:09

Felk


1 Answers

The subquery can be put into the outer query's where clause in the form of an exists-expression:

final JPQLQuery<Tuple> subquery = JPAExpressions
    .from(q2)
    .select(q2.type, q2.value.max())
    .groupBy(q2.type);
final JPQLQuery<Tuple> query = JPAExpressions
    .from(q1)
    .select(q1.id, q1.type)
    .from(q1)
    .where(subquery
        .having(q1.type.eq(q2.type), q1.value.eq(q2.value.max()))
        .exists());

Note that this query might be quite inefficient.

like image 162
Felk Avatar answered Sep 20 '25 14:09

Felk