I have a data set that records revenue by member, by month, by product. I can sum the revenue by member like so:
select sum(revenue), member, month from member_revenue group by member, month
select member, month, sum(revenue) as this_month_revenue, lag(sum(revenue)) over (partition by member order by month) as last_month_revenue from member_revenue group by member, month;
Note: This assumes that all months have revenue.
LAG() gets the revenue from the preceding row, even if it is not the preceding month.