Skip to content

Commit cb7163b

Browse files
authored
Create customer-order-frequency.sql
1 parent d6dba45 commit cb7163b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

MySQL/customer-order-frequency.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Time: O(n)
2+
# Space: O(n)
3+
4+
SELECT a.customer_id,
5+
a.name
6+
FROM Customers AS a
7+
INNER JOIN
8+
(SELECT *
9+
FROM Orders
10+
WHERE order_date BETWEEN "2020-06-01" AND "2020-07-31" ) AS b
11+
ON a.customer_id = b.customer_id
12+
INNER JOIN Product AS c
13+
ON b.product_id = c.product_id
14+
GROUP BY a.customer_id
15+
HAVING SUM(CASE
16+
WHEN LEFT(b.order_date, 7) = "2020-06" THEN c.price * b.quantity
17+
ELSE 0
18+
END) >= 100
19+
AND SUM(CASE
20+
WHEN LEFT(b.order_date, 7) = "2020-07" THEN c.price * b.quantity
21+
ELSE 0
22+
END) >= 100
23+
ORDER BY NULL;

0 commit comments

Comments
 (0)