Each row of Transactions is a deposit or a withdrawal made on some day. For every (day, type) pair that occurs in the table, return the full row of the last transaction, where the last one is the transaction with the largest transaction_id.
Input Table(s)
Table Transactions:
| Column | Type | Meaning |
|---|
| transaction_id | INT | Unique ID of the transaction |
| day | DATE | Calendar day of the transaction |
| type | ENUM('Deposit', 'Withdraw') | Either 'Deposit' or 'Withdraw' |
| amount | INT | Transaction amount, a positive integer |
Primary key: transaction_id.
Output Contract
-
Columns in this order:
transaction_id
,
day
,
type
,
amount
, copied unchanged from the input row.
-
One row per (
day
,
type
) pair that appears in the table.
-
Rows are ordered by
transaction_id
ascending. The ID is unique, so the order is fully determined.
-
No rounding is involved.
Example
Input Transactions:
| transaction_id | day | type | amount |
|---|
| 1 | 2024-03-01 | Deposit | 100 |
| 2 | 2024-03-01 | Withdraw | 40 |
| 3 | 2024-03-01 | Deposit | 25 |
| 4 | 2024-03-02 | Withdraw | 10 |
| 5 | 2024-03-02 | Deposit | 50 |
| 6 | 2024-03-02 | Withdraw | 70 |
| 7 | 2024-03-03 | Deposit | 30 |
Expected output:
| transaction_id | day | type | amount |
|---|
| 2 | 2024-03-01 | Withdraw | 40 |
| 3 | 2024-03-01 | Deposit | 25 |
| 5 | 2024-03-02 | Deposit | 50 |
| 6 | 2024-03-02 | Withdraw | 70 |
| 7 | 2024-03-03 | Deposit | 30 |
On 2024-03-01, deposit 3 comes after deposit 1, and withdrawal 2 is the only withdrawal. On 2024-03-02, withdrawal 6 comes after withdrawal 4, and deposit 5 is the only deposit. 2024-03-03 has a single deposit, so it contributes one row. The result is listed by transaction_id.
Constraints and Clarifications
-
The table has at most 10,000 rows.
-
amount
is a positive integer.
-
The table has no time-of-day column. "Last" is decided by
transaction_id
alone.
-
transaction_id
is unique, so each (
day
,
type
) group has exactly one last row and there are no ties.
-
A day with only one type of transaction returns one row. Pairs that never occur return nothing.
-
Assume
day
and
type
are never NULL.