Leetcode Problem 1321. Restaurant Growth

1321. Restaurant Growth

Leetcode Solutions

Calculating-Day Moving Average Using Window Functions

  1. Use a Common Table Expression (CTE) to calculate the sum of amount for each visited_on date.
  2. Use another CTE to calculate the rolling sum and average of amount over a 7-day window using window functions with the ROWS BETWEEN 6 PRECEDING AND CURRENT ROW frame.
  3. Apply a ROW_NUMBER() window function to assign a sequential integer to each row, ordered by visited_on.
  4. Filter the results to exclude the first 6 days, which do not have a full 7-day window.
  5. Select the visited_on, rolling sum as amount, and the rounded rolling average as average_amount from the second CTE.
  6. Order the final result by visited_on in ascending order.

erDiagram
    Customer {
        int customer_id
        varchar name
        date visited_on
        int amount
    }

Calculating-Day Moving Average Using Correlated Subqueries

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...