# 1831.(Medium)Maximum Transaction Each Day

表: Transactions

```
+----------------+----------+
| Column Name    | Type     |
+----------------+----------+
| transaction_id | int      |
| day            | datetime |
| amount         | int      |
+----------------+----------+
transaction_id 是此表的主键。
每行包括了该次交易的信息。
```

写一条 SQL 返回每天交易金额 amount 最大的交易 ID 。如果某天有多个这样的交易，返回这些交易的 ID 。

返回结果根据 transaction\_id 升序排列。

查询结果样例如下：

```
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     |
+----------------+--------------------+--------+

Result table:
+----------------+
| transaction_id |
+----------------+
| 1              |
| 5              |
| 6              |
| 8              |
+----------------+
"2021-4-3"  --> 有一个 id 是 8 的交易，因此，把它加入结果表。 
"2021-4-28" --> 有两个交易，id 是 5 和 9 ，交易 5 的金额是 40 ，而交易 9 的数量是 21 。只需要将交易 5 加入结果表，因为它是当天金额最大的交易。
"2021-4-29" --> 有两个交易，id 是 1 和 6 ，这两个交易的金额都是 58 ，因此需要把它们都写入结果表。
最后，把交易 id 按照升序排列。
```

进阶：你可以不使用 `MAX()` 函数解决这道题目吗?

来源：力扣（LeetCode）

链接：<https://leetcode-cn.com/problems/maximum-transaction-each-day>

著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。

## Solution

* 用 `date()` 函数提取日期
* 以每日为窗口，取rank=1&#x20;

```sql
SELECT transaction_id
FROM (
    SELECT *, rank() over (PARTITION BY date(day) order by amount desc) rk
    FROM Transactions 
) a
where rk = 1
order by 1
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zqt0.gitbook.io/leetcode/sql/1831.maximum-transaction-each-day.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
