Component Shortage SQL Query Generator
Buyers need a daily list of components that are short against open demand once on-hand stock and inbound purchase orders are counted. This generator builds that query as a common table expression pattern you can adapt to any ERP schema.
Fill in
Your item master table
Shortage query
-- Component shortage list, horizon 21 days, buyer EL01
-- Table and column names are generic: map them to your ERP schema.
WITH demand AS (
SELECT d.item_no, SUM(d.qty_required - d.qty_issued) AS open_demand
FROM wo_component d
WHERE d.need_date <= CURRENT_DATE + INTERVAL '21 days'
AND d.qty_required > d.qty_issued
GROUP BY d.item_no
),
supply AS (
SELECT p.item_no, SUM(p.qty_ordered - p.qty_received) AS inbound
FROM po_line p
WHERE p.status = 'OPEN'
AND p.due_date <= CURRENT_DATE + INTERVAL '21 days'
GROUP BY p.item_no
)
SELECT i.item_no,
i.description,
i.mpn,
i.lead_time_days,
COALESCE(s.qty_on_hand, 0) AS on_hand,
COALESCE(sp.inbound, 0) AS inbound,
dm.open_demand,
dm.open_demand - COALESCE(s.qty_on_hand, 0) - COALESCE(sp.inbound, 0) AS shortage
FROM demand dm
JOIN item i ON i.item_no = dm.item_no
LEFT JOIN item_stock s ON s.item_no = dm.item_no
LEFT JOIN supply sp ON sp.item_no = dm.item_no
WHERE i.buyer_code = 'EL01'
AND dm.open_demand - COALESCE(s.qty_on_hand, 0) - COALESCE(sp.inbound, 0) >= 1
ORDER BY shortage DESC, i.lead_time_days DESC;Variant: only parts that cannot be recovered inside lead time
-- Add this condition to the final WHERE clause to show parts whose -- earliest need date is inside the replenishment lead time. -- AND dm.earliest_need <= CURRENT_DATE + (i.lead_time_days * INTERVAL '1 day') -- You also need to add MIN(d.need_date) AS earliest_need to the demand CTE.
Table and column names are generic placeholders, not any specific ERP's schema. Verify joins, status codes, date arithmetic (shown in PostgreSQL syntax; change for SQL Server or Oracle) and how your ERP stores allocated or reserved stock before trusting the numbers. Run on a read replica or reporting copy, never on a busy production database without review.
Automated shortage report
Optional: we map this query to your actual ERP schema and show how it could run on a schedule inside your network.
Optional. The tool and every export work without this. No spam.
How to use the generator
Enter your own table names for items, work order components, stock and open purchase order lines, then set the demand horizon, a minimum shortage quantity and a buyer code. The query appears in the output panel and can be copied or exported. Map the generic column names, such as item_no and need_date, to the real columns in your database before running it.
- Replace generic table names with real ones
- Set a horizon that matches your expedite window
- Filter by buyer code so each buyer sees only their parts
The logic behind the query
Demand and supply are summed per item in two separate common table expressions, which prevents the row multiplication you get when work order lines and PO lines are joined directly. Shortage equals open demand minus on-hand minus inbound. Ordering by shortage and then by lead time puts the largest and slowest to recover parts at the top, which is the order a buyer wants to work in.
Adapting it to your ERP
Real systems have more nuance than this skeleton. Many keep reserved or allocated quantities separate from on-hand, hold quality or inspection stock in different locations, and store due dates on PO release lines. Decide whether quarantined stock counts as available, whether to net safety stock, and whether to include planned orders. Each choice changes the shortage figure, so document them next to the query.
Common mistakes
The most common error is joining demand and supply tables directly, which inflates quantities. Another is ignoring past-due PO lines, which makes late supply look like inbound stock. Finally, running the query without a horizon returns long-range demand that is not actionable, burying the parts the buyer must chase this week.
Frequently Asked Questions
Does this query work in SQL Server or Oracle?
The structure does, but date arithmetic differs. The sample uses PostgreSQL interval syntax, so change it to DATEADD for SQL Server or interval expressions supported by Oracle.
Should safety stock be included?
That is a policy choice. Some buyers net it out so that shortage means a real stockout risk, others add it to demand. Pick one approach and state it in a comment above the query.
Can I run it against a production ERP database?
Prefer a reporting replica or read-only copy, and test with limited rows first. Long queries can lock tables or slow transactions on a live system.
How do I handle parts with approved alternates?
Add a mapping table of alternates and either sum their stock into the supply CTE or report them as a separate column. Keep the alternate approval status in the join so unapproved parts are not counted.
If your buyers still build shortage lists by hand, we can set up a read-only agent over your ERP that answers shortage questions in plain language and shows the query it ran.
Related Tools
BOM Scrub Python Script Generator (Duplicate MPN, Missing RefDes, Qty Mismatch)
Generate a Python script that reads a BOM CSV and reports duplicate MPNs, blank part numbers, missing reference designators and quantity versus refdes count mismatches.
Supply Chain & InventoryElectronic Component Sourcing Resilience Assessment
Score your sourcing practices across sourcing breadth, forecasts, alternates, buffers, supplier terms and data quality to see where shortage exposure is greatest.
Electronics ManufacturingIC Package Type Reference (SOIC, QFN, BGA, TSSOP, QFP and More)
A searchable list of common semiconductor package types with full names, mounting method, lead style and typical lead pitch.
Electronics ManufacturingSMD Chip Component Size Code Reference (0201, 0402, 0603, 0805, 1206)
Look up imperial and metric size codes for chip resistors and capacitors with nominal length and width, plus typical resistor power ratings.
More Developer Tools tools
See all- Pick and Place Centroid File Converter Script Generator (Python)
- PCBA BOM Staging Table and Validation SQL Generator
- 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