Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mparaiso
committed
Jul 17, 2016
1 parent
61dd1cb
commit 4ce181e
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- +migrate Up | ||
|
||
-- +migrate StatementBegin | ||
CREATE VIEW IF NOT EXISTS threads_view AS | ||
SELECT t.ID, | ||
t.AuthorID, | ||
t.Title, | ||
t.Created, | ||
t.URL, | ||
t.Score, | ||
t.AuthorName, | ||
coalesce(COUNT(c.id), 0) AS CommentCount | ||
FROM ( | ||
SELECT threads.id AS ID, | ||
threads.author_id AS AuthorID, | ||
threads.title AS Title, | ||
threads.created AS Created, | ||
threads.url AS URL, | ||
u.username AS AuthorName, | ||
coalesce(SUM(thread_votes.score), 0) AS Score | ||
FROM threads | ||
JOIN | ||
users u ON u.id = threads.author_id | ||
LEFT JOIN | ||
thread_votes ON thread_votes.thread_id = threads.id | ||
GROUP BY threads.id | ||
) | ||
t | ||
LEFT JOIN | ||
comments c ON c.thread_id = t.ID | ||
GROUP BY t.id | ||
-- +migrate StatementEnd | ||
|
||
|
||
-- +migrate Down | ||
DROP VIEW IF EXISTS threads_view |