From 13094992c9d51a5c517c7f6b468897cd671557ca Mon Sep 17 00:00:00 2001 From: Michelle Saquetim Date: Wed, 20 May 2020 13:13:04 +0100 Subject: [PATCH] Add query number of replies by category --- lib/queries.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/queries.rb b/lib/queries.rb index df08fb4..567dac8 100644 --- a/lib/queries.rb +++ b/lib/queries.rb @@ -509,6 +509,34 @@ class Queries ORDER BY year DESC, qt DESC SQL + + queries["number_of_replies_by_category"]["sql"] = <<~SQL + -- [params] + -- boolean :enable_null_category = false + + WITH post AS (SELECT + id AS post_id, + topic_id, + EXTRACT(YEAR FROM created_at) AS year + FROM posts + WHERE post_type = 1 + AND deleted_at ISNULL + AND post_number != 1) + + SELECT + p.year, + t.category_id AS id, + c.name category, + COUNT(p.post_id) AS qt + FROM post p + INNER JOIN topics t ON t.id = p.topic_id + LEFT JOIN categories c ON c.id = t.category_id + WHERE t.deleted_at ISNULL + AND (:enable_null_category = true OR t.category_id NOTNULL) + GROUP BY t.category_id, c.name, p.year + ORDER BY p.year DESC, qt DESC + SQL + # convert query ids from "mostcommonlikers" to "-1", "mostmessages" to "-2" etc. queries.transform_keys!.with_index { |key, idx| "-#{idx + 1}" } queries