Vorrei poter ottenere un elenco di tutti i tag e il numero di nuovi argomenti/post creati per ciascuno in un determinato periodo (ad esempio, nell’ultimo mese). Qualcuno può aiutarmi con la query?
Potresti scorrere i file, suppongo, ma a mio avviso è più efficiente utilizzare il menu nella pagina di amministrazione dei plugin. Dovresti essere in grado di vedere molte tabelle che, una volta selezionate, mostrano i nomi dei campi e le informazioni sullo schema.
Non sono al mio desktop, ma il modello ha
# == 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
Nota che “topic_id” e “tag_id” suggeriscono che i nomi seguono il formato “nome tabella, trattino basso, nome campo”.
La tabella tag ha
# == 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
e ti lascio il compito di esaminare i campi delle tabelle topic.
Ma probabilmente stiamo anticipando le cose. Sei riuscito a eseguire una query molto semplice su una di queste tabelle, come un tipo di “Ciao Esploratore Dati”? Qual è?
Fantastico, grazie mille per i suggerimenti.
Sono riuscito a costruirlo. Potrebbe non essere perfetto, ma se può essere utile ad altri:
Nuovi argomenti per tag
-- [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