I have just started using the SQL Bridge for the first time and I'm having some trouble getting my head around setting it up to measure the duration time between start_time and finish_time in a database for a production batch record using the SQL Bridge module.
There are 3 batching units which I am trying to record times on the same table. A set of transaction groups have been setup for each unit (unit 1 shown below).
Currently I have the following transactions for each unit:
Unit 1 Batch Start:
Basic OPC/Group Items:
Unit_Select | OPC TAG | INT | Target: database-unit_number
start_time | OPC TAG | [System]Gateway/CurrentDateTime | DATETIME | Target: database-start_time
Action: OPC to DB / Insert new row
Trigger: Unit_Select | is active = (unit number int value 1)
Unit 1 Batch Finish
Basic OPC/Group Items:
Unit1_Discharge_Valve | OPC TAG | bool | Target: none, read only
finish_volume | OPC TAG | float | Target: database-finish_volume
finish_time | OPC TAG | [System]Gateway/CurrentDateTime | DATETIME | Target: database-finish_time
Action: OPC to DB / Update-custom: unit_number = 1 AND finish_time is null
Trigger: Unit1_Discharge_Valve | is ! = 0
These two transactions are working ok.
To try and write the duration_minutes, in the Batch Finish Group I also used the below SQL query to measure and write the duration time however as the table did not yet have the finish_time when executed, it would not write the value until the batch after this one was completed many hours later.
Triggered Expression Items:
duration_time | int | Expression Type: SQL Query | Target: database-duration_minutes
UPDATE batch_times
SET duration_minutes = TIMESTAMPDIFF(MINUTE, start_time, finish_time)
WHERE unit_number = 1
AND finish_time IS NOT NULL
ORDER BY start_time DESC
LIMIT 1;
I also tried using TIMESTAMPDIFF(MINUTE, start_time, NOW())
instead but I was still returning null values.
So I tried setting up a third Transaction for each unit that would
use the finish_time changing from NULL to NOT NULL as a trigger, then calculate the duration and write the duration using the SQL Query from above.
However I am struggling to implement this and getting the trigger to work, I have the following expression sql query tags setup as below.
I'm not sure if I am close or I have overcomplicated the whole process from the start?
Any help would be much appreciated, thanks.
finish_time_null - Run always - Trigger - Active on Value Change
SELECT finish_time
FROM batch_times
WHERE unit_number = 1
AND start_time IS NOT NULL
AND finish_time IS NULL
AND duration_minutes IS NULL
ORDER BY start_time DESC
LIMIT 1;
start_time - Run always
SELECT start_time
FROM batch_times
WHERE unit_number = 1
AND finish_time IS NULL
AND duration_minutes IS NULL
ORDER BY start_time DESC
LIMIT 1;
finish_time - Run always
SELECT finish_time
FROM batch_times
WHERE unit_number = 1
AND finish_time IS NOT NULL
AND duration_minutes IS NULL
ORDER BY start_time DESC
LIMIT 1;
duration_minutes - Target - Database-duration_minutes
UPDATE batch_times
SET duration_minutes = TIMESTAMPDIFF(MINUTE, start_time, finish_time)
WHERE unit_number = 1
AND finish_time IS NOT NULL
AND duration_minutes IS NULL
ORDER BY start_time DESC
LIMIT 1;
Database: MySQL
Table: batch_times
Columns:
id | int AI PK
unit_number | int
batch_date | date
product_name | varchar(50)
start_time | datetime
finish_time | datetime
duration_minutes | int
finish_volume | float
created_at | timestamp
t_stamp | datetime