Me encantaría tener una consulta de Explorador de Datos para todos los temas de una categoría y el número de votos de cada uno. Soy nuevo en las consultas SQL y no pude encontrar nada existente que se pareciera lo suficiente como para modificarlo.
Idealmente, se vería así:
tema
votos
respuestas
etiquetas
cerrado
tema de ejemplo 1
85
62
web, ios, android
FALSO
tema de ejemplo 2
79
45
web
VERDADERO
¿Alguien puede ayudarme con esto? Disculpas si este es el lugar equivocado. Parecía el tema existente más cercano.
-- [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í, gracias! ¡Esto es genial! Solo necesito un pequeño ajuste: ¿cómo hago para que muestre el nombre del tema en la exportación CSV (no solo el ID del tema)?
Creo que agregar t.title a SELECT debería incluir eso:
-- [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
También existe la opción de incluir un enlace funcional al tema en la exportación si te apetece, usando por ejemplo:
'https://meta.discourse.org/t/' || t.slug || '/' || t.id AS topic_url,
Está bien. Iría en esta parte aquí: (solo cámbialo a la dirección de tu propio sitio en lugar de 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