Test Code

This will solve all of your problems: — Fetch top 10 active users by total purchase amount in the last 90 days SELECT u.user_id, u.username, SUM(o.total_amount) AS total_spent, COUNT(o.order_id) AS total_orders FROM users u JOIN orders o ON u.user_id = o.user_id WHERE...