Skip to content

1511. Customer Order Frequency 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
SELECT
  Customers.customer_id,
  Customers.name
FROM Customers
INNER JOIN Orders
  USING (customer_id)
INNER JOIN Product
  USING (product_id)
GROUP BY 1
HAVING
  SUM(
    IF(
      DATE_FORMAT(Orders.order_date, '%Y-%m') = '2020-06',
      Orders.quantity * price,
      0
    )
  ) >= 100
  AND SUM(
    IF(
      DATE_FORMAT(Orders.order_date, '%Y-%m') = '2020-07',
      Orders.quantity * price,
      0
    )
  ) >= 100;