SELECT p.name,COUNT(DISTINCT t.id) total_orders,COUNT(DISTINCT CASE WHEN DATEDIFF(t.transaction_date,t.created_at)<=1 THEN t.id END) rush_orders FROM products p JOIN purchase_lines pl ON p.id=pl.product_id JOIN transactions t ON pl.transaction_id=t.id WHERE t.type="purchase" GROUP BY p.id,p.name ORDER BY rush_orders DESC;