I would like to get the average of a column using Kotlin Exposed.
object MyTable: IntIdTable("MyTable") {
val score = integer("score")
val result = MyTable.slice(
MyTable.score.avg().alias("avg_points")
).first()
How do I get the result?
For normal columns I would use
result[MyTable.score]
But now it is an aggregate with an alias. I've tried
result["avg_points"]
But that fails. I don't see many public methods on ResultRow
.
Try this. First save the average to a variable
val avgColumn = MyTable.score.avg().alias("avg_points")
Then get the results as such
val result = MyTable.slice(
avgColumn
).selectAll().first()
val avg = result[avgColumn]