ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Optimize Order Processing with Window Functions

SQL MySQL

Goal -- WPM

Ready
Exercise Algorithm Area
1SELECT
2o.order_id,
3o.customer_id,
4o.order_date,
5o.order_amount,
6SUM(o.order_amount) OVER (
7PARTITION BY o.customer_id
8ORDER BY o.order_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
9) AS running_total_amount
10FROM
11orders o
12WHERE
13o.customer_id IN (
14SELECT DISTINCT customer_id FROM orders
15);
16
17-- Helper function to get distinct customers (for demonstration, actual query uses subquery)
18-- This is conceptual, as window functions operate directly on the result set.
19-- The WHERE clause above ensures we only process customers with orders.
20
21-- Edge case: Customers with no orders will not appear in the result set
22-- due to the WHERE clause. If they were to be included, they would have
23-- a running_total_amount of NULL or 0, depending on aggregation.
24
25-- Example of a customer with multiple orders:
26-- Customer 101, Order 1 (2023-01-15, $100) -> Running Total: $100
27-- Customer 101, Order 2 (2023-02-20, $150) -> Running Total: $250
28-- Customer 101, Order 3 (2023-03-10, $50) -> Running Total: $300
29
30-- Example of a customer with a single order:
31-- Customer 102, Order 4 (2023-01-25, $200) -> Running Total: $200
Algorithm description viewbox

Optimize Order Processing with Window Functions

Algorithm description:

This scenario involves using MySQL window functions to compute a running total of order amounts for each customer, ordered by their order date. This is a common requirement for analyzing customer spending patterns over time, understanding sales trends, and generating financial reports. Window functions provide a powerful way to perform calculations across sets of table rows that are related to the current row.

Algorithm explanation:

The query uses the `SUM()` aggregate function as a window function. `OVER (PARTITION BY o.customer_id ORDER BY o.order_date ...)` defines the window. `PARTITION BY o.customer_id` divides the rows into partitions based on `customer_id`, meaning the sum restarts for each customer. `ORDER BY o.order_date` specifies the order within each partition, crucial for a running total. `ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW` defines the frame for the sum, including all rows from the start of the partition up to the current row. The `WHERE` clause filters out customers with no orders, implicitly handling that edge case by exclusion. The time complexity is typically O(N log N) due to sorting within partitions, or O(N) if data is already sorted. Space complexity is O(N) for storing intermediate results.

Pseudocode:

1. Select order details (ID, customer ID, date, amount).
2. Calculate the running total of `order_amount`.
3. Use `SUM(order_amount) OVER (...)`.
4. Partition the data by `customer_id`.
5. Order the data within each partition by `order_date`.
6. Define the window frame to include all preceding rows and the current row.
7. Alias the running total as `running_total_amount`.
8. Filter to include only customers who have placed at least one order.