Skip to content

Commit 4e87215

Browse files
authored
Create find-third-transaction.sql
1 parent a4e8414 commit 4e87215

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

MySQL/find-third-transaction.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
WITH prev_cte AS (
5+
SELECT *,
6+
RANK () OVER (PARTITION BY user_id ORDER BY transaction_date) AS rnk,
7+
LAG(spend, 1) OVER (PARTITION BY user_id ORDER BY transaction_date) AS prev1,
8+
LAG(spend, 2) OVER (PARTITION BY user_id ORDER BY transaction_date) AS prev2
9+
FROM Transactions
10+
)
11+
12+
SELECT user_id, spend AS third_transaction_spend, transaction_date AS third_transaction_date
13+
FROM prev_cte
14+
WHERE rnk = 3 AND prev2 < spend AND prev1 < spend
15+
ORDER BY 1;

0 commit comments

Comments
 (0)