jilen
jilen

Reputation: 5763

Slick group by with other fields

select user_id, user_name,created_at, count(id) from message group by user_id order by created_at desc;

I want to write sql(mysql) like the this, how to do it with slick ( not plain sql).

Upvotes: 3

Views: 5177

Answers (1)

cvogt
cvogt

Reputation: 11270

Slick mimics Scala's collection API where possible. Think how you would do it in Scala collections.

Messages.groupBy(_.userId).map{ case (userId,group) =>
    (userId, group.userName.max, group.createdAt.max, group.size)
}.orderBy(_.createdAt)

(There currently is no .head to get a single element from a collection. Using .max as a fallback here.)

For obtaining the Messages object refer to the documentation, e.g. http://slick.typesafe.com/doc/2.0.0/code-generation.html

Upvotes: 4

Related Questions