Developer ToolsFree generatorPDF, Excel, Word exportElectronics Manufacturing

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

Generated code
Download:

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.