As far as I can tell, Discourse doesn’t explicitly keep track of mentions, but it does keep track of links that are added to topics. A group mention in a post will be recorded as the value of the url in the topic_links table with the following form: "/groups/<group_name>". The following query should give you close to what you are looking for. You’ll need to supply values for the start_date and end_date parameters. Those dates should be in the form yyyy-mm-dd. For example, to get data for the past year, enter 2020-01-01 as the start_date and 2020-12-31 as the end_date:
--[params]
-- date :start_date
-- date :end_date
WITH group_mentions AS (
SELECT
split_part(url, '/', 3) AS group_name
FROM topic_links tl
JOIN topics t ON t.id = tl.topic_id
WHERE internal = true
AND url LIKE '/groups/%'
AND t.deleted_at IS NULL
AND t.archetype = 'regular'
AND tl.created_at::date BETWEEN :start_date AND :end_date
)
SELECT
gm.group_name,
COUNT(gm.group_name) AS mention_count
FROM group_mentions gm
JOIN groups g ON g.name = gm.group_name
WHERE g.created_at::date <= :start_date
GROUP BY gm.group_name
ORDER BY mention_count DESC
The query is only returning mentions that have been added to regular posts (it’s including mentions that have been added to whisper posts though.) If you would like mentions that have been added to personal messages to be included in the results, remove the AND t.archetype = 'regular' line from the first query.
Let me know if this isn’t returning the kind of data you are looking for. I’ll try to get to your other questions tomorrow if no one answers them before then.