kotlin 返回行值和行计数的总和

46qrfjad  于 2023-06-06  发布在  Kotlin
关注(0)|答案(1)|浏览(181)

我有一个查询,返回表列中值的总和。我现在还想返回行数。
下面是我当前查询的样子:

db.select(DSL.coalesce(DSL.sum(AMOUNT), 0).cast(Long::class.java))
                .from(this.innerJoin(ACCOUNT).on(ACCOUNT_ID.eq(ACCOUNT.ID)))
                .where(ACCOUNT.ACCOUNT_ID.eq(accId))
                .awaitSingle()
                .value1()
r8uurelv

r8uurelv1#

您可以只add another aggregate function to the same jOOQ query,例如:

val (sum, count) = 
db.select(
     coalesce(sum(AMOUNT), 0).coerce(Long::class.java),
     count())
  .from(this.innerJoin(ACCOUNT).on(ACCOUNT_ID.eq(ACCOUNT.ID)))
  .where(ACCOUNT.ACCOUNT_ID.eq(accId))
  .awaitSingle()

这个答案假设通常的import org.jooq.impl.DSL.*

相关问题