1

In the example below, how can I orderBy "amount" (which is an alias created by me using a subquery) in jooq? I cannot understand how to do it reading the documentation.

    return context
            .select(
                    NAME,
                    sum(
                            COLUMN_ONE.minus(COLUMN_TWO)
                    ).as("amount")
            )
            .from(MYTABLE)
            .groupBy(MYTABLE.NAME)
            .fetchInto(MyClass.class);
jarlh
  • 42,561
  • 8
  • 45
  • 63
Mavek
  • 63
  • 1
  • 6

1 Answers1

1

Ok I found the solution on my own. In order to do it is necessary to create a field:

Field<BigDecimal> amount = field(
                sum(
                     COLUMN_ONE.minus(COLUMN_TWO)
                ).as("amount")
        );

And finally use it in your original query:

    return dslContext
            .select(
                    TRANSACTIONS.COIN_NAME,
                    amount
            )
            .from(MYTABLE)
            .groupBy(MYTABLE.NAME)
            .orderBy(coinAmount.desc())
            .fetchInto(MyClass.class);
Mavek
  • 63
  • 1
  • 6