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
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.
Related Tools
SAP ABAP Stock Below Safety Stock Report Generator
Produces a small ABAP report that joins MARC and MARD to list materials with unrestricted stock under the safety stock for a plant and MRP controller.
Supply Chain & InventoryDemand Pattern Classification Reference (ADI and CV2)
Look up the four demand classes from ADI and CV2, with the usual forecasting method and stocking approach for each.
Supply Chain & InventoryInventory Planning Glossary with Formulas and SAP Fields
A searchable glossary of inventory planning terms with the working formula and the matching SAP MARC material master field where one exists.
Costing & FinanceInventory Write-Down Approval Memo Template
A memo for requesting approval to write down or dispose of excess and obsolete inventory, with the evidence finance and auditors expect.
More Developer Tools tools
See all- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator
- SAP QM Usage Decision BAPI Skeleton
- ABAP Custom Exception Class Generator
- ABAP Unit Test Class Generator
Keep exploring
Free tools
- OEE SQL Query GeneratorFill in your table and column names and get a SQL query that calculates availability, performance, quality and OEE per line per day.
- Inspection Results SQL Query GeneratorFill in your own inspection table and column names to generate SQL for lot reject rate by supplier and top failing characteristics.
- Open Production Operations Load SQL Query GeneratorFill in your table and column names and get ready SQL for remaining hours, past due operations and weekly load per work center.
ERP fixes and how-tos
- SYSPRO SQL Query for Inventory On Hand by WarehouseHow to query SYSPRO inventory on hand by warehouse in SQL Server: company database names, InvMaster and InvWarehouse joins, read-only safety and pitfalls.
- How to query Infor Data Lake with Compass SQLHow to run a SQL query against Infor Data Lake with Compass: finding the right schema, filtering large BOD tables, and exporting the results safely.
- D365 Master Planning Ran but No Planned Orders Were GeneratedMaster planning ran but created no planned orders in Dynamics 365. Check item coverage, coverage groups, stopped flags, time fences and Planning Optimization.
Guides
- SQL Query Optimization for SyteLine ReportingOptimize SQL queries against Infor SyteLine database tables. Index strategies for SL tables, query plan analysis, and performance tuning for SSRS reports.
- How to Optimize SyteLine Database PerformanceImprove Infor SyteLine database performance with SQL Server optimization techniques including index tuning, query analysis, tempdb configuration, and statistics maintenance.
- SyteLine Database Optimization and SQL TuningOptimize SyteLine SQL Server databases. Index maintenance, query tuning, tempdb optimization, and database configuration for peak CloudSuite performance.
AI on your ERP
- AI for Warranty Claims and Returns (RMA): Check Coverage, Find Failure Patterns and Close the Loop to QualityAI for warranty claims and returns on top of your ERP: check coverage, find failure patterns, draft RMA decisions and feed quality, with human approval throughout.
- Natural Language Query for ERP Data: Ask SAP, Infor, or Oracle a Question in Plain EnglishSee how natural language query over SAP, Infor, Oracle, and NetSuite data works: grounded text-to-SQL, role-based permissions, and a visible audit trail.