Last active
January 22, 2021 19:49
-
-
Save afanasevek/82fb6b0f09d7f74565b55adf9879d582 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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