ERPClimb logoERPClimb

Code examples · Business Data Cloud · Advanced

Prove a data product is fresh, unique and complete: three SQL checks

The problem

A daily open-orders metric looks fine because the job is green, but cancelled orders are still counted as open.

How we solve it

  1. 1Freshnesshours behind
  2. 2Uniquenessduplicate keys
  3. 3Reconcilestatus counts

The result

Each check returns one small result you can alert on: hours behind, number of duplicate keys, and the statuses that differ.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A daily open-orders metric looks fine because the job is green, but cancelled orders are still counted as open. The team needs checks that prove the data is current and correct, not just that the pipeline ran.

2. How we design it

A successful job does not prove correct data. We define three explicit expectations and test each with one query: freshness (the newest change is recent), uniqueness (one row per key) and reconciliation (status counts match a trusted reference).

Why we decided it this way

  1. 1Freshness is a numberHours since the newest change is easy to alert on. 'The job ran' is not.
  2. 2Separate duplicates from missing updatesA duplicate key and a missing cancellation need different fixes, so each gets its own check.
  3. 3Reconcile on a small, agreed measureCounting orders per status is cheap and finds the cancelled-but-open case directly.

Before you start

  • A table or data product with an order key, a status and a last-changed timestamp. This example assumes columns order_id, status and changed_at; your product will have other names.
  • A trusted reference for the status counts, for example a table loaded directly from the source for the same day.
  • Agree the freshness limit with the business first, for example 26 hours for a daily load.

3. The code, step by step

Step 1 of 3: How far behind are we?

One number: hours between now and the newest change.

SQL · SQL · check 1 freshness
-- Hours since the newest change
SELECT
  (unix_timestamp(current_timestamp()) - unix_timestamp(MAX(changed_at))) / 3600 AS hours_behind,
  CASE
    WHEN (unix_timestamp(current_timestamp()) - unix_timestamp(MAX(changed_at))) / 3600 > 26
      THEN 'STALE'
    ELSE 'OK'
  END AS freshness_status
FROM my_catalog.my_schema.orders_product;
Part by part (plain text)
  1. Hours since the newest change. MAX( changed_at ) is the newest change the consumer has. unix_timestamp turns a timestamp into seconds, so the difference divided by 3600 is hours. The CASE compares it with the agreed limit and returns a plain word you can alert on.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

4. The final outcome

Each check returns one small result you can alert on: hours behind, number of duplicate keys, and the statuses that differ.

What you should see

Example output shape: hours_behind = 30.5 (limit is 26: alert) duplicate_keys = 0 status cancelled: product 118, reference 131 (differs: investigate)

Try it and check the result

  • Run each check once on good data: expect OK, no rows and no rows.
  • Insert one duplicate key and one wrong status into a copy of the table, then run again. Each check should catch exactly its own problem.
  • Schedule the three queries and alert on STALE or any returned row.

Common mistakes

  • A timestamp column that stores the load time hides stale business data. Use the business change time.
  • Counts can match while values differ. Add an amount total to the reconciliation for money fields.
  • Time zones: check that changed_at and current_timestamp() use the same time zone.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: Needs an SAP Business Data Cloud tenant with SAP Databricks. Reviewed against documentation only; catalog, schema, table and column names are placeholders you replace with your own.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.