Skip to content

Instantly share code, notes, and snippets.

@afanasevek
Last active January 22, 2021 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afanasevek/82fb6b0f09d7f74565b55adf9879d582 to your computer and use it in GitHub Desktop.
Save afanasevek/82fb6b0f09d7f74565b55adf9879d582 to your computer and use it in GitHub Desktop.
SELECT * FROM blog.POSTS p
LEFT JOIN BLOG.POST_VOTES pv
ON p.POST_ID = pv.POST_ID
JOIN (SELECT p2.POST_ID as id, COUNT(pv2.VOTE_ID) "count" FROM BLOG.POSTS p2 LEFT
JOIN BLOG.POST_VOTES pv2 ON p2.POST_ID = pv2.POST_ID GROUP BY p2.POST_ID) r
ON p.POST_ID = r.id ORDER BY r."count";
val x = SubQuery.syntax("x").include(v, p, u)
DB readOnly { implicit session =>
withSQL {
select.all(p,u,v)
.from(Post as p)
.leftJoin(Vote as v)
.on(p.postId, v.postId)
.join(User as u)
.on(p.userId, u.userId)
.join{
select(p.result.*,count(v.voteId))
.from(Post as p)
.leftJoin(Vote as v)
.on(p.postId, v.postId)
.join(User as u)
.on(p.userId, u.userId)
.groupBy(p.postId)
.as(x)
}.on(x(p).postId, p.postId)
}
.one(Post(p.resultName, u.resultName))
.toMany(
rs => Vote.opt(v)(rs),
).map{
(p:Post,v:Seq[Vote])=> {
p.copy(votes = v)
}
}.list.apply
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment