Counting number of times a word has been said

I have been curious about checking trends of specific word usage on my forum, is there any query I could use to see how many times a substring has been mentioned by week?

Found a working one with the help of ChatGPT

WITH date_series AS (
  SELECT generate_series(
           DATE_TRUNC('week', MIN(created_at)),  -- Start of the first week
           DATE_TRUNC('week', MAX(created_at)),  -- Start of the last week
           '1 week'::interval                    -- Weekly interval
         ) AS week_start
  FROM posts
),
posts_with_substring AS (
  SELECT
    DATE_TRUNC('week', created_at) AS week_start,
    COUNT(*) AS total_posts,
    SUM((LENGTH(raw) - LENGTH(REPLACE(lower(raw), lower('your_substring'), ''))) / LENGTH('your_substring')) AS substring_count
  FROM
    posts
  WHERE
    raw ILIKE '%test%'
  GROUP BY
    DATE_TRUNC('week', created_at)
)
SELECT
  ds.week_start,
  COALESCE(pws.total_posts, 0) AS total_posts
FROM
  date_series ds
LEFT JOIN
  posts_with_substring pws ON ds.week_start = pws.week_start
ORDER BY
  ds.week_start

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.