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.
COMMENTS
RELATED SNIPPETS
2
2
2
2
2
1
1
1
1
1
Find Snippets by Language
Find Snippets by Use