I have a pretty simple query to do - a table has a date field and I need to select count of all rows, grouped by month including gaps.
I have the following query that works for days, but it does not work for month
SELECT f.date::date as period, count(e.id) as count
FROM generate_series((now() - interval '1 year')::date, now()::date, '1 month'::interval) AS f(date)
LEFT JOIN my_table AS e ON e.created_at::date = f.date::date
GROUP BY f.date
ORDER BY f.date DESC;
For some reason this query returns zero's for all rows.
This query works, but it does not fill in gaps
SELECT date_trunc('month', created_at) AS txn_month, count(*) as count
FROM my_table
GROUP BY txn_month
What is the right way to do so?