Leetcode Problem 2474. Customers With Strictly Increasing Purchases
2474. Customers With Strictly Increasing Purchases
AI Mock Interview
Leetcode Solutions
Using Window Functions to Identify Strictly Increasing Purchases
Solution Idea
Query Steps
Code Implementation
Use a Common Table Expression (CTE) to calculate the sum of prices for each customer per year.
Within the CTE, use the
LAG
window function to get the previous year's total purchases for each customer.
Compare the current year's total purchases with the previous year's total to ensure that the current year's total is strictly greater.
Use a subquery to exclude customers who have any year where the total purchases did not strictly increase from the previous year.
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
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...