Hello guys, how do i select max value of a specifi...
# exposed
a
Hello guys, how do i select max value of a specific column?
k
I’m still learning too, so I hope someone more knowledgeable will step in and show us the right way. Until then, I came up with 3 different ways to do what you ask, one using raw SQL, one using the DSL api, and one using DAO api. I included some print and timing statements, but I can clean that up if you want me to.
Copy code
// Raw SQL, not ideal
println(measureTimeMillis {
    val resultSet = this
        .connection
        .createStatement()
        .executeQuery("select max(date) from message")
    resultSet.next()
    val max = resultSet.getString(1)
    println("max: $max")
})

// Using the DSL, possibly a good compromise depending on your needs
println(measureTimeMillis {
val max = Messages
    .selectAll()
    .orderBy(Messages.date to false) // false means descending order
    .limit(1)
    .map{it[Messages.date]}
    println("max: $max")
})

// Clean but slow
println(measureTimeMillis {
    val max = Message.all().maxBy { it.date }?.date
    println("max: $max")
})