Mi piacerebbe avere una query Esplora dati per tutti gli argomenti di una categoria e il numero di voti su ciascuno. Sono nuovo alle query SQL e non ho trovato nulla di esistente che sembrasse abbastanza simile da poterlo modificare.
Idealmente, dovrebbe apparire così:
argomento
voti
risposte
tag
chiuso
argomento di esempio 1
85
62
web, ios, android
FALSO
argomento di esempio 2
79
45
web
VERO
Qualcuno può aiutarmi con questo? Mi scuso se questo è il posto sbagliato. Sembrava l’argomento esistente più vicino.
-- [params]
-- int :category_id
WITH tag_names AS (
SELECT t.id topic_id,
string_agg(tags.name, ', ' ORDER BY tags.name) AS "tags"
FROM topics t
JOIN topic_tags tt ON tt.topic_id = t.id
JOIN tags ON tags.id = tt.tag_id
GROUP BY t.id
)
SELECT t.created_at::date,
dvtvc.topic_id,
votes_count,
t.posts_count,
tn.tags,
t.closed
FROM discourse_voting_topic_vote_count dvtvc
JOIN topics t ON t.id = dvtvc.topic_id
LEFT JOIN tag_names tn ON dvtvc.topic_id = tn.topic_id
WHERE t.category_id = :category_id
AND votes_count > 0
ORDER BY t.created_at DESC
Sì, grazie! È fantastico! Ho solo bisogno di una piccola modifica: come faccio a visualizzare il nome dell’argomento nell’esportazione CSV (non solo l’ID dell’argomento)?
Penso che l’aggiunta di t.title a SELECT dovrebbe includerlo:
-- [params]
-- int :category_id
WITH tag_names AS (
SELECT t.id topic_id,
string_agg(tags.name, ', ' ORDER BY tags.name) AS "tags"
FROM topics t
JOIN topic_tags tt ON tt.topic_id = t.id
JOIN tags ON tags.id = tt.tag_id
GROUP BY t.id
)
SELECT t.created_at::date,
dvtvc.topic_id,
t.title,
votes_count,
t.posts_count,
tn.tags,
t.closed
FROM discourse_voting_topic_vote_count dvtvc
JOIN topics t ON t.id = dvtvc.topic_id
LEFT JOIN tag_names tn ON dvtvc.topic_id = tn.topic_id
WHERE t.category_id = :category_id
AND votes_count > 0
ORDER BY t.created_at DESC
C’è anche un’opzione per includere un link funzionante all’argomento nell’esportazione, usando ad esempio:
'https://meta.discourse.org/t/' || t.slug || '/' || t.id AS topic_url,
Va bene. Andrebbe inserito qui: (cambia solo con l’indirizzo del tuo sito invece di Meta)
-- [params]
-- int :category_id
WITH tag_names AS (
SELECT t.id topic_id,
string_agg(tags.name, ', ' ORDER BY tags.name) AS "tags"
FROM topics t
JOIN topic_tags tt ON tt.topic_id = t.id
JOIN tags ON tags.id = tt.tag_id
GROUP BY t.id
)
SELECT t.created_at::date,
dvtvc.topic_id,
t.title,
'https://meta.discourse.org/t/' || t.slug || '/' ||
t.id AS topic_url,
votes_count,
t.posts_count,
tn.tags,
t.closed
FROM discourse_voting_topic_vote_count dvtvc
JOIN topics t ON t.id = dvtvc.topic_id
LEFT JOIN tag_names tn ON dvtvc.topic_id = tn.topic_id
WHERE t.category_id = :category_id
AND votes_count > 0
ORDER BY t.created_at DESC