特定の期間(例えば先月など)に作成されたトピックや投稿の数ごとに、すべてのタグのリストを取得できるようにしたいと考えています。
クエリについてご助力いただけますか?
ファイルを検索することも可能だとは思いますが、私見ではプラグイン管理ページのメニューを使う方が効率的です。選択するとフィールド名とスキーマ情報が表示される多数のテーブルが表示されるはずです。
私はデスクトップにいませんが、モデルには以下のような記述があります。
# == Schema Information
#
# Table name: topic_tags
#
# id :integer not null, primary key
# topic_id :integer not null
# tag_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_topic_tags_on_topic_id_and_tag_id (topic_id,tag_id) UNIQUE
「topic_id」と「tag_id」に注目してください。これは名前の形式が「テーブル名_アンダースコア_フィールド名」であることを示唆しています。
タグテーブルには以下のような記述があります。
# == Schema Information
#
# Table name: tags
#
# id :integer not null, primary key
# name :string not null
# topic_count :integer default(0), not null
# created_at :datetime not null
# updated_at :datetime not null
# pm_topic_count :integer default(0), not null
# target_tag_id :integer
#
# Indexes
#
# index_tags_on_lower_name (lower((name)::text)) UNIQUE
# index_tags_on_name (name) UNIQUE
トピックテーブルのフィールドについては、ご自身で確認していただくことにしましょう。
ただし、それは少し先走っているかもしれません。これらのテーブルのいずれかに対して、「Hello Data Explorer」のような非常に単純なクエリを実行することはできましたか?その結果は何ですか?
素晴らしい、ヒントをどうもありがとうございます。
ビルドに成功しました。完璧ではないかもしれませんが、もし誰かに役立つようであれば:
タグごとの新規トピック
-- [params]
-- int :months_ago = 0
WITH query_period as (
SELECT
date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' as period_start,
date_trunc('month', CURRENT_DATE) - INTERVAL ':months_ago months' + INTERVAL '1 month' - INTERVAL '1 second' as period_end
)
SELECT
tags.name,
count(1) as topic_count
FROM topics t
RIGHT JOIN topic_tags tt
ON t.id = tt.topic_id
RIGHT JOIN tags tags
ON tt.tag_id = tags.id
RIGHT JOIN query_period qp
ON t.created_at >= qp.period_start
AND t.created_at <= qp.period_end
WHERE t.user_id > 0
AND tt.topic_id IS NOT NULL
GROUP BY tags.name
ORDER BY topic_count DESC