简体   繁体   English

SQL 历史查询未返回所有结果

[英]SQL Historian Query not returning all results

I am using an historian to search for certain values of a specific tag.我正在使用历史学家来搜索特定标签的某些值。 The historian has certain rules, such as, I cannot create or drop tables, queries need tagnames etc.历史学家有一定的规则,例如,我不能创建或删除表,查询需要标记名等。

I want to search a TagName 'Tank1' for example and return its DateTime and Value results, then search further Tags using these results to match those tags that have the same Values at that DateTime.例如,我想搜索 TagName 'Tank1' 并返回其 DateTime 和 Value 结果,然后使用这些结果进一步搜索标签以匹配在该 DateTime 具有相同值的那些标签。

I search 'Tank1' between a given date and time and receive 4 results as below我在给定日期和时间之间搜索“Tank1”并收到 4 个结果,如下所示

2021-11-02 08:00:54.9870000 2021-11-02 08:00:54.9870000 1 1
2021-11-02 10:22:27.9850000 2021-11-02 10:22:27.9850000 1 1
2021-11-02 11:47:31.3360000 2021-11-02 11:47:31.3360000 2 2
2021-11-02 23:11:57.8120000 2021-11-02 23:11:57.8120000 2 2

So, I need to search four other Tags and return results that match the dateTime and value.因此,我需要搜索其他四个标签并返回与 dateTime 和 value 匹配的结果。

The below code is what I have produced (I should now tell you that I am a virtual novice)下面的代码是我制作的(我现在应该告诉你我是一个虚拟新手)

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

This works fine, albeit it only produces the last dateTime and Value result below,这很好用,尽管它只产生下面的最后一个 dateTime 和 Value 结果,

2021-11-02 23:11:57.8120000 2021-11-02 23:11:57.8120000 FM_S2_batch 2 FM_S2_batch 2

Am I right in assuming this is because the Variable is being overwritten each time and only holding the last values?我是否正确地假设这是因为变量每次都被覆盖并且只保存最后一个值?

The results that should be returned should look something like the results below应该返回的结果应该类似于下面的结果

2021-11-02 08:00:54.9870000 2021-11-02 08:00:54.9870000 FM_S1_batch 1 FM_S1_batch 1
2021-11-02 10:22:27.9850000 2021-11-02 10:22:27.9850000 FM_S1_batch 1 FM_S1_batch 1
2021-11-02 11:47:31.3360000 2021-11-02 11:47:31.3360000 FM_S2_batch 2 FM_S2_batch 2
2021-11-02 23:11:57.8120000 2021-11-02 23:11:57.8120000 FM_S2_batch 2 FM_S2_batch 2

Is there anyway I can do several scans and save each result until I have all the results needed?无论如何我可以进行多次扫描并保存每个结果,直到获得所需的所有结果? or is there an easier more suitable method (which I am guessing there is).还是有更简单更合适的方法(我猜有)。

TIA TIA

If I'm understanding correctly, you are hoping your int and datetime variables (@AT1DateTime and @AT1Value) will hold more than one value returned by the first query.如果我理解正确,您希望您的intdatetime变量(@AT1DateTime 和@AT1Value)将包含第一个查询返回的多个值。 That won't work (as you indicated, they will hold only one value).那是行不通的(正如您所指出的,它们将只保留一个值)。

From the code provided, it's not clear that you need to store those values in a variable at all.从提供的代码来看,您根本不清楚是否需要将这些值存储在变量中。 I think you are probably looking for something like:我想你可能正在寻找类似的东西:

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

This is your two queries combined together into one.这是您的两个查询合并为一个。 The 2nd/middle WHERE condition of the outer query checks that the a.value/a.datetime combination exists in the inner query.外部查询的第二个/中间WHERE条件检查 a.value/a.datetime 组合是否存在于内部查询中。

Thanks to everyone who took the time to reply to my problem.感谢所有花时间回复我的问题的人。 I finally got it working using a cursor thanks to @EdmCoff for suggesting trying a for-loop or a cursor.感谢@EdmCoff 建议尝试使用for-loop 或cursor,我终于使用cursor 让它工作了。

The for-loop returned the last value 4 times so I tried the cursor and it worked for me. for 循环返回最后一个值 4 次,所以我尝试了 cursor,它对我有用。

I have posted the code that works for me below我在下面发布了适合我的代码

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                                    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM