This problem requires retrieving the three most recent orders for each customer from a database, handling cases where a customer has fewer than three orders. The solution involves joining customer and order information, ranking orders by date, and filtering for the most recent ones. The final result is sorted by customer name, ID, and order date.
Approach:
The solution uses a common approach involving these steps:
Join Customer and Order Tables: We first join the Customers
and Orders
tables using the customer_id
as the common key. This combines customer information (name, ID) with their order details (order ID, date, cost).
Rank Orders by Date: A window function, specifically ROW_NUMBER()
, is employed to assign a rank to each order within each customer's order history. The ranking is based on order_date
in descending order (most recent orders get lower ranks). The PARTITION BY customer_id
clause ensures that ranking is done separately for each customer.
Filter for Top 3 (or Fewer): We filter the results to include only those orders with a rank (rk
in the query) less than or equal to 3. This selects the three most recent orders for each customer. If a customer has fewer than three orders, all their orders will be included because their ranks will all be less than or equal to 3.
Sort the Results: The final result set is sorted first by customer_name
(ascending), then by customer_id
(ascending), and finally by order_date
(descending) to handle ties. This ensures consistent ordering as specified in the problem.
WITH
T AS (
SELECT
*,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY order_date DESC
) AS rk
FROM
Orders
JOIN Customers USING (customer_id)
)
SELECT name AS customer_name, customer_id, order_id, order_date
FROM T
WHERE rk <= 3
ORDER BY 1, 2, 4 DESC;
Time Complexity Analysis:
The time complexity is dominated by the ROW_NUMBER()
window function and the sorting operations. Window functions generally have a complexity that's linear in the size of the input data (O(N), where N is the number of rows in the joined table). The sorting step also has a complexity that is typically O(N log N) in the worst case, though database systems often employ optimized sorting algorithms that can perform better in practice. Therefore, the overall time complexity is approximately O(N log N), where N is the total number of orders.
Space Complexity Analysis:
The space complexity depends on the size of intermediate tables created during query execution. The CTE (Common Table Expression) T
stores the joined data with the added rank column. The size of T
is proportional to the size of the input data. The final output also has a size proportional to the input. Thus, the space complexity is approximately O(N).