CODE WORKSPACE

Reconcile order totals

Return order_id, order_total, items_total, and diff (order_total - items_total) for orders whose stored total differs from the recomputed line-item total. Order by order_id.

Aggregate line items in a CTE, join to orders, and filter to the rows where the two totals disagree.

Sample input

ordersStored order totals (orders that have line items).
order_idtotal_amount
100180
1002120
100795
1008150
order_itemsLine items. Order 1001 sums to 80 + 30 + 120 = 230, not its stored 80.
order_idquantityunit_price
1001180
1001215
10011120
1007115
1008520

Expected output

Expected outputOrders whose stored total disagrees with the line-item sum; a negative diff means the stored total is lower.
order_idorder_totalitems_totaldiff
100180230-150
1002120140-20
1007951580
1008150180-30

Constraints

Recompute SUM(quantity * unit_price) per order in a CTE, JOIN it to orders, and keep only rows where total_amount <> items_total. Include the signed diff column.

Expected skills

Reconciliation by independent recomputation and surfacing signed differences.

SQL
Loading...

AI evaluation

Run the SQL query to inspect preview rows.