SELECT p.name,COUNT(t.id) current_month_sales,LAG(COUNT(t.id)) OVER (PARTITION BY p.id ORDER BY DATE_FORMAT(t.transaction_date,"%Y-%m")) prev_month_sales FROM products p JOIN transaction_sell_lines tsl ON p.id=tsl.product_id JOIN transactions t ON tsl.transaction_id=t.id WHERE t.type="sell" GROUP BY p.id,p.name,DATE_FORMAT(t.transaction_date,"%Y-%m") ORDER BY (current_month_sales-prev_month_sales)/NULLIF(prev_month_sales,0) DESC;