From 4ce181e192c69ae17b64f9544f93425781be995f Mon Sep 17 00:00:00 2001 From: mparaiso Date: Sun, 17 Jul 2016 04:16:49 +0200 Subject: [PATCH] views --- migrations/development/sqlite3/003-views.sql | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 migrations/development/sqlite3/003-views.sql diff --git a/migrations/development/sqlite3/003-views.sql b/migrations/development/sqlite3/003-views.sql new file mode 100644 index 0000000..22dc8ce --- /dev/null +++ b/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 \ No newline at end of file