+------------------+----------+
| Column Name | Type |
+------------------+----------+
| user_id | int |
| spend | decimal |
| transaction_date | datetime |
+------------------+----------+
(user_id, transaction_date) is column of unique values for this table.
This table contains user_id, spend, and transaction_date.
Write a solution to find the third transaction(if they have at least three transactions) of every user, where the spending on the preceding two transactions is lower than the spending on the third transaction.
Return the result table byuser_idinascending order.
Input:
Transactions table:+---------+--------+---------------------+| user_id | spend | transaction_date |+---------+--------+---------------------+|1|65.56|2023-11-1813:49:42||1|96.0|2023-11-3002:47:26||1|7.44|2023-11-0212:15:23||1|49.78|2023-11-1200:13:46||2|40.89|2023-11-2104:39:15||2|100.44|2023-11-2007:39:34||3|37.33|2023-11-0306:22:02||3|13.89|2023-11-1116:00:14||3|7.0|2023-11-2922:32:36|+---------+--------+---------------------+**Output**+---------+-------------------------+------------------------+| user_id | third_transaction_spend | third_transaction_date |+---------+-------------------------+------------------------+|1|65.56|2023-11-1813:49:42|+---------+-------------------------+------------------------+**Explanation**- For user_id 1, their third transaction occurred on 2023-11-18 at 13:49:42with an amount of $65.56, surpassing the expenditures of the previous two transactions which were $7.44 on 2023-11-02 at 12:15:23 and $49.78 on 2023-11-12 at 00:13:46. Thus,this third transaction will be included in the output table.- user_id 2 only has a total of 2 transactions, so there isn't a third transaction to consider.- For user_id 3, the amount of $7.0for their third transaction is less than that of the preceding two transactions, so it won't be included.Output table is ordered by user_id in ascending order.
We want to find, for each user, the third transaction (by date) where the previous two spends are both less than the third. We can use window functions to rank transactions and compare each transaction’s spend with the previous two.
SELECT user_id, spend, transaction_date
FROM (
SELECT*,
ROW_NUMBER() OVER (PARTITION BY user_id ORDERBY transaction_date) AS rn,
LAG(spend, 1) OVER (PARTITION BY user_id ORDERBY transaction_date) AS prev1,
LAG(spend, 2) OVER (PARTITION BY user_id ORDERBY transaction_date) AS prev2
FROM Transactions
) t
WHERE rn =3AND prev1 < spend AND prev2 < spend
ORDERBY user_id;
1
2
3
4
5
6
7
8
9
10
SELECT user_id, spend, transaction_date
FROM (
SELECT*,
ROW_NUMBER() OVER (PARTITION BY user_id ORDERBY transaction_date) AS rn,
LAG(spend, 1) OVER (PARTITION BY user_id ORDERBY transaction_date) AS prev1,
LAG(spend, 2) OVER (PARTITION BY user_id ORDERBY transaction_date) AS prev2
FROM Transactions
) t
WHERE rn =3AND prev1 < spend AND prev2 < spend
ORDERBY user_id;