1 Upvote

SQL window function II: percent growth

SQL
Querying

***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

By changjulian17 - Last Updated Dec. 23, 2021, 12:51 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
left join LATERAL
SQL
Querying

2
median
SQL
Querying

2
SQL creating a view
SQL
Querying

1
SQL window function
SQL
Querying

1
SQL RANK
SQL
Querying

1
EXISTS
SQL
Querying

1
Top Contributors
103
100