我正在使用历史学家来搜索特定标签的某些值。历史学家有一定的规则,例如,我不能创建或删除表,查询需要标记名等。
例如,我想搜索 TagName 'Tank1' 并返回其 DateTime 和 Value 结果,然后使用这些结果进一步搜索标签以匹配在该 DateTime 具有相同值的那些标签。
我在给定日期和时间之间搜索“Tank1”并收到 4 个结果,如下所示
2021-11-02 08:00:54.9870000 | 1 |
2021-11-02 10:22:27.9850000 | 1 |
2021-11-02 11:47:31.3360000 | 2 |
2021-11-02 23:11:57.8120000 | 2 |
因此,我需要搜索其他四个标签并返回与 dateTime 和 value 匹配的结果。
下面的代码是我制作的(我现在应该告诉你我是一个虚拟新手)
DECLARE @AT1Value INT,
@AT1DateTime DateTime
SELECT @AT1Value = Value, --GETS THE VALUES OF AT1 STERILISER
@AT1DateTime = DateTime --GETS THE DATETIME OF AT1 STERILISER VALUES
From Runtime.dbo.v_History
Where
Runtime.dbo.v_History.Tagname = 'AT1_Select_ster'
AND Runtime.dbo.v_History.DateTime >= '2021-11-02 08:00'
AND Runtime.dbo.v_History.DateTime <= '2021-11-03 08:01'
AND Runtime.dbo.v_History.Value > 0
Select a.DateTime,
a.TagName,
a.Value
From Runtime.dbo.v_History AS a --GETS THE VALUES OF THE FM TAGS AT THE DATETIME OF AT1 STERILISER VALUES
Where
((a.TagName = 'FM_S1_Batch' AND a.Value = @AT1Value AND a.DateTime = @AT1DateTime)
OR (a.Tagname = 'FM_S2_batch' AND a.Value = @AT1Value AND a.DateTime = @AT1DateTime)
OR (a.Tagname = 'FM_S3_batch' AND a.Value = @AT1Value AND a.DateTime = @AT1DateTime)
OR (a.Tagname = 'FM_S4_batch' AND a.Value = @AT1Value AND a.DateTime = @AT1DateTime))
AND a.Value > 0
这很好用,尽管它只产生下面的最后一个 dateTime 和 Value 结果,
2021-11-02 23:11:57.8120000 | FM_S2_batch 2 |
我是否正确地假设这是因为变量每次都被覆盖并且只保存最后一个值?
应该返回的结果应该类似于下面的结果
2021-11-02 08:00:54.9870000 | FM_S1_batch 1 |
2021-11-02 10:22:27.9850000 | FM_S1_batch 1 |
2021-11-02 11:47:31.3360000 | FM_S2_batch 2 |
2021-11-02 23:11:57.8120000 | FM_S2_batch 2 |
无论如何我可以进行多次扫描并保存每个结果,直到获得所需的所有结果?还是有更简单更合适的方法(我猜有)。
TIA