Code Generator | SQL

SQL Function to Calculate Lost Customers

This SQL function called "calculate_lost_customers" is created to determine the number of lost customers by month and year from a sales table. The function iterates through each month, starting from the minimum date until 6 months prior to the curren...


Empty image or helper icon

This Query related with thread "Creating a Table to Track New Customers Each Month"

Prompt

What is the code of calculating lost customer by month and year from a sales table, let’s say customers are lost after not purchasing for 6 months

Answer

To calculate the number of lost customers by month and year from a sales table, you can use the following SQL function:

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;

Example usage:

SELECT *
FROM calculate_lost_customers();

In the example above, the function calculate_lost_customers is created to calculate the number of lost customers by month and year. It takes no input parameters and returns a table with three columns: year, month, and lost_customers_count.

The function first finds the minimum and maximum dates from the sales table to determine the range of dates to loop through. It then iterates through each month, starting from the minimum date until 6 months prior to the current date.

For each month, the function calculates the count of distinct customer IDs who have not made a purchase within the previous 6 months. This is achieved by querying the sales table using the appropriate date range. The result is stored in the lost_customers_count variable.

Finally, the function returns the current month, year, and the corresponding lost customers count using the RETURN NEXT statement.

Unit Testing:

The function can be tested by executing the SELECT statement to retrieve the results from the calculate_lost_customers function. This will return a table with three columns: year, month, and lost_customers_count, showing the number of lost customers for each month and year.

SELECT *
FROM calculate_lost_customers();

Make sure to replace the table and column names in the function with the appropriate names from your actual database schema.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description