力扣题目跳转(. - 力扣(LeetCode))
表:
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 年订购了一次。我们不把它包括在答案中。
case 1 的建表语句。
Create table If Not Exists Orders (order_id int, product_id int, quantity int, purchase_date date)
Truncate table Orders
insert into Orders (order_id, product_id, quantity, purchase_date) values ('1', '1', '7', '2020-03-16')
insert into Orders (order_id, product_id, quantity, purchase_date) values ('2', '1', '4', '2020-12-02')
insert into Orders (order_id, product_id, quantity, purchase_date) values ('3', '1', '7', '2020-05-10')
insert into Orders (order_id, product_id, quantity, purchase_date) values ('4', '1', '6', '2021-12-23')
insert into Orders (order_id, product_id, quantity, purchase_date) values ('5', '1', '5', '2021-05-21')
insert into Orders (order_id, product_id, quantity, purchase_date) values ('6', '1', '6', '2021-10-11')
insert into Orders (order_id, product_id, quantity, purchase_date) values ('7', '2', '6', '2022-10-11')
一 我们使用窗口函数,配合使用 range between 来计算当前行时间到一年后购买的次数 cnt1,以及当前行时间到二年后购买的次数 cnt2.
select *, count(order_id) over(partition by product_id order by purchase_date range between current row and interval '1' year following) as cnt1, count(order_id) over(partition by product_id order by purchase_date range between current row and interval '2' year following) as cnt2 from orders;
输出如下
二 那么我们只需要挑选 cnt1 >= 3, cnt2 - cnt1 >= 3的数据即可。
with tmp as
(select *,
count(order_id) over(partition by product_id order by purchase_date range between current row and interval '1' year following) as cnt1,
count(order_id) over(partition by product_id order by purchase_date range between current row and interval '2' year following) as cnt2
from orders)
select distinct product_id from tmp where cnt1 >= 3 and cnt2 - cnt1 >= 3;
输出如下
结果发现我这个不对啊,因为我这个是具体到连续两年一共 365 x 2 的天数,而题目只需要每年内次数大于等于3次然后年再连续即可。那么我们改变下思路即可。
我们只求每年的成交次数,并选出大于等于3次的。
select distinct product_id,year(purchase_date) as year from orders group by product_id,year having count(*) >= 3
然后我们增加一列选取他的一下位为 t1.
with tmp as (select distinct product_id,year(purchase_date) as year from orders group by product_id,year having count(*) >= 3) select *,lag(year) over(partition by product_id order by year) as t1 from tmp;
with tmp as (select distinct product_id,year(purchase_date) as year from orders group by product_id,year having count(*) >= 3), tmp1 as (select *,lag(year) over(partition by product_id order by year) as t1 from tmp) select distinct product_id from tmp1 where year - t1 = 1;
很明显选出差值为1的即可。
以上就是全部答案,如果对你有帮助请点个赞,谢谢。
来源:力扣(leecode)
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
转载请注明出处:
我会尽快把力扣上的所有数据库题目发出来。感兴趣的可以点个赞与关注。每天不定时跟新。
标签:purchase,product,2292,id,订单,year,date,两年,order From: https://blog.csdn.net/CYJ1844/article/details/143780146