Tag Historical data - frequency and duration of tag values

This not a job for the tag historian. Events should be recorded in a dedicated table with timestamp, preferably as just the numeric code. A transaction group would be fine, or just a tag change event script. A separate table should have the association between code and text. Don't record the text (terribly wasteful of DB space).

Once you have events recorded, your database's lead() or lag() functions can help you obtain precise intervals, which the database can then sum(), with or without splitting intervals across shift boundaries. The shift boundaries can come from other tables, possibly with support for rotating shifts (also in the DB). The DB's lead() or lag() functions can also clean up any intervals where disruptions cause the same value to be recorded as the previous one.

Some of the DB operations can be complex, but regular scheduled maintenance operations can insert the results into pre-computed aggregate tables most useful for reports.

There are many ways to compute pieces of this task. All others are nightmares to assemble into the final result you indicate you want. This is a database task. Use the database.

For durations from event recordings in a time interval, see this:

For rotating shifts, this is a comprehensive solution:

4 Likes