I would like to create a query that allows the equivalent SQL:
"SELECT id, foo FROM MyTable HAVING id = (SELECT MAX(id) FROM MyTable) GROUP BY id".
After diving into the source code, I can't seem to find a means to construct that type of query. Is this possible?
Try out:
MyTable.slice(MyTable.id, MyTable.name).
selectAll().
groupBy(MyTable.id).
having {
MyTable.id eq wrapAsExpression(MyTable.slice(MyTable.id.max()).selectAll())
}
Perfect, on the mark. func 'wrapAsExpression' is the missing piece. Thanks!
Just for those like me who find this page via Google:
SELECT id, foo FROM MyTable HAVING id = (SELECT MAX(id) FROM MyTable) GROUP BY id. is equivalent to
SELECT id, foo FROM MyTable WHERE id = (SELECT MAX(id) FROM MyTable).
The resulting Exposed code could be simplified to this:
val alias = MyTable.alias("t0")
val subquery = alias.slice(alias[MyTable.id].max()).selectAll()
MyTable.slice(MyTable.id, MyTable.name).select { MyTable.id.eq(wrapAsExpression(subquery)) }
Most helpful comment
Perfect, on the mark. func 'wrapAsExpression' is the missing piece. Thanks!