Created
May 13, 2024 00:14
-
-
Save lfy79001/2e6c3828f58db093f4f6695857dd88af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH CustomerData AS ( | |
SELECT | |
customer_unique_id, | |
customer_zip_code_prefix AS zip_code_prefix, | |
COUNT(DISTINCT orders.order_id) AS order_count, | |
SUM(payment_value) AS total_payment, | |
JULIANDAY(MIN(order_purchase_timestamp)) AS first_order_day, | |
JULIANDAY(MAX(order_purchase_timestamp)) AS last_order_day | |
FROM customers | |
JOIN orders USING (customer_id) | |
JOIN order_payments USING (order_id) | |
GROUP BY customer_unique_id | |
) | |
SELECT | |
customer_unique_id, | |
zip_code_prefix, | |
order_count AS PF, | |
total_payment / order_count AS AOV, | |
CASE | |
WHEN (last_order_day - first_order_day) < 7 THEN | |
1 | |
ELSE | |
(last_order_day - first_order_day) / 7 | |
END AS ACL | |
FROM CustomerData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment