70. Find Transactions with Maximum Amount Per Day

We have a table named Transactions that contains the information about transactions.

Table: Transactions

╔════════════════╦══════════╗
ā•‘  Column Name   ā•‘   Type   ā•‘
╠════════════════╬══════════╣
ā•‘ transaction_id ā•‘   int    ā•‘
║────────────────┼──────────║
ā•‘      day       ā•‘ datetime ā•‘
║────────────────┼──────────║
ā•‘     amount     ā•‘   int    ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

transaction_id is the primary key and contains unique values for this table.

Each row in this table represents a transaction with its transaction_id, day of the transaction, and the amount for that transaction.

Task:

Write a query to find the transaction_id of the transactions with the maximum amount on their respective day. If there are multiple transactions on the same day with the maximum amount, return all of them.

Return the result table ordered by transaction_id in ascending order.

Example 1:

Example

Input:

Transactions table:

╔════════════════╦════════════════════╦════════╗
ā•‘ transaction_id ā•‘        day         ā•‘ amount ā•‘
╠════════════════╬════════════════════╬════════╣
ā•‘       8        ā•‘ 2021-4-3 15:57:28  ā•‘   57   ā•‘
║────────────────┼────────────────────┼────────║
ā•‘       9        ā•‘ 2021-4-28 08:47:25 ā•‘   21   ā•‘
║────────────────┼────────────────────┼────────║
ā•‘       1        ā•‘ 2021-4-29 13:28:30 ā•‘   58   ā•‘
║────────────────┼────────────────────┼────────║
ā•‘       5        ā•‘ 2021-4-28 16:39:59 ā•‘   40   ā•‘
║────────────────┼────────────────────┼────────║
ā•‘       6        ā•‘ 2021-4-29 23:39:28 ā•‘   58   ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•©ā•ā•ā•ā•ā•ā•ā•ā•ā•

Output:

╔════════════════╗
ā•‘ transaction_id ā•‘
╠════════════════╣
ā•‘       1        ā•‘
║────────────────║
ā•‘       5        ā•‘
║────────────────║
ā•‘       6        ā•‘
║────────────────║
ā•‘       8        ā•‘
ā•šā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•ā•

Explanation:

On 2021-4-3, we have only one transaction with ID 8, so we add 8 to the result.

On 2021-4-28, we have two transactions with IDs 5 and 9. Transaction 5 has the maximum amount (40), so we include it in the result.

On 2021-4-29, both transactions 1 and 6 have the same amount (58), so we include both in the result.

The result is ordered by transaction_id.

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Ā 
Test Case

Input:

Transactions