SELECT QUARTER(t.transaction_date) quarter,COUNT(t.id) transactions,SUM(t.final_total) revenue,LAG(SUM(t.final_total)) OVER (ORDER BY QUARTER(t.transaction_date)) prev_quarter_revenue FROM transactions t WHERE t.type="sell" GROUP BY QUARTER(t.transaction_date) ORDER BY quarter;