Если я правильно следил за вами, вы можете использовать агрегированный запрос для подсчета количества заказов на клиента и применять арифметику по мере необходимости:
SELECT c.email, (current_date - c.customer_reg_date) / 7 / COUNT(*) res
FROM customer_details c
INNER JOIN order_details o ON c.email = o.email
GROUP BY c.email, c.customer_reg_date
Демонстрация на DB Fiddle:
| email | res |
| ------------ | --- |
| fa@gmail.com | 5 |
| fr@gmail.com | 26 |
| mo@gmail.com | 9 |
Пример данных :
CREATE TABLE order_details(
email VARCHAR(12) NOT NULL
,order_id INTEGER NOT NULL
);
INSERT INTO order_details(email,order_id) VALUES ('mo@gmail.com',201);
INSERT INTO order_details(email,order_id) VALUES ('mo@gmail.com',202);
INSERT INTO order_details(email,order_id) VALUES ('fa@gmail.com',203);
INSERT INTO order_details(email,order_id) VALUES ('fr@gmail.com',204);
INSERT INTO order_details(email,order_id) VALUES ('fa@gmail.com',205);
CREATE TABLE customer_details(
email VARCHAR(12) NOT NULL PRIMARY KEY
,customer_reg_date DATE NOT NULL
);
INSERT INTO customer_details(email,customer_reg_date) VALUES ('mo@gmail.com','2019-05-24');
INSERT INTO customer_details(email,customer_reg_date) VALUES ('fa@gmail.com','2019-07-15');
INSERT INTO customer_details(email,customer_reg_date) VALUES ('fr@gmail.com','2019-03-29');