COMPARE¶
Description¶
The COMPARE statement computes the net field difference between the earliest version and the latest version of a row within a specified time range. Unlike EVOLUTION (which lists every intermediate change), COMPARE collapses intermediate edits and shows only the net changes between the start and end of the interval.
Syntax¶
(See the Syntax Notation Guide for notation rules).
Basic syntax¶
Full syntax¶
[!IMPORTANT] Strict Clause Order: In
COMPARE,WHEREmust strictly precedeBETWEEN ... AND .... Both clauses are mandatory.
Parameters¶
| Parameter | Description |
|---|---|
<table> |
Target table name. |
WHERE <condition> |
Filter condition identifying the target row (e.g., id = 1). |
<date1> |
Starting date or timestamp of the observation interval. |
<date2> |
Ending date or timestamp of the observation interval. |
Example (employees)¶
Suppose employee 1 had multiple updates between January and December:
* Initial salary was 75000.0
* Mid-year salary changed to 95000.0
* End-of-year salary changed to 125000.0
Running COMPARE:
Expected Output¶
----------------------------------------------------------------------------------
Column Before After
----------------------------------------------------------------------------------
salary 75000.00 125000.00
----------------------------------------------------------------------------------
1 difference(s) across 1 row(s)
The intermediate update to 95000.00 is bypassed; only the net delta from 75000.00 to 125000.00 is shown.
How It Works¶
- Retrieve Range Records: Calls
Table::selectBetween(pk, t1, t2). - Select Endpoints: Selects the first version in the range (
recs.front()) and the last version in the range (recs.back()). - Field-by-Field Diff: Calls
compareRecords(front, back, meta). If a field's value infrontdoes not matchback, aDifferencerecord is added containingcolumn,before, andafter.
Errors & Edge Cases¶
| Error Condition | Error Message |
|---|---|
| Range Start After End | COMPARE range starts after it ends |
| Missing WHERE or BETWEEN | Parse error (after the WHERE condition in COMPARE). |
| No Matching Rows | No rows matched |