Consecutive Transactions with Increasing Amounts Problem
Description
LeetCode Problem 2701.
Table: Transactions
+------------------+------+
| Column Name | Type |
+------------------+------+
| transaction_id | int |
| customer_id | int |
| transaction_date | date |
| amount | int |
+------------------+------+
transaction_id is the primary key of this table.
Each row contains information about transactions that includes unique (customer_id, transaction_date) along with the corresponding customer_id and amount.
Write an SQL query to find the customers who have made consecutive transactions with increasing amount for at least three consecutive days. Include the customer_id, start date of the consecutive transactions period and the end date of the consecutive transactions period. There can be multiple consecutive transactions by a customer.
Return the result table ordered by customer_id, consecutive_start, consecutive_end in ascending order.
The query result format is in the following example.
Example 1:
Input:
Transactions table:
+----------------+-------------+------------------+--------+
| transaction_id | customer_id | transaction_date | amount |
+----------------+-------------+------------------+--------+
| 1 | 101 | 2023-05-01 | 100 |
| 2 | 101 | 2023-05-02 | 150 |
| 3 | 101 | 2023-05-03 | 200 |
| 4 | 102 | 2023-05-01 | 50 |
| 5 | 102 | 2023-05-03 | 100 |
| 6 | 102 | 2023-05-04 | 200 |
| 7 | 105 | 2023-05-01 | 100 |
| 8 | 105 | 2023-05-02 | 150 |
| 9 | 105 | 2023-05-03 | 200 |
| 10 | 105 | 2023-05-04 | 300 |
| 11 | 105 | 2023-05-12 | 250 |
| 12 | 105 | 2023-05-13 | 260 |
| 13 | 105 | 2023-05-14 | 270 |
+----------------+-------------+------------------+--------+
Output:
+-------------+-------------------+-----------------+
| customer_id | consecutive_start | consecutive_end |
+-------------+-------------------+-----------------+
| 101 | 2023-05-01 | 2023-05-03 |
| 105 | 2023-05-01 | 2023-05-04 |
| 105 | 2023-05-12 | 2023-05-14 |
+-------------+-------------------+-----------------+
Explanation:
- customer_id 101 has made consecutive transactions with increasing amounts from May 1st, 2023, to May 3rd, 2023
- customer_id 102 does not have any consecutive transactions for at least 3 days.
- customer_id 105 has two sets of consecutive transactions: from May 1st, 2023, to May 4th, 2023, and from May 12th, 2023, to May 14th, 2023.
customer_id is sorted in ascending order.
Solution
Approach: Gaps and islands with LAG
Walk each customer's transactions in date order and mark a row as the start of a new run unless it continues the previous row: the previous transaction is exactly one day earlier (DATEDIFF = 1) and has a smaller amount. A running SUM of these start flags per customer gives every run its own id (the classic gaps-and-islands trick). Then group by customer and run id, keep runs with at least 3 rows, and report the first and last date.
A customer's first row has NULL from LAG, so the CASE falls to the ELSE branch and starts a run. A run that is cut by a missing day or a non-increasing amount starts a new one, so one customer can have several runs (customer 105 in the example), and a longer run is reported once, in full.
WITH flagged AS (
SELECT customer_id,
transaction_date,
CASE
WHEN DATEDIFF(transaction_date, LAG(transaction_date) OVER w) = 1
AND amount > LAG(amount) OVER w THEN 0
ELSE 1
END AS starts_run
FROM Transactions
WINDOW w AS (PARTITION BY customer_id ORDER BY transaction_date)
),
runs AS (
SELECT customer_id,
transaction_date,
SUM(starts_run) OVER (
PARTITION BY customer_id ORDER BY transaction_date
) AS run_id
FROM flagged
)
SELECT customer_id,
MIN(transaction_date) AS consecutive_start,
MAX(transaction_date) AS consecutive_end
FROM runs
GROUP BY customer_id, run_id
HAVING COUNT(*) >= 3
ORDER BY customer_id, consecutive_start, consecutive_end;