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

https://leetcode.cn/problems/products-with-three-or-more-orders-in-two-consecutive-years/description/

表: 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 年订购了一次。我们不把它包括在答案中。

思路:

1、首先截取order_date的年,对product_id和年分组,count统计个数,然后根据执行顺序用having count(1) >= 3 ,这一步是求出哪些产品在这一年是被订购了三次及以上;

2、然后用CTE,对上面的结果进行row_number排序,然后用年year -row_number 得出差值;

原理:

原理

  • 假设一个产品的年份是 2020, 2021, 2022

  • row_number(简称rn) = 1, 2, 3

  • year - rn = 2019, 2019, 2019 → 相同的 diff → 同一个连续年份组

如果年份断了,比如 2020, 2022

  • rn = 1, 2

  • year - rn = 2019, 2020 → 不同 grp_id → 被划分成不同组

所以 diff年份段聚在一起了。

3、根据product_id和diff分组,having count(1)>=2 是筛选连续的年,因为连续最少就是两年就是连续,最后 对product_id去重得出结果。

代码:

sql 复制代码
with t as(
    select 
product_id,
extract(year from purchase_date) as year,
count(1)
from orders
group by product_id,extract(year from purchase_date)
having count(1) >=3
)
,t1 as(
    select 
product_id,
year,
year - row_number() over(partition by product_id order by year) diff
 from t
)
select distinct product_id from t1 group by product_id,diff
having count(*) >= 2
;
相关推荐
澈2071 天前
【无标题】QT入门第十二天:数据库编程(下)模型视图与数据展示 | 零基础学QT
数据库·qt·oracle
郭梧悠1 天前
算法:有效的括号
python·算法·leetcode
旖-旎1 天前
《LeetCode 1137 第N个泰波那契数 和 LeetCode 三步问题》
c++·算法·leetcode·动态规划
wabs6661 天前
关于动态规划【力扣718.最长重复子数组的思考】
算法·leetcode·动态规划
YuK.W1 天前
Leetcode100: 94.二叉树中序遍历、104.二叉树最大深度、226.翻转二叉树
java·算法·leetcode·二叉树
在路上~~~~1 天前
EBS 启用系统标准的debug
运维·oracle
落叶-IT1 天前
Java异常处理深度实战教程:彻底掌握异常传播机制,规避线上隐性故障
java·数据库·oracle
想吃火锅10051 天前
【leetcode】146.LRU缓存js
算法·leetcode·缓存
To_OC7 天前
LC 207 课程表:刚学图论那会儿,我连这是拓扑排序都没看出来
javascript·算法·leetcode
To_OC7 天前
LC 208 实现 Trie 前缀树:曾被名字劝退,写完发现是送分题
javascript·算法·leetcode