Compare Source and Target Data

Prev Next

To compare source and target data, build a standard test whose control is Another Data Set: one query runs against the target, another against the source, and Validatar compares the two results. Each query runs where its data lives.

A load can finish without errors and still leave rows behind. Comparing the two sides is how you find out before someone builds a report on the gap.

Three ways to compare

How much you compare is up to the queries you write and how you tell Validatar to process their results.

Comparison Each query returns Process data as What a failure tells you
Row count One number, from COUNT(*) Single Value The two sides have different numbers of rows
Aggregate One number, such as a SUM of an amount Single Value The totals differ, even if the row counts match
Key-based Rows, each with key columns and value columns Table Which keys are missing on either side, and which keys have different values

A key-based comparison can be coarse or fine. Key on the month and the value is a monthly total; key on the order id and the values are the order's columns, which is a row-by-row comparison. The coarse version runs fast and tells you which months to look at. The fine version names every row.

Column Aggregation and Row Count also reduce a result to one number, but they do it after the rows come back to Validatar. For big tables, aggregate in the query and use Single Value.

Set up the comparison

These steps start on the Test Definition tab of a standard test whose test data set is already set up. If you have not started one, see Create a Standard Test.

  1. In What do you want to compare to?, choose Another Data Set.
  2. In Data Source, choose the other side of the comparison. It can be the same data source as the test data set, for example a staging schema against a reporting schema.
  3. In How to define the control data, choose Script.
  4. Enter the control query in SQL Script. If it is the same query as the test side, tick Same as above instead.
  5. Click Preview & Initialize Options on the control panel.
  6. Set Process data as to match the test side.
  7. For Table, set each column's role: Key, Value or Ignore.
  8. Set the Result Configuration (below), then click Save.

How key and value columns work

With Table processing, every row is identified by its Key columns. Validatar matches each test row to the control row with the same key, then compares the Value columns of the pair. Columns set to Ignore come back in the result but are not compared, which suits load timestamps and audit columns.

  • Define column roles using chooses whether roles follow the column Name or its Position. By name is safer when the two queries list columns in a different order.
  • In the Column Mapping row of the Result Configuration, Mapping Method decides how test columns pair with control columns: Automatic pairs them for you, Manual lets you choose each pair. Treat values as sets the type used to compare values: Automatic, String, Numeric, Date, Date/Time or Date/Time with Time Zone.
  • Both queries must return keys in the same format. 2024-04 on one side and 2024-4 on the other are different keys, so every row would go unmatched.

A key can be present on one side only. Unmatched Keys in the Result Configuration says what to do:

Setting Means Choices
Test Only The key is in the test data set and not in the control Fail, Pass, or Override with a value in Set Control Value To
Control Only The key is in the control and not in the test data set Fail, Pass, or Override with a value in Set Test Value To

Override suits sparse data. A month with no orders may be absent from one side's GROUP BY instead of showing 0. Override the missing value with 0 and Validatar compares 0 with the other side's count.

When both sides return a single value, the same row is labelled Empty Data Set, and it covers a query that returns nothing.

Tolerances

Value Success Criteria decides when a pair of values matches. For numbers:

Comparison method Passes when the difference is at most
Exact Match Only Nothing: the values must be equal
Absolute Numeric Difference The Numeric Value you enter, for example 0.01 to absorb rounding between two currency types
Percent of Test Value The Percent Value you enter, as a percent of the test value
Percent of Control Value The Percent Value you enter, as a percent of the control value
Custom Calculation Whatever your expression says

Dates use Exact Match Only, Within (a number and a unit of time) or Custom Calculation. Strings compare with an operator you choose, with an optional Case sensitive box.

Overall Success Criteria then decides whether the test passes from the row results: All Rows Must Pass, Up to X Rows Can Fail, Up to X Percent of Rows Can Fail or Custom Calculation. The formulas behind each method are on Create a Standard Test, and every setting is on Configuring Result Criteria.

Worked example: invoiced sales by month

The Warehouse Reconciliation project has a test named Wholesale Invoiced Sales Tie Out by Month. Finance closes each month on invoiced sales, so the warehouse copy must match the Wholesale Distribution System to the cent, month by month. That makes it an aggregate and a key-based comparison at once: the key is the month and the value is that month's total.

The test data set is the warehouse copy, on the Enterprise Data Warehouse (Snowflake):

-- Invoiced sales per calendar month in the warehouse copy, to the cent, keyed by month as YYYY-MM.
-- Pass: every month ties to the wholesale system.
SELECT
    -- Dates landed as text, so the invoice date is read back as a date first.
    TO_CHAR(TO_DATE(i."InvoiceDate"), 'YYYY-MM') AS KEY,
    SUM(il."ExtendedPrice") AS VALUE
FROM LOAD_SQLSERVER_WORLDWIDEIMPORTERS.SALES_INVOICELINES il
-- Each invoice line takes its month from its invoice.
JOIN LOAD_SQLSERVER_WORLDWIDEIMPORTERS.SALES_INVOICES i
    ON i."InvoiceID" = il."InvoiceID"
GROUP BY 1

The control is the source, on the Wholesale Distribution System (SQL Server):

-- Invoiced sales per calendar month in the wholesale system, to the cent, keyed by month as YYYY-MM.
-- Pass: every month ties to the warehouse copy.
SELECT
    -- Style 120 writes a date as yyyy-mm-dd, so its first seven characters are the month.
    CONVERT(char(7), i.InvoiceDate, 120) AS "KEY",
    SUM(il.ExtendedPrice) AS "VALUE"
FROM Sales.InvoiceLines il
-- Each invoice line takes its month from its invoice.
JOIN Sales.Invoices i
    ON i.InvoiceID = il.InvoiceID
GROUP BY CONVERT(char(7), i.InvoiceDate, 120)

Both sides use Process data as Table, with KEY as Key and VALUE as Value. The result configuration:

Setting Value
Unmatched Keys: Test Only Fail
Unmatched Keys: Control Only Fail
Treat values as Numeric
Value Success Criteria Exact Match Only
Overall Success Criteria All Rows Must Pass

Each side reads its own month from a different date format, which is why the SQL differs: the warehouse stored dates as text, so its query converts them back first. The comparison only works because both queries produce the same YYYY-MM key.

The result of Wholesale Invoiced Sales Tie Out by Month: 167 rows, 135 passed and 32 failed, with the grid filtered to failed rows and April 2024 and the first Control Only month highlighted

What the result shows

For a single-value comparison, the result shows the test value and the control value, and whether their difference is within your tolerance.

For a key-based comparison, the result has two parts:

  • Overall Results: counts of Total Rows, Passed and Failed for each Match Status. Matched rows had their key on both sides. Test Only and Control Only rows had their key on one side.
  • The row grid: one line per key, with its Match Status, the test value, the control value and the difference (a % Difference when you compare by percent). Filter it to the failed rows to see exactly which keys broke.

In the worked example, every month through March 2024 ties out to the cent. From April 2024 on, every month fails: invoiced sales are in the wholesale system and not in the warehouse. Months where the warehouse has no invoices at all appear as Control Only rows. Nothing in the load errored. The copy stopped, and the comparison is what noticed.

For more on reading results, see Run a Test and Read Its Results.

Do it with Validatar AI (Validatar Cloud, beta). Open the AI panel and ask: "Create a standard test in the Warehouse Reconciliation project, folder Wholesale System to Warehouse, that compares invoiced sales per month, to the cent, between Sales.Invoices and Sales.InvoiceLines on the Wholesale Distribution System and their copies in LOAD_SQLSERVER_WORLDWIDEIMPORTERS on the Enterprise Data Warehouse. Key on the month as YYYY-MM and fail on any month that is missing or different." Review what it creates before you run it. You can undo the change from the chat history.

Compare many tables

One standard test compares one pair of queries. Two tools take the same idea further:

  • Every table, one check each. A template test writes the comparison once with variables for the schema and table names and creates a child test per table. The Warehouse Reconciliation project compares the row counts of all 49 wholesale tables with one template. See Create a Template Test.
  • Every row of many tables. Reconciling Source and Target with Key-Based Templates explains the Key-Based Reconciliation pack from the Marketplace: templates that compare key coverage, row counts per key and whole rows for every table you mark, and name each missing or different key.

Troubleshooting

Symptom Cause Fix
Every row is Test Only or Control Only The keys differ in format, type or case between the two sides Make both queries return the key identically, for example cast both to text in the same format
Totals differ by a cent here and there Different numeric types round differently Use Absolute Numeric Difference with a small tolerance such as 0.01