1821. 寻找今年具有正收入的客户

Leetcode题目:摸鱼题

表:Customers

+--------------+------+
| Column Name  | Type |
+--------------+------+
| customer_id  | int  |
| year         | int  |
| revenue      | int  |
+--------------+------+
(customer_id, year) 是该表的主键(具有唯一值的列的组合)。
这个表包含客户 ID 和不同年份的客户收入。
注意,这个收入可能是负数。

编写一个解决方案来报告 2021 年具有 正收入 的客户。

可以以 任意顺序 返回结果表。

结果格式如下示例所示。

示例 1:

Input:
Customers
+-------------+------+---------+
| customer_id | year | revenue |
+-------------+------+---------+
| 1           | 2018 | 50      |
| 1           | 2021 | 30      |
| 1           | 2020 | 70      |
| 2           | 2021 | -50     |
| 3           | 2018 | 10      |
| 3           | 2016 | 50      |
| 4           | 2021 | 20      |
+-------------+------+---------+

Output:
+-------------+
| customer_id |
+-------------+
| 1           |
| 4           |
+-------------+
客户 1 在 2021 年的收入等于 30 。
客户 2 在 2021 年的收入等于 -50 。
客户 3 在 2021 年没有收入。
客户 4 在 2021 年的收入等于 20 。
因此,只有客户 1 和 4 在 2021 年有正收入。

解题思路

摸鱼题

这种简单的题那肯定是要一次过嘀。

# Write your MySQL query statement below
SELECT customer_id
FROM Customers
WHERE year=2021
GROUP BY customer_id
HAVING SUM(revenue)>0
Subscribe
提醒
0 评论
Inline Feedbacks
View all comments
0
在此留下你的评论x