Leetcode Problem 2701. Consecutive Transactions with Increasing Amounts

2701. Consecutive Transactions with Increasing Amounts

Leetcode Solutions

Identifying Consecutive Transactions with Increasing Amounts Using Window Functions

  1. Use the LAG window function to get the previous transaction's amount and date for each customer, ordered by transaction date.
  2. Compare the current transaction's amount and date with the previous ones to determine if it's part of a consecutive increasing sequence.
  3. Use a conditional CASE statement to assign a value indicating the start of a new sequence whenever the consecutive increasing condition is not met.
  4. Calculate a running sum over these values to assign unique group identifiers to each sequence.
  5. Group by customer_id and the sequence identifier, and select the minimum and maximum dates to find the start and end of each sequence.
  6. Filter out groups where the duration is less than three days.
  7. Order the final result by customer_id and the start date of the sequence.

erDiagram
    TRANSACTIONS {
        int transaction_id PK
        int customer_id
        date transaction_date
        int amount
    }

Identifying Consecutive Transactions with Increasing Amounts Using Self-Joins and Aggregation

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...