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.