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.

sql fiddle

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

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -