IMMEX Temporary Import Balance Aging SQL Generator
Under an IMMEX program, temporarily imported inputs must be exported, returned or otherwise discharged within the permitted period. This generator gives trade compliance and ERP teams a starting query to age the open balance per pedimento line from their own data.
Fill in
Your own staging table of temporary-import lines
Your own staging table of exports, returns or transfers that discharge imports
548 is about 18 months; confirm the term that applies to each item category
Flag lines expiring within this many days
Aging query (PostgreSQL syntax)
-- Assumes generic staging tables. Adjust column names to your ERP extract.
-- imports: pedimento_no, line_no, part_no, import_date, qty_imported
-- returns: import_pedimento_no, import_line_no, qty_discharged
WITH discharged AS (
SELECT import_pedimento_no, import_line_no, SUM(qty_discharged) AS qty_discharged
FROM stg_immex_returns
GROUP BY import_pedimento_no, import_line_no
)
SELECT i.pedimento_no,
i.line_no,
i.part_no,
i.import_date,
i.qty_imported,
COALESCE(d.qty_discharged, 0) AS qty_discharged,
i.qty_imported - COALESCE(d.qty_discharged, 0) AS qty_open,
i.import_date + INTERVAL '548 days' AS legal_deadline,
(i.import_date + INTERVAL '548 days')::date - CURRENT_DATE AS days_left,
CASE
WHEN (i.import_date + INTERVAL '548 days')::date < CURRENT_DATE THEN 'OVERDUE'
WHEN (i.import_date + INTERVAL '548 days')::date - CURRENT_DATE <= 60 THEN 'EXPIRING'
ELSE 'OK'
END AS status
FROM stg_immex_imports i
LEFT JOIN discharged d
ON d.import_pedimento_no = i.pedimento_no
AND d.import_line_no = i.line_no
WHERE i.qty_imported - COALESCE(d.qty_discharged, 0) > 0
ORDER BY legal_deadline ASC;Variant: open balance by part number
SELECT i.part_no,
SUM(i.qty_imported - COALESCE(d.qty_discharged, 0)) AS qty_open,
MIN(i.import_date + INTERVAL '548 days') AS earliest_deadline
FROM stg_immex_imports i
LEFT JOIN (
SELECT import_pedimento_no, import_line_no, SUM(qty_discharged) AS qty_discharged
FROM stg_immex_returns
GROUP BY import_pedimento_no, import_line_no
) d ON d.import_pedimento_no = i.pedimento_no AND d.import_line_no = i.line_no
GROUP BY i.part_no
HAVING SUM(i.qty_imported - COALESCE(d.qty_discharged, 0)) > 0
ORDER BY earliest_deadline ASC;Table and column names are generic placeholders, not a specific ERP schema. The permitted period differs by goods category under the IMMEX rules, and discharge is not always one-to-one with a pedimento line (scrap, waste, transfers and bill of materials yield matter). Use this as a planning report and confirm control rules with your customs broker.
Reconcile IMMEX balances against production
We can discuss a read-only report that links production consumption in your ERP to open import lines.
Optional. The tool and every export work without this. No spam.
How to use the query
Load imports and discharges into two staging tables, or point the placeholders at views over your ERP's customs module. Set the term in days for your item category, choose a warning window and run the first query to see open quantity, the deadline and a status. The second query rolls the open balance up to part number so planners can see which materials need attention first. Schedule it weekly and send the OVERDUE and EXPIRING rows to the customs team.
- Keep one row per pedimento line, not per pedimento
- Match discharges to import lines with a stable key
- Include scrap and waste discharges in the returns table
- Review the term per goods category with your broker
Why the balance rarely ties out at first
Raw quantities seldom match because finished goods consume inputs through the bill of materials, with yield loss, scrap and substitutions. The ERP knows the production quantity, but the customs record needs the equivalent input quantity discharged. Many first-time reports show large open balances that are really unrecorded discharges. Treat the first run as a data-quality exercise and fix the mapping from production orders and shipments to import lines.
Common mistakes
Frequent errors include using calendar months as 30-day blocks, ignoring tolerance for transfers between IMMEX companies, and counting a line as discharged once the export document is created rather than when the export is validated. Another mistake is building the report on inventory balances instead of on customs documents, which hides lines that were consumed but never reported. Keep the report anchored on pedimento lines.
Frequently Asked Questions
What is the permitted period for temporary imports?
It depends on the goods. Raw materials and components under IMMEX have historically had around 18 months, while machinery and other categories follow different rules. Confirm the term for each category with your customs broker and the current rules, then set the days field accordingly.
Can I run this on SQL Server or Oracle?
The syntax is PostgreSQL. For SQL Server replace the date arithmetic with DATEADD and DATEDIFF, and for Oracle use date plus a number of days. The CTE and join logic stays the same. Test the result on a small sample first.
Does this replace the IMMEX automated inventory control system?
No. Companies must keep the control records required by the customs authority, and the regulations specify the format and content. This query is an internal monitoring report built on your data, not a compliance filing or a replacement for those records.
What data do I need from the ERP?
Pedimento number and line, part number, import date and quantity for imports, plus export or return documents that reference the import line and the discharged quantity. If your ERP lacks that link, creating it is the main project, not the query.
If your open IMMEX balances do not reconcile with production, we can help map consumption from your ERP to import lines in a read-only report your customs team can review.
Related Tools
Nearshoring ERP and Trade Compliance Readiness Assessment
Score how ready your ERP, master data and compliance processes are for running a plant or supplier base in Mexico or Latin America.
Developer ToolsMexico CFDI 4.0 Invoice XML Skeleton Generator
Fill in a few fields and get a CFDI 4.0 ingreso XML skeleton to test your ERP mapping or a PAC sandbox request.
Country & TaxLatin America E-Invoicing and Tax ID Reference
One table of tax ID names, formats, e-invoice document names, authorities and VAT names for the main Latin American manufacturing markets.
Safety & WorkforceMexico Severance, Finiquito and Seniority Premium Calculator
Estimate the main liquidation components for a Mexican plant employee: constitutional indemnity, 20 days per year, seniority premium, proportional aguinaldo and vacation.
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