ERP Foreign Currency Revaluation SQL Generator
African manufacturers often hold payables and receivables in dollars, euros and several local currencies. At month end someone has to value the open items at the closing rate and explain the unrealised gain or loss. This generator writes the SQL for that check so you can compare it with what your ERP revaluation run posts.
Fill in
Generic name. Replace with your open item table or a view over AR and AP.
Generic name. Must have from_currency, to_currency, rate_date and rate.
Open items at the closing rate
-- Revalue open AP items to ZAR at the latest rate on or before 2026-09-30
-- Table and column names are generic. Map them to your ERP.
SELECT oi.doc_no,
oi.partner_id,
oi.currency,
oi.open_amount_fc,
oi.booked_rate,
r.rate AS closing_rate,
oi.open_amount_fc * oi.booked_rate AS base_booked,
oi.open_amount_fc * r.rate AS base_at_close,
oi.open_amount_fc * r.rate - oi.open_amount_fc * oi.booked_rate AS reval_difference
FROM open_items oi
JOIN fx_rates r
ON r.from_currency = oi.currency
AND r.to_currency = 'ZAR'
AND r.rate_date = (
SELECT MAX(r2.rate_date)
FROM fx_rates r2
WHERE r2.from_currency = oi.currency
AND r2.to_currency = 'ZAR'
AND r2.rate_date <= DATE '2026-09-30'
)
WHERE oi.ledger = 'AP'
AND oi.status = 'OPEN'
AND oi.currency <> 'ZAR'
ORDER BY oi.currency, oi.doc_no;Summary by currency
-- Net unrealised difference by currency
SELECT oi.currency,
COUNT(*) AS open_items,
SUM(oi.open_amount_fc) AS open_fc,
SUM(oi.open_amount_fc * oi.booked_rate) AS base_booked,
SUM(oi.open_amount_fc * r.rate) AS base_at_close,
SUM(oi.open_amount_fc * r.rate - oi.open_amount_fc * oi.booked_rate) AS unrealised_difference
FROM open_items oi
JOIN fx_rates r
ON r.from_currency = oi.currency
AND r.to_currency = 'ZAR'
AND r.rate_date = (
SELECT MAX(r2.rate_date)
FROM fx_rates r2
WHERE r2.from_currency = oi.currency
AND r2.to_currency = 'ZAR'
AND r2.rate_date <= DATE '2026-09-30'
)
WHERE oi.ledger = 'AP'
AND oi.status = 'OPEN'
AND oi.currency <> 'ZAR'
GROUP BY oi.currency
ORDER BY oi.currency;Currencies with no rate on file
-- Open currencies that have no rate on or before the revaluation date
SELECT DISTINCT oi.currency
FROM open_items oi
WHERE oi.ledger = 'AP'
AND oi.status = 'OPEN'
AND oi.currency <> 'ZAR'
AND NOT EXISTS (
SELECT 1
FROM fx_rates r
WHERE r.from_currency = oi.currency
AND r.to_currency = 'ZAR'
AND r.rate_date <= DATE '2026-09-30'
);Table and column names are generic placeholders, not those of a specific ERP. Map them to your schema. DATE 'yyyy-mm-dd' literal syntax works in PostgreSQL, Oracle and MySQL, in SQL Server use CAST('yyyy-mm-dd' AS date). Check whether your ERP stores rates as units of foreign currency per base or the inverse, and for a receivable the difference sign means the opposite of a payable. This is a reconciliation aid, not a posting routine. Accounting treatment should follow your reporting framework.
Explain revaluation differences faster
Optional: a read only variance view that ties your revaluation journal to open items, built on your own ERP data.
Optional. The tool and every export work without this. No spam.
How to use it
Enter the names of your open items table and rate table, the base currency, the revaluation date and the ledger type. The generator produces three queries. The first lists each open item with its booked rate, the closing rate and the difference. The second summarises by currency. The third lists currencies with no rate on file, which is the most common cause of a missing line in a revaluation run. Run them in a read only reporting environment.
The logic behind the query
For each open foreign currency item the query multiplies the open amount in foreign currency by the closing rate and by the rate at which it was booked, then takes the difference. The closing rate is the latest rate dated on or before the revaluation date, found with a MAX(rate_date) subquery. This avoids failures on weekends or bank holidays where no rate was entered. The base currency items are excluded because they do not revalue.
Reading the output
A positive difference on a payable means you owe more in base currency than when you booked it, which is a loss. On a receivable, a positive difference is a gain. Compare the currency summary with the ERP revaluation journal. If they differ, check rate type, rate source, items in dispute, partly paid documents and whether the ERP revalues at document or account level. Currencies with big swings, such as the Nigerian naira or Ghanaian cedi in volatile periods, deserve a line by line review.
Common mistakes
Mistakes include mixing quote directions, using average rather than closing rates, revaluing items already settled, and forgetting intercompany balances. Another is assuming a rate exists for every day. Always run the missing rate query first. If your group reports under IFRS, follow your accounting policy on monetary items, and ask your auditor when in doubt.
Frequently Asked Questions
Does this post journals?
No. It is a read only query to check and explain revaluation. Posting should be done by the revaluation function of your ERP, with the right accounts, periods and approvals.
Why use the latest rate on or before the date?
Rate tables often have gaps for weekends and holidays. The MAX(rate_date) subquery picks the most recent available rate so the query still returns a value, which mirrors how most closing rate policies work.
How do I handle SQL Server?
Replace the DATE literal with CAST of a string to date, and check any reserved words in column names. The rest of the logic is standard SQL and works in most databases.
What about hyperinflation or devalued currencies?
This query only handles rate revaluation. Hyperinflation accounting and large devaluations need separate treatment under your reporting framework, so involve your finance and audit teams.
If month-end revaluation needs a faster explanation than exporting to Excel, Netray can build a read only variance view over your ERP finance tables.
Related Tools
Multi-Currency Revaluation Gain and Loss Calculator
Compute the unrealised gain or loss at month end on an open foreign currency balance, then the realised result at settlement.
Costing & FinanceMulti-Currency Month End Revaluation Worksheet
Revalue foreign currency debtors, creditors and bank balances at the closing rate and see the unrealised gain or loss per line.
Compliance & ContractsAfrica Manufacturer Monthly Tax Compliance Checklist
A 32-point month-end checklist covering VAT, withholding tax, payroll taxes, customs and ERP data controls for a manufacturing entity in Africa.
Costing & FinanceAfrica FX Devaluation Landed Cost Sensitivity Calculator
Model how a depreciation of the naira, cedi, kwacha, birr or shilling moves the local-currency landed cost of an imported part.
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