4 ms·
The first example "month over month" will never result in a large dataset if you just do count(distinct user_id) ... group by month order by month. There's only
by jmiserez 6y ago
The first example "month over month" will never result in a large dataset if you just do count(distinct user_id) ... group by month order by month. There's only max 12 values per year.
That query is much simpler, safer, faster, no self-join or windowing necessary, and you can properly handle missing months in your dataset in your higher-level language (which the provided solution doesn't do BTW).
And you're still getting the performance boost from the DB indexes for grouping and sorting.