Given this table. I would like to know for each day how many different customers made a sale on date t and and t+1.
-- create a table CREATE TABLE sales_t( id INTEGER PRIMARY KEY, d_date date NOT NULL, sale INT NOT NULL, customer_n INT NOT NULL ); -- insert some values INSERT INTO sales_t VALUES (1, '2021-06-30', 12, 1); INSERT INTO sales_t VALUES (2, '2021-06-30', 22, 5); INSERT INTO sales_t VALUES (3, '2021-06-30', 111, 3); INSERT INTO sales_t VALUES (4, '2021-07-01', 27, 1); INSERT INTO sales_t VALUES (5, '2021-07-01', 90, 4); INSERT INTO sales_t VALUES (6, '2021-07-01', 33, 3); INSERT INTO sales_t VALUES (6, '2021-07-01', 332, 3);
The result for date 2021-06-30 is 2 because customer 1 and 3 made a sale in t and t+1.
Date sale_t_and_t+1 ..................................... 2021-06-30 2 2021-07-01 0
Advertisement
Answer
Use LEAD()
window function for each distinct combination of date and customer to create a flag which will be 1
if the customer is present in both days or 0
if not and aggregate:
SELECT d_date, COALESCE(SUM(flag), 0) `sale_t_and_t+1` FROM ( SELECT DISTINCT d_date, customer_n, LEAD(d_date) OVER (PARTITION BY customer_n ORDER BY d_date) = d_date + INTERVAL 1 DAY flag FROM sales_t ) t GROUP BY d_date;
See the demo.