MySales: Calculation of Lost Sales and On-Shelf Availability Using an SQL Query

MySales: Calculation of Lost Sales and On-Shelf Availability Using an SQL Query

How does MySales calculate lost sales?

MySales calculates lost sales by taking into account several key factors and algorithms. The main calculation steps include:

  1. Determining the lost sales period

The system analyzes the store assortment over the last 1–2 weeks (configurable parameter).

Lost sales are recorded if:

  • End-of-day inventory level was equal to zero.

  • Inventory level was below 30% (configurable parameter) of the forecasted daily sales.

  1. Factors influencing lost sales calculation

  • Day-of-week coefficients — redistribute the weekly forecast across individual days.

  • Price elasticity and discounts — account for the impact of price changes on demand.

  • Seasonality and weather conditions — adjust sales estimates.

  1. Use of historical data

  • Sales history over the last 28 days is analyzed (configurable parameter).

  • Promotional and regular sales are separated.

  • If valid data is available, the average daily sales volume is calculated with adjustments applied.

  1. Lost sales calculation formula

Lost Sales = max(Forecast − Actual Sales, 0)

  • This calculation is applied only when inventory levels were insufficient.

  1. Data storage and forecast adjustment

  • Lost sales data is stored across multiple tables:

    • FCST_SKU_LOST — forecast-related lost sales for the last 2 weeks.

    • SALES_SKU_LOST — lost sales history at SKU–store–day level.

    • SALES_SKU_LOST_WEEK — aggregated lost sales at weekly level.

  • Future forecasts are adjusted based on the identified lost sales.

This mechanism enables MySales to account for true demand and minimize losses caused by insufficient product availability.

SQL query to extract lost sales, compare them with actual sales, and calculate on-shelf availability percentage

SELECT s.STORE_ID, s.SKU_ID, s.SDATE, s.SALES_VOLUME AS actual_sales, COALESCE(l.LOST_VOLUME, 0) AS lost_sales, (s.SALES_VOLUME * 100.0) / NULLIF(s.SALES_VOLUME + COALESCE(l.LOST_VOLUME, 0), 0) AS availability_percentage FROM SALES_SKU_DAY s LEFT JOIN SALES_SKU_LOST l ON s.STORE_ID = l.STORE_ID AND s.SKU_ID = l.SKU_ID AND s.SDATE = l.SDATE WHERE s.SDATE >= CURRENT_DATE - INTERVAL '30 days' -- Analyze the last 30 days ORDER BY s.SDATE DESC, s.STORE_ID, s.SKU_ID;

Query explanation

  • SALES_SKU_DAY (s) — contains actual sales data (SALES_VOLUME).

  • SALES_SKU_LOST (l) — contains lost sales data (LOST_VOLUME).

  • LEFT JOIN — ensures all actual sales records are included, even if no lost sales exist.

  • COALESCE — replaces NULL values in LOST_VOLUME with 0 when no lost sales are recorded.

On-shelf availability calculation

Formula:

(Actual Sales / (Actual Sales + Lost Sales)) * 100
  • NULLIF prevents division by zero.

  • WHERE filter limits the dataset to the last 30 days.

  • ORDER BY sorts results with the most recent dates first.

This query enables analysis of which stores and SKUs experience on-shelf availability issues. If the availability percentage drops below 100%, it indicates potential lost sales caused by insufficient product availability.