About this tutorial
Compare analytical and booked Repo P&L
Continue from the previous Repo P&L calculation tutorial. We aggregate the analytical results and the corresponding general ledger entries at the same level: GL account, instrument, ISIN and currency.
Using Common Table Expressions (CTEs), UNION ALL and conditional aggregation, we bring both sides together and calculate the difference between analytical and booked P&L.
← Start with the Repo P&L calculation tutorial
How to reconcile two datasets using Oracle SQL
Data reconciliation compares two independently calculated balances at the same level of detail. In this example, we compare analytical Repo profit and loss with amounts posted to the general ledger, grouped by GL account, instrument, ISIN and currency.
Step 1: Separate the logic with Common Table Expressions (CTEs)
The query starts with a params CTE containing the reporting dates. The analytical_balance CTE calculates and aggregates Repo P&L, while booked_balance aggregates the corresponding general ledger entries. Keeping these calculations separate makes the reconciliation easier to review and troubleshoot.
Step 2: Combine analytical and booked balances with UNION ALL
Each dataset includes a source label: ANAL for analytical P&L and BOOKED for accounting entries. The recon CTE combines them using UNION ALL. This retains every aggregated row from both sources, including rows that exist on only one side.
recon AS (
SELECT * FROM analytical_balance
UNION ALL
SELECT * FROM booked_balance
)
Step 3: Use COALESCE and conditional aggregation to calculate the difference
CASE WHEN directs each source's amount into its own column. SUM aggregates the values at the common reconciliation keys, while COALESCE(..., 0) converts a missing aggregate to zero. This is especially useful when an instrument has analytical P&L but no corresponding booked entries, or vice versa.
SELECT
gl,
instrument_name,
isin,
currency,
ROUND(COALESCE(SUM(CASE WHEN type = 'ANAL'
THEN pnl END), 0), 2) AS anal,
ROUND(COALESCE(SUM(CASE WHEN type = 'BOOKED'
THEN pnl END), 0), 2) AS booked,
ROUND(
COALESCE(SUM(CASE WHEN type = 'ANAL'
THEN pnl END), 0)
- COALESCE(SUM(CASE WHEN type = 'BOOKED'
THEN pnl END), 0), 2) AS diff
FROM recon
GROUP BY gl, instrument_name, isin, currency;
The diff column is analytical P&L less booked P&L. A zero difference means the two totals agree for that grouping; a nonzero value identifies a reconciliation item to investigate.
Step 4: Review sign conventions and reporting dates
In the supplied example, general ledger debits are treated as positive and credits as negative. The analytical calculation uses the Repo transaction type to determine its sign. Both sides must follow consistent sign conventions and reporting periods for the difference to be meaningful.
Try the complete reconciliation in the SQL Playground
Open the Repo P&L Reconciliation example to run the full query with sample data, or download the complete Oracle SQL script. For the calculation behind the analytical side, see the previous Repo P&L tutorial.
Frequently asked questions
Why use UNION ALL instead of INNER JOIN for reconciliation?
UNION ALL preserves both datasets, including records that have no matching group on the other side. An INNER JOIN would omit unmatched groups unless the join strategy were changed. A FULL OUTER JOIN is another valid reconciliation approach.
What does COALESCE(SUM(...), 0) do?
It returns the aggregated amount when present and zero when the aggregate is null. This prevents missing values from turning the calculated difference into null.
Can the same SQL reconciliation pattern be used for other data?
Yes. Once the two sources use matching keys, consistent units and sign conventions, the same CTE, UNION ALL and conditional aggregation pattern can compare other analytical and accounting datasets.