scala - Do Aggregation with Slick -
my database structure looks this:
id | content
i entry max id (not id).
i read answer how make aggregations slick, found there no first method in statement: query(coffees.map(_.price).max).first. how now?
what if need content of item max id?
to retrieve column, following. below example calculates max of 1 column, finds row maximum value, , returns value of column in row:
val coffees = tablequery[coffees] val mostexpensivecoffeequery = { maxprice <- coffees.map(_.price).max.result c <- maxprice match { case some(p) => coffees.filter(_.price === p).result case none => dbio.successful(seq()) } } yield c.headoption.map(_.name) val mostexpensivecoffee = db.run(mostexpensivecoffeequery) // future[option[string]] alternatively, return full coffees object:
val mostexpensivecoffeequery = { ... } yield c.headoption val mostexpensivecoffee = db.run(mostexpensivecoffeequery) // future[option[coffees]]
Comments
Post a Comment