Penso che dovresti essere in grado di riutilizzare alcuni degli esempi in (Superseded) What cool data explorer queries have you come up with? per avvicinarti (e il tag sql-query ha molti più argomenti)
Se dai un’occhiata a quelli e vedi se qualcosa si avvicina a ciò che stai cercando, possiamo aiutarti a perfezionare qualsiasi cosa se ne hai bisogno. ![]()
Sono sicuro che ci sono modi più eleganti per farlo, ma qualcosa forse come questo?
-- [params]
-- int :cat_id = 6
-- date :date_from = 01/03/2022
-- date :date_to = 01/04/2022
SELECT t.category_id, count(t.category_id)
FROM topics t
WHERE t.category_id = :cat_id
AND t.created_at::date BETWEEN :date_from::date AND :date_to::date
AND t.deleted_at is null
GROUP BY t.category_id
Aspetta. Quello non sarebbe mese per mese. Fammi riprovare…
Forse farò meglio con questo.
Che ne dici di qualcosa del genere?
-- [params]
-- int :cat_id = 5
-- int :months = 12
SELECT
date_part('year', created_at) AS year,
date_part('month', created_at) AS month,
COUNT(category_id) AS "new_topics_month"
FROM topics t
WHERE t.category_id = :cat_id
AND t.deleted_at is NULL
GROUP BY date_part('year', created_at), date_part('month', created_at)
ORDER BY date_part('year', created_at) DESC, date_part('month', created_at) DESC
LIMIT :months