# Why timestamp joins still pin machine data to the wrong order

> Getting it right means knowing which job held the machine at every moment, including when the machine stopped partway through an order.

Original: https://fdetimes.net/en/guides/isa-95-machine-data-production-orders/

Many teams combine machine data with MES data after the fact, by aligning timestamps or joining tables downstream, according to software vendor Sepasoft.

The SQL is usually very short: this machine, from start time to end time. But the OPC UA specification for ISA-95 allows the end time to be empty, and the actual equipment too.

For an FDE, this bug is a nasty one because it raises no error. The query runs and the chart looks good, while energy, speed or scrap is charged to the wrong order. When a shop-floor supervisor disputes the number, you have to be able to explain every row.

ISA-95 helps by giving each link in the chain a name. You need a few of its concepts before writing the first line of SQL.

## How many layers separate an order from a machine

ISA, the organization behind the standard, says ISA-95 is mainly concerned with the interface between level 3 (manufacturing operations, such as MES) and level 4 (business systems, such as ERP). The sensor data you receive does not carry those concepts. Connecting the two is the integrator's job.

From the top down, a Work Schedule contains all the activities needed to meet a production request, meaning the production order. Below it, each Work Request has one or more Job Orders, and a Job Order defines the work in one work center. The ERP order number is therefore not enough to tell you which job a machine is running.

A Job Order is a request: carry out this unit of work. A Job Response is a report: how the work went, with actual start and end times. The report's JobOrderID field points back to the request.

Equipment also has two sides. ISA-95 separates role-based equipment from the physical asset: the physical device gets replaced, while the equipment in the system's logic stays the same. If a sensor reports the ID of a specific physical item, you need to know which role that item filled at the time of measurement.

**Key point:** A timestamp tells you when. To know which order, you also need the equipment and the state of the job.

## One morning, 121 readings

This hypothetical example packs in the situations that most easily lead to wrong assignments. Packaging machine PACK-02 reports its speed once a minute from 08:00 through 10:00, for a total of 121 readings. Job JO-118 starts at 08:00, is interrupted at 08:40, resumes at 09:10 and finishes at 10:00; JO-119 starts at that moment and is still open.

Filter JO-118 with `BETWEEN` from 08:00 to 10:00 and you get all 121 readings. The 10:00 reading is counted for both jobs. Of the remaining 120, 30 fall within the interruption.

Say the machine runs at a steady 60 packs a minute and drops to 0 when stopped. Across 120 readings, average speed is 45; counting only running time, it is 60. Both numbers are useful, but one describes the entire time the job occupied the machine and the other its capacity while running.

There is one more trap: at 09:30, sensor unit SN-A is replaced by SN-B. Anyone filtering on SN-A sees only 60 of the 90 running readings. Nothing reports that 30 readings have disappeared.

That same morning, job JO-121 runs from 08:00 to 09:00, but its report leaves the actual equipment empty. Nobody should guess which machine it ran on.

## Store run segments instead of two timestamps

The OPC UA Job Control model lets a job enter the Interrupted state and then resume. A single StartTime, EndTime pair therefore cannot express the full run history. A compact approach is to store each state segment as a row and to keep a separate table recording which sensor was attached to which equipment, and over what time range.

The `equipment_id` column may be null, paired with the label `unknown`, so that jobs with missing equipment still have a place.

```sql
create extension if not exists btree_gist;

create table work_request (
  work_request_id text primary key,
  production_order_id text not null
);
create table job_order (
  job_order_id text primary key,
  work_request_id text not null references work_request
);
create table job_segment (
  job_order_id text not null references job_order,
  equipment_id text, -- null when the actual machine is unknown
  equipment_source text not null
    check (equipment_source in ('actual', 'planned', 'unknown')),
  state text not null check (state in ('running', 'interrupted')),
  during tstzrange not null,
  check ((equipment_id is null) = (equipment_source = 'unknown')),
  -- only one job holds a machine at any moment
  exclude using gist (equipment_id with =, during with &&)
);
create table asset_on_equipment (
  asset_id text not null,
  equipment_id text not null,
  during tstzrange not null,
  exclude using gist (asset_id with =, during with &&)
);
create table reading (
  asset_id text not null,
  ts timestamptz not null, -- device time
  speed numeric,
  primary key (asset_id, ts)
);
```

Next, insert that morning's data. In PostgreSQL, the two-argument range constructor includes the start and excludes the end, so the reading at exactly 10:00 belongs only to JO-119.

```sql
insert into work_request values ('WR-1', 'PO-7001'), ('WR-2', 'PO-7002');
insert into job_order values
 ('JO-118', 'WR-1'), ('JO-119', 'WR-2'), ('JO-121', 'WR-2');

insert into job_segment values
 ('JO-118','PACK-02','actual','running',
  tstzrange('2026-03-02 08:00Z','2026-03-02 08:40Z')),
 ('JO-118','PACK-02','actual','interrupted',
  tstzrange('2026-03-02 08:40Z','2026-03-02 09:10Z')),
 ('JO-118','PACK-02','actual','running',
  tstzrange('2026-03-02 09:10Z','2026-03-02 10:00Z')),
 ('JO-119','PACK-02','actual','running',
  tstzrange('2026-03-02 10:00Z', null)), -- not finished
 ('JO-121', null, 'unknown', 'running', -- missing equipment
  tstzrange('2026-03-02 08:00Z','2026-03-02 09:00Z'));

insert into asset_on_equipment values
 ('SN-A','PACK-02', tstzrange('2026-03-02 00:00Z','2026-03-02 09:30Z')),
 ('SN-B','PACK-02', tstzrange('2026-03-02 09:30Z', null));

insert into reading
select case when t < '2026-03-02 09:30Z' then 'SN-A' else 'SN-B' end,
       t,
       case when t >= '2026-03-02 08:40Z'
             and t < '2026-03-02 09:10Z' then 0 else 60 end
from generate_series('2026-03-02 08:00Z'::timestamptz,
                     '2026-03-02 10:00Z', interval '1 minute') as t;
```

The join follows the concepts in order: reading, equipment, run segment, job, production order. Use `left join` so that unmatched readings show up instead of vanishing.

```sql
select w.production_order_id, s.job_order_id, s.state,
       count(*) as readings, avg(r.speed) as avg_speed
from reading r
left join asset_on_equipment a
  on a.asset_id = r.asset_id and a.during @> r.ts
left join job_segment s
  on s.equipment_id = a.equipment_id and s.during @> r.ts
left join job_order j on j.job_order_id = s.job_order_id
left join work_request w on w.work_request_id = j.work_request_id
group by 1, 2, 3
order by 2, 3;
```

The result is 90 running and 30 interrupted readings for JO-118, plus 1 reading for JO-119. But a total of 121 proves nothing: `left join` keeps every reading, and the two `exclude` constraints block duplicate rows. The total comes out right even when the assignments are wrong.

Remove the SN-B row from `asset_on_equipment` and the total is still 121, but 31 readings fall into the group with no job. JO-118 is left with only 60 running readings, and JO-119 disappears. The test therefore has to inspect the empty group and the row count for each job:

```sql
select count(*) filter (where s.job_order_id is null) = 0
   and count(*) filter (where s.job_order_id = 'JO-118'
                          and s.state = 'running') = 90
   and count(*) filter (where s.job_order_id = 'JO-118'
                          and s.state = 'interrupted') = 30
   and count(*) filter (where s.job_order_id = 'JO-119'
                          and s.state = 'running') = 1
   as passed
from reading r
left join asset_on_equipment a
  on a.asset_id = r.asset_id and a.during @> r.ts
left join job_segment s
  on s.equipment_id = a.equipment_id and s.during @> r.ts;
```

JO-121 does not appear here because it has no equipment to join on. It needs its own query, run alongside the test above:

```sql
select w.production_order_id, s.job_order_id, s.during
from job_segment s
join job_order j on j.job_order_id = s.job_order_id
join work_request w on w.work_request_id = j.work_request_id
where s.equipment_id is null;
```

The query returns exactly one row: JO-121, belonging to PO-7002. That is a question for the operator; fill in `equipment_id` and change the label to `actual` only after they confirm.

## What to ask on the shop floor before creating tables

The schema above is right only if its assumptions about the shop floor are right. Put these four questions to the shift operator and the MES administrator, with a real order open on the screen.

| Question | What the answer decides |
|---|---|
| When the machine stops mid-order, who records it, and where? | The source of the `interrupted` segments |
| If an order moves to another machine, is the MES updated? | When you may record `actual` |
| Do two jobs ever hold the same machine at once? | Whether to keep or drop the `exclude` constraint |
| Is this time recorded by the device or on receipt by the server? | Which column is used as `ts` |

Press hard on the last one. TigerData's sample schema states that the ts column is the time of the event at the device, separate from the time of ingestion. If a gateway holds data for 20 minutes during a network outage, ingestion time can push a whole batch of readings into the next order.

## Four mistakes that make data look complete but wrong

The first common mistake is filling in planned equipment when the actual field is empty. The OPC UA for Machinery specification draws a clear distinction: the plan may be in EquipmentRequirements, the actual in EquipmentActuals. If you have to use the plan, record `planned` and make that visible to whoever reads the report; otherwise, leave it as `unknown`.

The next is treating an empty EndTime as the current time and then storing it as a fixed value. It may be empty simply because the job is unfinished, so leave the range open. If someone forgets to close a job, the `exclude` constraint will reject the new job on the same machine, and you put the case in a queue for the operator to confirm.

Orders that cut in are also often missed. If JO-120 runs while JO-118 is interrupted, close the `interrupted` segment on that machine before opening the JO-120 segment. JO-118 is still unfinished; it just no longer holds the machine.

The last mistake is confusing equipment levels. In this model, a work unit is any element below a work center, while a Job Order defines work at the work center level. If the sensor is attached to a work unit, you need a parent-child table to know which work center it belongs to, and the client must confirm which unit the job ran on.

## A skill you can prove with row counts

In job postings, phrases such as MES integration, OPC UA, historian or data contextualization signal that the work will run into this problem. Build the PACK-02 example as a small repo with four tests: order boundary, interruption, sensor replacement and missing equipment.

On your résumé, describe how you measured the match rate and how you handled the readings that did not match; include only numbers you have measured.

An exercise for your next client: choose one finished order, count the readings, then sit down with the machine operator and explain every row that could not be assigned. That list often says more about the plant than any diagram they send you.

**Try this week:**

- Run the example in this article on PostgreSQL, then add JO-120 cutting in from 08:50 to 09:05. Adjust the JO-118 segments and update the test: JO-118 has 90 running and 15 interrupted readings, JO-120 has 15, JO-119 has 1, and no reading lacks a job.
- For a system you are working on, measure the share of jobs missing actual equipment and the share with no end time yet.
- Read three industrial FDE job postings and underline every requirement involving MES, OPC UA or time-series data.

## Sources

- [ISA-95 Standard: Enterprise-Control System Integration](https://www.isa.org/standards-and-publications/isa-standards/isa-95-standard)

- [Job Order – OPC UA for ISA-95 - Part 4: Job Control](https://reference.opcfoundation.org/specs/OPC-10031-4/4.1)

- [6.3.5 ISA95JobResponseDataType – OPC UA for ISA-95 - Part 4: Job Control](https://reference.opcfoundation.org/specs/OPC-10031-4/6.3.5)

- [OPC UA for ISA-95 - Part 4: Job Control](https://reference.opcfoundation.org/specs/OPC-10031-4/full)

- [7.4.1 ISA95EquipmentElementLevelEnum – OPC Unified Architecture - Common Object Model: ISA-95](https://reference.opcfoundation.org/specs/OPC-10030/7.4.1)

- [8.2 Role based Equipment Information – OPC Unified Architecture - Common Object Model: ISA-95](https://reference.opcfoundation.org/specs/OPC-10030/8.2)

- [OPC UA for Machinery - Part 3: Job Management (Version 1.0.1)](https://reference.opcfoundation.org/specs/OPC-40001-3/full)

- [Why Your Unified Namespace Needs MES Context | Sepasoft](https://www.sepasoft.com/blog/sepaiq/why-your-uns-needs-mes-context/)

- [Ask Your Factory Floor Anything: Structuring Industrial Data for AI Agents](https://www.tigerdata.com/blog/ask-factory-floor-anything-structuring-industrial-data-ai-agents)

- [PostgreSQL 19 Documentation: Range Types](https://www.postgresql.org/docs/current/rangetypes.html)
