Leetcode Problem 2474. Customers With Strictly Increasing Purchases

2474. Customers With Strictly Increasing Purchases

Leetcode Solutions

Using Window Functions to Identify Strictly Increasing Purchases

  1. Use a Common Table Expression (CTE) to calculate the sum of prices for each customer per year.
  2. Within the CTE, use the LAG window function to get the previous year's total purchases for each customer.
  3. Compare the current year's total purchases with the previous year's total to ensure that the current year's total is strictly greater.
  4. Use a subquery to exclude customers who have any year where the total purchases did not strictly increase from the previous year.
  5. Select the distinct customer IDs that meet the criteria of strictly increasing yearly purchases.

erDiagram
    Orders {
        int order_id PK
        int customer_id
        date order_date
        int price
    }

Using Self-Join to Compare Yearly Totals

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...