Developer ToolsFree generatorPDF, Excel, Word export

SQL Days of Supply and Inventory Coverage Query Generator

Days of supply is the quickest check on whether stock matches demand. This generator writes the SQL for it from your own table and column names, so you can run it in your ERP reporting database or hand it to a BI developer.

Fill in

Generated code
Download:

Days of supply by item

-- Generic SQL (ANSI style). Replace table and column names with your own.
-- Date arithmetic differs by database: adjust the window filter below.
WITH usage AS (
  SELECT item_no, SUM(qty) AS total_issued,
         SUM(qty) / 90.0 AS avg_daily_usage
  FROM inventory_transactions
  WHERE txn_type = 'ISSUE'
    AND txn_date >= CURRENT_DATE - INTERVAL '90' DAY
  GROUP BY item_no
)
SELECT s.item_no,
       s.site_id,
       s.qty_on_hand AS on_hand,
       s.safety_stock_qty AS safety_stock,
       COALESCE(u.avg_daily_usage, 0) AS avg_daily_usage,
       CASE WHEN COALESCE(u.avg_daily_usage, 0) = 0 THEN NULL
            ELSE s.qty_on_hand / u.avg_daily_usage END AS days_of_supply,
       CASE WHEN s.qty_on_hand < s.safety_stock_qty THEN 'BELOW SAFETY' ELSE 'OK' END AS status
FROM item_stock s
LEFT JOIN usage u ON u.item_no = s.item_no
WHERE s.site_id = 'PLANT01'
ORDER BY days_of_supply ASC;

Items below safety stock only

SELECT s.item_no, s.site_id, s.qty_on_hand, s.safety_stock_qty,
       s.safety_stock_qty - s.qty_on_hand AS shortfall
FROM item_stock s
WHERE s.site_id = 'PLANT01'
  AND s.safety_stock_qty > 0
  AND s.qty_on_hand < s.safety_stock_qty
ORDER BY shortfall DESC;

Excess coverage (more than 365 days)

SELECT s.item_no, s.qty_on_hand,
       s.qty_on_hand / (u.total_issued / 90.0) AS days_of_supply
FROM item_stock s
JOIN (SELECT item_no, SUM(qty) AS total_issued
      FROM inventory_transactions
      WHERE txn_type = 'ISSUE'
        AND txn_date >= CURRENT_DATE - INTERVAL '90' DAY
      GROUP BY item_no) u
  ON u.item_no = s.item_no
WHERE u.total_issued > 0
  AND s.qty_on_hand / (u.total_issued / 90.0) > 365;

Names are generic placeholders, not a specific ERP schema. Verify date syntax (SQL Server uses DATEADD, Oracle uses SYSDATE - n, MySQL uses DATE_SUB), whether quantities are signed, and that scrap or transfer transactions are excluded from usage.

Run this against your ERP

Optional: have the query adapted to your schema and scheduled.

Optional. The tool and every export work without this. No spam.

How to use the generator

Type the names of your stock table, usage history table and the key columns. Pick a usage window, usually 90 days for fast items and 365 for slow items. The three blocks give a full coverage list, a below-safety-stock list and an excess list. Copy the code into your SQL client and test it on a small site first.

  • Use read-only database access or a reporting replica
  • Start with one site to check that results look right
  • Export the result to Excel for the planning meeting

The logic

Average daily usage is the sum of issues over the window divided by the number of days. Days of supply is on hand divided by that rate. Division by zero is handled by returning NULL when there was no usage, so slow and dead items do not break the report. The status column compares on hand with the safety stock field and flags items below it.

Reading the results

Days of supply well below the replenishment lead time signals risk of a stockout. Values above a year point to excess or obsolete stock. Look at both ends of the sorted list together, because the same planning rule may be causing short and long positions in different items.

Common mistakes

Including receipts, returns or transfers in usage inflates demand. A window that crosses a plant shutdown understates it. Mixing units of measure between stock and usage tables gives nonsense coverage. Always check a handful of items by hand against the ERP screen.

Frequently Asked Questions

Which tables does this work with?

Any relational table pair with an on hand quantity and a transaction history. The names are placeholders you replace, so it fits SQL Server, Oracle, PostgreSQL or MySQL ERP databases after minor date syntax changes.

How long should the usage window be?

Ninety days suits fast movers. Slow items need 12 months or more, otherwise most show zero usage. Many teams run the report twice with different windows and compare.

Why is days of supply NULL for some items?

Those items had no issues in the window, so average daily usage is zero and coverage is undefined. They are candidates for slow moving or obsolete review rather than zero days of supply.

Can I run this against a production ERP?

Use a reporting replica or run during off hours, and keep the account read-only. Large transaction tables benefit from an index on item and date.

If you prefer to ask for coverage in plain language, Netray's ERPray can run read-only queries like this against your ERP data and show the SQL it used.