Value of a set of tags at a predetermined value in SQL

115 Views Asked by At

I'm trying to bring values ​​to SQL from Wonderware Historian, in the code below it is taking values ​​from the StateSummaryHistory view, which is a view of the Historian Client that updates as the values ​​change in InTouch, I needed the SELECT i'm doing to return the values ​​for me to pass to the ReportBuilder.

SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = '20210521 10:08:51.000'
SET @EndDate = '20210521 11:08:51.000'
SET NOCOUNT OFF
SELECT StateSummaryHistory.TagName, StartDateTime, EndDateTime, Value, vValue
 FROM StateSummaryHistory
 WHERE StateSummaryHistory.TagName IN ('VTIS01_FT04', 'VTIS01_LOG_SUM_VOL', 'VTIS01_M02', 'VTIS01_STEP_NO') AND StateSummaryHistory.TagName = 'VTIS01_STEP_NO' AND Value = 22
 AND Value = 22
 AND wwVersion = 'Latest'
 AND wwRetrievalMode = 'Cyclic'
 AND wwCycleCount = 1
 AND StartDateTime >= @StartDate
 AND EndDateTime <= @EndDate

What I needed to bring were the values ​​of the tags 'VTIS01_FT04', 'VTIS01_LOG_SUM_VOL', 'VTIS01_M02' only when the tag 'VTIS01_STEP_NO' is at a value of 22, I tried to solve it with the LIKE and CASE commands, but it still didn't work, someone could give me a hand?

1

There are 1 best solutions below

0
On

How do you want to link the tags? You cannot say "retrieve values for tag1 when tag2 is equal to 22" because tags are independent. In fact they can be saved using different criteria. There is not a 1 to 1 relationship. You may want to link them using the timestamp. In that case you will need first to select the VTIS01_STEP_NO when it is equal to 22, and then use that timestamp to select the rest of the tags. You have to be very careful because the other tags may not have the exact same timestamp. Actually, it is not very likely they will do.