Skip to content

Commit e069176

Browse files
authored
Update products-with-three-or-more-orders-in-two-consecutive-years.sql
1 parent c907ed6 commit e069176

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

MySQL/products-with-three-or-more-orders-in-two-consecutive-years.sql

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Time: O(nlogn)
22
# Space: O(n)
33

4-
WITH consecutive_cte AS
5-
(SELECT product_id,
6-
YEAR(purchase_date) AS curr_year,
7-
LEAD(YEAR(purchase_date), 1) OVER(PARTITION BY product_id
8-
ORDER BY YEAR(purchase_date)) - YEAR(purchase_date) AS year_diff
9-
FROM orders
10-
GROUP BY 1, 2
11-
HAVING COUNT(order_id) >= 3
12-
ORDER BY NULL
13-
)
4+
WITH consecutive_cte AS (
5+
SELECT product_id,
6+
YEAR(purchase_date) AS curr_year,
7+
LEAD(YEAR(purchase_date), 1) OVER(PARTITION BY product_id
8+
ORDER BY YEAR(purchase_date)) - YEAR(purchase_date) AS year_diff
9+
FROM orders
10+
GROUP BY 1, 2
11+
HAVING COUNT(order_id) >= 3
12+
ORDER BY NULL
13+
)
1414

1515
SELECT DISTINCT product_id
1616
FROM consecutive_cte

0 commit comments

Comments
 (0)