Join with date filter and aggregation
SQL
Medium
2 views
Problem Description
For each customer, show total paid amount in the last 30 days. Join Orders with Payments and group by customer.
Sample Test Case
Input:
Payments(order_id, amount, paid_at), Orders(order_id, customer_id)
Constraints
Assume PostgreSQL-style interval
Official Solution
SELECT o.customer_id, SUM(p.amount) AS paid_30d FROM Orders o JOIN Payments p ON p.order_id = o.order_id WHERE p.paid_at >= (CURRENT_DATE - INTERVAL '30 days') GROUP BY o.customer_id;
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!