I had a similar problem, but I had multiple columns I wanted to count. So I tried these 2 queries.
Count Distinct:
SELECT
to_char(action_date, 'YYYY-MM') as "Month",
count(*) as "Count",
count(distinct batch_id)
FROM transactions t
JOIN batches b on t.batch_id = b.id
GROUP BY to_char(action_date, 'YYYY-MM')
ORDER BY to_char(action_date, 'YYYY-MM');
Sub-Query:
WITH batch_counts AS (
SELECT to_char(action_date, 'YYYY-MM') as "Month",
COUNT(*) as t_count
FROM transactions t
JOIN batches b on t.batch_id = b.id
GROUP BY b.id
)
SELECT "Month",
SUM(t_count) as "Transactions",
COUNT(*) as "Batches"
FROM batch_counts
GROUP BY "Month"
ORDER BY "Month";
I ran both of these queries multiple on my test data of about 100k rows, the sub-query approach ran in ~90ms on average, but the count distinct approach took about ~200ms on average.