SQL Historian 查询未返回所有结果
SQL Historian Query not returning all results
我正在使用历史记录器来搜索特定标签的某些值。历史学家有一定的规则,例如,我不能创建或删除表,查询需要标记名等
我想搜索一个标记名称 'Tank1' 和 return 其日期时间和值结果,然后使用这些结果搜索更多标记以匹配在该日期时间具有相同值的那些标记。
我在给定日期和时间之间搜索 '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
因此,我需要搜索其他四个标签和 return 个与日期时间和值相匹配的结果。
下面的代码是我制作的(我现在应该告诉你我是虚拟新手)
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
这工作正常,尽管它只产生下面的最后一个日期时间和值结果,
2021-11-02 23:11:57.8120000
FM_S2_batch 2
我假设这是因为变量每次都被覆盖并且只保留最后一个值,我的假设是否正确?
应该 returned 的结果应该类似于下面的结果
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
如果我的理解正确,您希望您的 int
和 datetime
变量(@AT1DateTime 和 @AT1Value)将包含第一个查询返回的多个值。那行不通(正如您所指出的,它们将只包含一个值)。
从提供的代码来看,根本不清楚您是否需要将这些值存储在变量中。我认为您可能正在寻找类似的东西:
Select a.DateTime,
a.TagName,
a.Value
From Runtime.dbo.v_History AS a
Where
a.TagName IN ('FM_S1_Batch', 'FM_S2_batch', 'FM_S3_batch', 'FM_S4_batch')
AND EXISTS
(SELECT *
From Runtime.dbo.v_History b
Where
b.Tagname = 'AT1_Select_ster'
AND b.DateTime >= '2021-11-02 08:00'
AND b.DateTime <= '2021-11-03 08:01'
AND b.Value > 0
AND b.Value = a.Value
AND b.DateTime = a.DateTime
)
AND a.Value > 0
这是您的两个查询合二为一。外部查询的第二个/中间 WHERE
条件检查内部查询中是否存在 a.value/a.datetime 组合。
感谢所有花时间回复我的问题的人。
感谢@EdmCoff 建议尝试使用 for 循环或游标,我终于使用游标让它工作了。
for 循环返回最后一个值 4 次,所以我尝试了游标,它对我有用。
我在下面发布了适合我的代码
DECLARE @AT1Value INT,
@AT1DateTime DateTime
DECLARE cursor_result CURSOR
FOR SELECT Runtime.dbo.v_History.Value,
Runtime.dbo.v_History.DateTime
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-04 08:01'
AND Runtime.dbo.v_History.Value > 0
OPEN cursor_result
FETCH NEXT FROM cursor_result INTO
@AT1Value,
@AT1DateTime
WHILE @@FETCH_STATUS = 0
Select a.DateTime,
a.TagName,
a.Value
From Runtime.dbo.v_History AS a
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
FETCH NEXT FROM cursor_result INTO
@AT1Value,
@AT1DateTime
END
CLOSE cursor_result
DEALLOCATE cursor_result
我正在使用历史记录器来搜索特定标签的某些值。历史学家有一定的规则,例如,我不能创建或删除表,查询需要标记名等
我想搜索一个标记名称 'Tank1' 和 return 其日期时间和值结果,然后使用这些结果搜索更多标记以匹配在该日期时间具有相同值的那些标记。
我在给定日期和时间之间搜索 '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 |
因此,我需要搜索其他四个标签和 return 个与日期时间和值相匹配的结果。
下面的代码是我制作的(我现在应该告诉你我是虚拟新手)
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
这工作正常,尽管它只产生下面的最后一个日期时间和值结果,
2021-11-02 23:11:57.8120000 | FM_S2_batch 2 |
我假设这是因为变量每次都被覆盖并且只保留最后一个值,我的假设是否正确?
应该 returned 的结果应该类似于下面的结果
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
如果我的理解正确,您希望您的 int
和 datetime
变量(@AT1DateTime 和 @AT1Value)将包含第一个查询返回的多个值。那行不通(正如您所指出的,它们将只包含一个值)。
从提供的代码来看,根本不清楚您是否需要将这些值存储在变量中。我认为您可能正在寻找类似的东西:
Select a.DateTime,
a.TagName,
a.Value
From Runtime.dbo.v_History AS a
Where
a.TagName IN ('FM_S1_Batch', 'FM_S2_batch', 'FM_S3_batch', 'FM_S4_batch')
AND EXISTS
(SELECT *
From Runtime.dbo.v_History b
Where
b.Tagname = 'AT1_Select_ster'
AND b.DateTime >= '2021-11-02 08:00'
AND b.DateTime <= '2021-11-03 08:01'
AND b.Value > 0
AND b.Value = a.Value
AND b.DateTime = a.DateTime
)
AND a.Value > 0
这是您的两个查询合二为一。外部查询的第二个/中间 WHERE
条件检查内部查询中是否存在 a.value/a.datetime 组合。
感谢所有花时间回复我的问题的人。 感谢@EdmCoff 建议尝试使用 for 循环或游标,我终于使用游标让它工作了。
for 循环返回最后一个值 4 次,所以我尝试了游标,它对我有用。
我在下面发布了适合我的代码
DECLARE @AT1Value INT,
@AT1DateTime DateTime
DECLARE cursor_result CURSOR
FOR SELECT Runtime.dbo.v_History.Value,
Runtime.dbo.v_History.DateTime
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-04 08:01'
AND Runtime.dbo.v_History.Value > 0
OPEN cursor_result
FETCH NEXT FROM cursor_result INTO
@AT1Value,
@AT1DateTime
WHILE @@FETCH_STATUS = 0
Select a.DateTime,
a.TagName,
a.Value
From Runtime.dbo.v_History AS a
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
FETCH NEXT FROM cursor_result INTO
@AT1Value,
@AT1DateTime
END
CLOSE cursor_result
DEALLOCATE cursor_result