postgresql - SQL query: Growth of users per month in percentage -
i have easy query calculates number of user registrations per month.
select to_char(created_at, 'yyyy-mm') month, count(user_id) users group month order month desc what have every month growth in percentages compare previous month.
select month, total, (total::float / lag(total) on (order month) - 1) * 100 growth ( select to_char(created_at, 'yyyy-mm') month, count(user_id) total users group month ) s order month; month | total | growth ---------+-------+------------------ 2013-01 | 2 | 2013-02 | 3 | 50 2013-03 | 5 | 66.6666666666667
Comments
Post a Comment