SQL window function II: percent growth

SQL

***VERY IMPORTANT***
TAKE NOTE OF THE PARENTHESIS
TAKE NOT OF 0.0 TO MAKE SURE COMPUTATION IS IN FLOAT

select date_trunc('month', created_at)::date as date
  , count(*) as count
  , round(((100.0 / (lag(count(*)) over (order by date_trunc('month', created_at)::date asc))) * count(*)) -100, 1) || '%' as percent_growth 
from posts 
group by date
order by date asc
Did you find this snippet useful?

Sign up for free to to add this to your code library

median
SQL

4
SQL RANK
SQL

4
EXISTS
SQL

3