Skip to content

Commit

Permalink
views
Browse files Browse the repository at this point in the history
  • Loading branch information
mparaiso committed Jul 17, 2016
1 parent 61dd1cb commit 4ce181e
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions migrations/development/sqlite3/003-views.sql
@@ -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

0 comments on commit 4ce181e

Please sign in to comment.