列表

详情


2292. 连续两年有 3 个及以上订单的产品

表: Orders

+---------------+------+
| Column Name   | Type |
+---------------+------+
| order_id      | int  |
| product_id    | int  |
| quantity      | int  |
| purchase_date | date |
+---------------+------+
order_id 包含唯一值。
该表中的每一行都包含订单 ID、购买的产品 ID、数量和购买日期。

 

编写解决方案,获取连续两年订购三次或三次以上的所有产品的 id。

以 任意顺序 返回结果表。

结果格式示例如下。

 

示例 1:

输入: 
Orders 表:
+----------+------------+----------+---------------+
| order_id | product_id | quantity | purchase_date |
+----------+------------+----------+---------------+
| 1        | 1          | 7        | 2020-03-16    |
| 2        | 1          | 4        | 2020-12-02    |
| 3        | 1          | 7        | 2020-05-10    |
| 4        | 1          | 6        | 2021-12-23    |
| 5        | 1          | 5        | 2021-05-21    |
| 6        | 1          | 6        | 2021-10-11    |
| 7        | 2          | 6        | 2022-10-11    |
+----------+------------+----------+---------------+
输出: 
+------------+
| product_id |
+------------+
| 1          |
+------------+
解释: 
产品 1 在 2020 年和 2021 年都分别订购了三次。由于连续两年订购了三次,所以我们将其包含在答案中。
产品 2 在 2022 年订购了一次。我们不把它包括在答案中。

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
# Write your MySQL query statement below

mysql 解法, 执行用时: 412 ms, 内存消耗: 0 B, 提交时间: 2023-10-15 17:29:12

# Write your MySQL query statement below
with a as (
SELECT product_id, YEAR(purchase_date) YY
FROM Orders
GROUP BY product_id, YEAR(purchase_date)
HAVING COUNT(DISTINCT order_id) >= 3
)

SELECT DISTINCT b.product_id
FROM a b, a c
WHERE b.product_id = c.product_id AND ABS(b.YY - c.YY) = 1

mysql 解法, 执行用时: 331 ms, 内存消耗: 0 B, 提交时间: 2023-10-15 17:29:06

# Write your MySQL query statement below


select distinct b.product_id
from (
select
a.product_id,year(a.purchase_date) as year ,
dense_rank()over(partition by a.product_id order by year(a.purchase_date)) as rk 
from Orders as a group by a.product_id,year(a.purchase_date) having count(a.order_id)>2 ) as b 
group by b.product_id,b.year-b.rk having count(*)>1

上一题