I provided a simplified sample of the table I'm currently working with here: http://sqlfiddle.com/#!17/c57e33/1
CREATE TABLE orders
(id SERIAL PRIMARY KEY,
customer_id INT,
created_at DATE)
;
INSERT INTO orders
(customer_id, created_at)
VALUES
(1, '2019-10-09'),
(1, '2019-10-01'),
(1, '2019-08-09'),
(2, '2019-10-09'),
(2, '2019-10-09'),
(3, '2019-09-09'),
(3, '2019-08-09'),
(4, '2019-08-09'),
(4, '2019-08-09'),
(5, '2019-10-09'),
(5, '2019-10-09'),
(5, '2019-08-09')
;
My problem is that I want to return only the customers with one or more orders on two specific dates and no orders in between. In the fiddle only the customer_id of 5 should match.
After trying many different ways of queries I finally found a way that works using a bunch of EXISTS clauses:
SELECT DISTINCT(customer_id)
FROM orders o1
WHERE EXISTS (SELECT 1 FROM orders o2 WHERE o1.customer_id = o2.customer_id AND o2.created_at = '2019-10-09')
AND EXISTS (SELECT 1 FROM orders o2 WHERE o1.customer_id = o2.customer_id AND o2.created_at = '2019-08-09')
AND NOT EXISTS (SELECT 1 FROM orders o2 WHERE o1.customer_id = o2.customer_id AND o2.created_at BETWEEN '2019-08-10' AND '2019-10-08')
But I am wondering if there is an easier and more efficient way to do this.