https://kotlinlang.org logo
#exposed
Title
# exposed
e

Evan R.

09/03/2019, 5:06 PM
Hey all, is the best way to return the value of a sum to create an aliased field and pass it into the query like so? Or can I just refer to the column in the
.get()
on the query via the
.sum()
method on the column? Here’s what I have right now:
Copy code
val sumAlias = TransactionsTable.amount.sum().alias("campaignTotal")
TransactionsTable.slice(sumAlias)
    .select { TransactionsTable.campaignID eq campaignID }
    .limit(1)
    .firstOrNull()
    ?.get(sumAlias)
s

spand

09/04/2019, 8:36 AM
Pretty sure you can just do:
Copy code
val sum = TransactionsTable.amount.sum()
TransactionsTable.slice(sumAlias)
    .select { TransactionsTable.campaignID eq campaignID }
    .limit(1)
    .firstOrNull()
    ?.get(sum)
1
Also, I think in most databases sum will always return one row so no need for
.limit(1)
and you can do
.first()
instead
e

Evan R.

09/04/2019, 5:15 PM
Gotcha. The reason I was doing
.firstOrNull()?.get(sumAlias)
was because
.firstOrNull()
will return a
ResultRow?
so I need to retrieve the actual sum value. Thanks!
2 Views