Documentation Generator | SQL
calculate_lost_customers
This PostgreSQL function, named `calculate_lost_customers()`, returns a table with three columns: `year`, `month`, and `lost_customers_count`. It uses a loop to iterate through each month starting from the minimum date in the `sales` table up until 6...
![Empty image or helper icon](https://fs.enterprisedna.co/library/data-mentor/cover-images/JSOdsynofJZUtNsHh4eIfV6Aan3QQI7eMxz4XHDO.webp)
This Query related with thread "Creating a Table to Track New Customers Each Month"
Prompt
CREATE FUNCTION calculate_lost_customers()
RETURNS TABLE (year INT, month INT, lost_customers_count INT) AS
$$
DECLARE
previous_date DATE;
BEGIN
-- Get the minimum and maximum dates from the sales table
SELECT MIN(sales_date), MAX(sales_date)
INTO previous_date
FROM sales;
-- Loop through each month
WHILE previous_date <= CURRENT_DATE - INTERVAL '6 months' LOOP
-- Get the year and month
year := EXTRACT(YEAR FROM previous_date);
month := EXTRACT(MONTH FROM previous_date);
-- Get the customers who have not made a purchase for 6 months
SELECT COUNT(DISTINCT customer_id)
INTO lost_customers_count
FROM sales
WHERE sales_date >= previous_date - INTERVAL '6 months'
AND sales_date < previous_date;
-- Return the result for the current month and year
RETURN NEXT;
-- Move to the next month
previous_date := previous_date + INTERVAL '1 month';
END LOOP;
END
$$ LANGUAGE plpgsql;
Answer
CREATE FUNCTION calculate_lost_customers()
RETURNS TABLE (year INT, month INT, lost_customers_count INT) AS
$$
DECLARE
previous_date DATE;
BEGIN
-- Get the minimum and maximum dates from the sales table
SELECT MIN(sales_date), MAX(sales_date)
INTO previous_date
FROM sales;
-- Loop through each month
WHILE previous_date <= CURRENT_DATE - INTERVAL '6 months' LOOP
-- Get the year and month
year := EXTRACT(YEAR FROM previous_date);
month := EXTRACT(MONTH FROM previous_date);
-- Get the customers who have not made a purchase for 6 months
SELECT COUNT(DISTINCT customer_id)
INTO lost_customers_count
FROM sales
WHERE sales_date >= previous_date - INTERVAL '6 months'
AND sales_date < previous_date;
-- Return the result for the current month and year
RETURN NEXT;
-- Move to the next month
previous_date := previous_date + INTERVAL '1 month';
END LOOP;
END
$$ LANGUAGE plpgsql;
This code snippet shows a PostgreSQL function named calculate_lost_customers()
. The function returns a table with three columns: year
, month
, and lost_customers_count
.
The function uses a loop to iterate through each month, starting from the minimum date in the sales
table up until 6 months prior to the current date.
Each iteration of the loop calculates the number of lost customers for the current month by counting the distinct customer IDs from the sales
table.
The function uses the INTO
keyword to assign the values to the corresponding variables: previous_date
for the minimum date and lost_customers_count
for the customer count.
The RETURN NEXT
statement returns the result for the current month and year as a row in the result set.
Finally, the previous_date
is incremented by 1 month to move to the next iteration.
Overall, this function calculates the number of lost customers for each month within the past 6 months based on the sales
table.