MRP Net Requirements SQL Query Generator
Planners and analysts often need a quick projected balance outside the ERP screen, for a supplier review or a shortage report. This generator writes the query from your own table and column names.
Fill in
One row per requirement
Open POs and production orders
One row per item with on hand and safety stock
Already truncated to day or week
Projected available balance by item and bucket
WITH keys AS (
SELECT item_no, bucket_date FROM mrp_demand
UNION
SELECT item_no, bucket_date FROM mrp_supply
),
flow AS (
SELECT k.item_no,
k.bucket_date,
COALESCE((SELECT SUM(d.qty) FROM mrp_demand d
WHERE d.item_no = k.item_no
AND d.bucket_date = k.bucket_date), 0) AS gross_req,
COALESCE((SELECT SUM(s.qty) FROM mrp_supply s
WHERE s.item_no = k.item_no
AND s.bucket_date = k.bucket_date), 0) AS sched_receipts
FROM keys k
WHERE k.bucket_date <= DATE '2026-12-31'
)
SELECT f.item_no,
f.bucket_date,
f.gross_req,
f.sched_receipts,
st.on_hand_qty + SUM(f.sched_receipts - f.gross_req)
OVER (PARTITION BY f.item_no ORDER BY f.bucket_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS projected_available,
st.safety_stock_qty AS safety_stock
FROM flow f
JOIN item_stock st ON st.item_no = f.item_no
ORDER BY f.item_no, f.bucket_date;Shortage list: first bucket below safety stock per item
WITH proj AS (
-- paste the query above here as a CTE, then filter
SELECT * FROM projection_cte
)
SELECT item_no,
MIN(bucket_date) AS first_short_bucket,
MIN(projected_available - safety_stock) AS worst_gap
FROM proj
WHERE projected_available < safety_stock
GROUP BY item_no
ORDER BY first_short_bucket, item_no;Table and column names here are generic placeholders, not those of any specific ERP. Map them to your own demand, supply and stock views. The window syntax runs on PostgreSQL, SQL Server 2012 and later, Oracle and MySQL 8; the DATE literal may need CAST on SQL Server. The running balance does not create planned orders, so a negative balance shows the cumulative shortage, not a per-bucket order quantity. Test on a copy of the data first.
Adapt this query to your ERP schema
Optional: help mapping the generic table names to the real tables of your ERP.
Optional. The tool and every export work without this. No spam.
How to use it
Enter the names of your demand, supply and stock tables or views, the item and bucket date columns, and the horizon. The tool fills the placeholders and shows runnable SQL. Copy it into your SQL client, ideally against a reporting copy. Demand and supply must already be at one row per requirement or receipt with a bucket date, so truncate dates to day or week in a view first.
The logic behind the query
Each bucket's net movement is receipts minus gross requirements. A running sum of that movement per item, ordered by bucket, added to starting on-hand stock, gives the projected available balance. That is exactly the balance row of the MRP record. Comparing it to safety stock shows where MRP would have to plan a new order.
Limits and common mistakes
This is a netting projection, not a replacement for the MRP run.
- It ignores lot sizing, lead times and time fences.
- Demand from planned orders must be included if you want the dependent view.
- Mixing units of measure between tables gives wrong balances.
- Past-due receipts should be moved to the first bucket before running.
- Allocated stock should be removed from on hand if your ERP tracks it separately.
Frequently Asked Questions
Does this replace the MRP run?
No. It only nets existing demand and supply per bucket. It does not size planned orders, apply lead times, respect time fences or explode BOMs. Use it to check or explain what MRP shows, not to replace the engine.
Why use a window function for the balance?
A running sum per item ordered by date reproduces the projected available balance row in one pass, and it avoids slow self-joins. All major databases from recent versions support it.
How do I include past-due orders?
Create a view that sets the bucket date of any requirement or receipt earlier than today to the first bucket. Otherwise they appear in historic buckets and the running balance may start from the wrong point.
Can I run it on ERP production tables?
Prefer a reporting replica or read-only views. Large union and subquery plans can be heavy, so run off peak, add indexes on item and date columns, and have your DBA review it before scheduling.
If you would rather ask for a shortage list in plain language and get the query shown back, ERPray does that read-only over live ERP data.
Related Tools
BOM Low Level Code Python Script Generator
Fill in your CSV file and column names and get a Python script that assigns low level codes and flags circular BOMs.
Supply Chain & InventoryMRP Planning Terms Glossary Reference
A searchable glossary of the terms MRP planners use every day, with the plain meaning and how each term shows up in a planning run.
Supply Chain & InventoryPlanned Order Release Lead Time Calculator
Add setup, run, queue, safety and inspection time to find how many calendar days before the need date an order must be released.
Supply Chain & InventoryMRP Weekly Planner Review Agenda Template
A ready agenda and action log for the weekly meeting where planners, buyers and production review MRP exceptions and shortages.
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